2

开始测试运行后,远程 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);

null在 chromeoptions 中获得价值。

4

1 回答 1

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}

如您所见,没有关于deviceMetrics和的所需信息userAgent

另一个尝试的选择(如果你可以执行 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
于 2015-12-22T17:11:21.657 回答