Learning Mobile Web Automation
Appium can be a great source of help, in case you are looking for Mobile web Automation. Further, in Appium can be a great source in case you want to automate your web app in Mobile Safari on iOS or Chrome on Android. For this purpose, you write a normal WebDriver test, and thereby use Appium as the Selenium server with a special set of desired capabilities.
iOS Mobile Automation
The Appium can automate the Safari browser on real and simulated iOS devices. Further, it is accessed by setting the browserName desired capabilty to “Safari” while leaving the app capability empty. Therefore, you must run Safari on the device before attempting to use Appium, in order to set the correct preferences .
Desired capabilities to run your test in mobile Safari (In Java)
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(MobileCapabilityType.PLATFORM_NAME, “iOS”);
capabilities.setCapability(MobileCapabilityType.PLATFORM_VERSION, “11.0”);
capabilities.setCapability(MobileCapabilityType.BROWSER_NAME, “Safari”);
capabilities.setCapability(MobileCapabilityType.DEVICE_NAME, “iPhone 8”);
Prerequisites to run your tests against Safari on a real device
- Firstly, you should have the ios-webkit-debug-proxy installed, running and listening on port 27753
- Secondly, turn on web inspector on iOS device (settings > safari > advanced)
- Lastly, make sure that SafariLauncher will work
How to run your test?
In order to run your test, it is very important to configure you test to run against safari simply set the “browserName” to be “Safari”, in Java
//setup the web driver and launch the webview app.
DesiredCapabilities desiredCapabilities = new DesiredCapabilities();
desiredCapabilities.setCapability(MobileCapabilityType.BROWSER_NAME, “Safari”);
URL url = new URL(“http://127.0.0.1:4723/wd/hub”);
AppiumDriver driver = new AppiumDriver(url, desiredCapabilities);
// Navigate to the page and interact with the elements on the guinea-pig page using id.
driver.get(“http://saucelabs.com/test/guinea-pig”);
WebElement div = driver.findElement(By.id(“i_am_an_id”));
Assert.assertEquals(“I am a div”, div.getText()); //check the text retrieved matches expected value
driver.findElement(By.id(“comments”)).sendKeys(“My comment”); //populate the comments field by id.
//close the app.
driver.quit();
Android Mobile Automation
Appium supports automating the Chrome browser both real and emulated Android devices. Some of the prerequisites include,
- To make sure Chrome is installed on your device or emulator.
- Also, Chromedriver needs to be installed (a default version comes with Appium) and configured for automating the specific version of Chrome available on the device.
Desired capabilties like these to run your test in Chrome
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(MobileCapabilityType.PLATFORM_NAME, “Android”);
capabilities.setCapability(MobileCapabilityType.PLATFORM_VERSION, “4.4”);
capabilities.setCapability(MobileCapabilityType.DEVICE_NAME, “Android Emulator”);
capabilities.setCapability(MobileCapabilityType.BROWSER_NAME, “Chrome”);
Note that on 4.4+ devices, you can also use the ‘Browser’ browserName cap to automate the built-in browser. On all devices you can use the ‘Chromium’ browserName cap to automate a build of Chromium which you have installed.
How to troubleshoot Chromedriver?
With reference to the Chrome version 33, a rooted device is no longer required. If running tests on older versions of Chrome, devices needed to be rooted as Chromedriver required write access to the /data/local directory to set Chrome’s command line arguments. But, in case you are testing on Chrome app prior to version 33, then you must ensure that adb shell has read/write access to /data/local directory on the device.