如何从远程webdriver功能获取chromeOptions值?

时间:2015-12-22 15:47:31

标签: selenium selenium-webdriver selenium-chromedriver selenium-grid chrome-options

启动测试运行后,远程Web驱动程序已针对给定功能进行了初始化:

下面是selenium节点日志:[这里我设置了一些chrome选项]

19:16:08.245 INFO - Creating a new session for Capabilities [{browserName=chrome, takesScreenshot=true, chromeOptions={mobileEmulation={deviceMetrics={pixelRatio=2, width=768, height=1024}, userAgent=Mozilla/5.0 (iPad; CPU OS 8_0 like Mac OS X) AppleWebKit/600.1.3 (KHTML, like Gecko) Version/8.0 Mobile/12A4345d Safari/600.1.4}, args=[--ignore-certificate-errors, --disable-bundled-ppapi-flash, --disable-extensions, --disable-web-security, --always-authorize-plugins, --allow-running-insecure-content, --test-type, --enable-npapi], extensions=[]}, version=,platform=ANY}]

取决于我的要求我可以在远程webdriver功能中设置chrome选项。但无法获得这些价值观。

我需要从所需的功能中获取deviceMetricsuserAgent值吗?

我尝试使用::

String chromeoptions= ((RemoteWebDriver) driver).getCapabilities().getCapability(ChromeOptions.CAPABILITY);

在chromeoptions中获取null值。

1 个答案:

答案 0 :(得分:1)

相信此信息无法使用getCapabilities()方法,因为

Capabilities actualCapabilities = ((RemoteWebDriver) driver).getCapabilities();
System.out.println("Capabilities: " + actualCapabilities.asMap());

输出:

Capabilities: {platform=MAC, javascriptEnabled=true, acceptSslCerts=true, browserName=chrome, chrome={userDataDir=/var/folders/w7/jwwf8cq50mz5g3wdwhxgxgy80000gn/T/.org.chromium.Chromium.nh0o2Y}, rotatable=false, locationContextEnabled=true, mobileEmulationEnabled=true, webdriver.remote.sessionid=5622d7bc-9083-4f00-b852-e67cc885c818, version=47.0.2526.106, takesHeapSnapshot=true, cssSelectorsEnabled=true, databaseEnabled=false, handlesAlerts=true, browserConnectionEnabled=false, nativeEvents=true, webStorageEnabled=true, hasTouchScreen=true, applicationCacheEnabled=false, takesScreenshot=true}

如您所见,没有关于deviceMetricsuserAgent的所需信息。

尝试的另一个选项(如果您可以执行Javascript):

mobileEmulation.put("deviceName", "Google Nexus 5");

...

driver.get("http://www.google.com");

...

JavascriptExecutor js = (JavascriptExecutor) driver;

long pixelRatio = (Long) js.executeScript("return window.devicePixelRatio");
long width = (Long) js.executeScript("return screen.width");
long height = (Long) js.executeScript("return screen.height");

String userAgent = (String) js.executeScript("return navigator.userAgent");

System.out.println("pixelRatio: " + pixelRatio);
System.out.println("width: " + width);
System.out.println("height: " + height);
System.out.println("userAgent: " + userAgent);

输出:

pixelRatio: 3
width: 360
height: 640
userAgent: Mozilla/5.0 (Linux; Android 4.4.4; en-us; Nexus 5 Build/JOP40D) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2307.2 Mobile Safari/537.36