3

I need to verify that a certain condition happens on different platforms, e.g. a link of an app for iphone only happens on ios devices, and similarly, a link of an app for android only happens on android devices.

My question is if there is a way for selenium driver to detect which platform it is on so I can run a test something similar to:

if (driver == ios) {
    do something...
} else if (driver == android) {
    do something...
}
1
  • I assume your code is actually opening the driver, so have that code set a flag that can be accessed from your test scripts. Feb 24, 2016 at 13:03

3 Answers 3

3

I figured out the solution:

DesiredCapabilities capability;
if (capability.getPlatform().is(Platform.ANDROID)) {
    // do something...
} else if (capability.getPlatform().is(Platform.MAC) {
    // do something...
}
1

I think you can do this using the capability.setPlatform() key/value pair from DesiredCapabilities of Selenium. You can read about the official documentation here.

0
0

To determine the OS of mobile operating system you can use javascript.

Following is the javascript to detect android phone

function isAndriod() { 
 if( navigator.userAgent.match(/Android/i) {
 return true;
} else {
  return false;
}

And following is the javascript to detect the IOS devices

function isIOS() { 
 if(navigator.userAgent.match(/iPhone/i) || navigator.userAgent.match(/iPad/i) || navigator.userAgent.match(/iPod/i)) {
 return true;
} else {
  return false;
}

You can execute this javascripts using JavascriptExecutor of selenium and based on return value you can perform your future operations.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.