2

Once start the test run, remote web-driver has initialized for the given capabilities:

Below is the selenium node log:[Here I am setting some chrome options]

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}]

Depends on my requirement I can able to set chrome options in the remote webdriver capabilities. but couldn't get those values.

I need to get deviceMetrics and userAgent values from the desired capabilities?

I tried with::

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

Getting null value in the chromeoptions.

skrrgwasme
  • 9,358
  • 11
  • 54
  • 84

1 Answers1

1

I believe this information is not possible to get using getCapabilities() method since

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

Output:

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}

As you can see there is no desired information about deviceMetrics and userAgent.

Another option to try (if you are fine to execute 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);

Output:

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
drets
  • 2,583
  • 2
  • 24
  • 38
  • Thx for the quick reply. Its working fine for user agent and pixel ratio not for width and height. seems its taking browser width and height value instead of which i mentioned in the chrome options desired capabilities(deviceMetrics). And also suggest me any other way to define device orientation in the mobileEmulation. – Manibharathi Sundaram Dec 22 '15 at 17:37
  • Caveat: you need to execute js related to width and height after going on some page (that's why I mentioned `www.google.com` in the example). – drets Dec 22 '15 at 17:40
  • yes i tried that with after navigated to some page. Still getting incorrect width and height value. – Manibharathi Sundaram Dec 22 '15 at 17:48
  • device Orientation is possible in mobile emulation ? – Manibharathi Sundaram Dec 22 '15 at 17:49
  • Thanks, answer updated! About additional questions - please google it or ask new question - thanks for understanding. – drets Dec 22 '15 at 17:51
  • 1
    can able to take exact width and height values using js: long width = (Long) js.executeScript("return screen.width"); long height = (Long) js.executeScript("return screen.height"); sorry, before actually i tried width window.innerWidth & windows.innerHeight – Manibharathi Sundaram Dec 23 '15 at 05:24