selenium 如何在使用移动仿真(Java)的同时向ChromeDriver添加参数?

ocebsuys  于 2022-11-10  发布在  Java
关注(0)|答案(4)|浏览(208)

我正在使用ChromeDriver和我的JUnit/Selify测试。我有台式机和移动设备的测试。
对于我的桌面测试,我指定了几个参数以确保测试结果一致,如下所示:

DesiredCapabilities capabilities = DesiredCapabilities.chrome();
ChromeOptions desktopOptions = new ChromeOptions();

desktopOptions.addArguments("start-maximized", "disable-extensions",
    "test-type", "no-default-browser-check", "ignore-certificate-errors");

capabilities.setCapability(ChromeOptions.CAPABILITY, desktopOptions);
driver = new RemoteWebDriver(service.getUrl(), capabilities);

对于我目前使用的移动测试:

Map<String, String> mobileEmulation = new HashMap<String, String>();
mobileEmulation.put("deviceName", "Apple iPhone 6");

Map<String, Object> mobileOptions = new HashMap<String, Object>();
mobileOptions.put("mobileEmulation", mobileEmulation);

DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability(ChromeOptions.CAPABILITY, mobileOptions);
driver = new RemoteWebDriver(service.getUrl(), capabilities);

我看不到在我的移动测试中包含参数的方法(比如“禁用扩展”)。
有没有什么方法可以重构移动方法以允许指定参数?
编辑:说得更清楚一点--我想做一些类似以下的事情:

Map<String, String> mobileEmulation = new HashMap<String, String>();
        mobileEmulation.put("deviceName", deviceType);

        Map<String, Object> mobileOptions = new HashMap<String, Object>();
        mobileOptions.put("mobileEmulation", mobileEmulation);

        ChromeOptions chromeArgs = new ChromeOptions();
        chromeArgs.addArguments("disable-extensions",
                "test-type", "no-default-browser-check", "ignore-certificate-errors");

        DesiredCapabilities capabilities = DesiredCapabilities.chrome();
        capabilities.setCapability(ChromeOptions.CAPABILITY, mobileOptions);
        capabilities.setCapability(ChromeOptions.CAPABILITY, chromeArgs);
        driver = new RemoteWebDriver(service.getUrl(), capabilities);

但这些能力相互覆盖。

ylamdve6

ylamdve61#

得到解决方案=)
(感谢ChromeDRIVER-USERS谷歌小组):

DesiredCapabilities capabilities = DesiredCapabilities.chrome();

Map<String, String> mobileEmulation = new HashMap<String, String>();
mobileEmulation.put("deviceName", "Apple iPhone 6");
Map<String, Object> mobileOptions = new HashMap<String, Object>();
mobileOptions.put("mobileEmulation", mobileEmulation);

mobileOptions.put("args", Arrays.asList("disable-extensions",
"test-type", "no-default-browser-check", "ignore-certificate-errors"));

capabilities.setCapability(ChromeOptions.CAPABILITY, mobileOptions);
driver = new RemoteWebDriver(service.getUrl(), capabilities);
ix0qys7i

ix0qys7i2#

使用ChromeOptions的setExperientalOption()方法

ChromeOptions提供了一个方法集实验选项(字符串名,对象值)。这允许您设置许多选项,包括您已设置的mobileEmulation参数。
将您的代码放在一起,您可以使用以下代码:

DesiredCapabilities capabilities = DesiredCapabilities.chrome();
ChromeOptions chromeOptions= new ChromeOptions();

chromeOptions.addArguments("start-maximized", "disable-extensions",
"test-type", "no-default-browser-check", "ignore-certificate-errors");

Map<String, String> mobileEmulation = new HashMap<String, String>();
mobileEmulation.put("deviceName", "Apple iPhone 6");
chromeOptions.setExperimentalOption("mobileEmulation", mobileEmulation);

capabilities.setCapability(ChromeOptions.CAPABILITY, chromeOptions);
driver = new RemoteWebDriver(service.getUrl(), capabilities);
j9per5c4

j9per5c43#

也许你可以这样做:

capabilities.setCapability("chrome.switches", Arrays.asList("--disable-extensions"));

下面是一个类似的答案how to disable chrome extension in selenium

goucqfw6

goucqfw64#

您可以使用Selenium_Profiles
存储库仍在进行中//正在处理BTW

相关问题