How to Run Selenium Tests On Android And IOS
How to Run Selenium Tests On Android And IOS
In todays post we will go over the process to setup and run Selenium tests for a mobile website or web application on Android and iOS. We will cover running tests on device emulators and on real mobile devices. You can perform all below steps on Windows, Linux and OS X / macOS with the exception of testing in the iPhone emulator which is only available on OS X / macOS.
What is Selenium?
Selenium is a free and open source browser automation framework backed by all major browser vendors including Google, Apple, Mozilla and Microsoft. If you want to find out more about the basics please check our recent post about Selenium 3.
Setup Android
To get testing on Android devices setup you will need to install and setup a few things.
- First you will need to install Java and the Android Studio which includes the Android SDK and the device emulator.
- Set an environment variable JAVA_HOME to the installation path of the Java SDK you installed above
- Set an environment variable ANDROID_HOME to the installation path of the Android SDK
- Install at least one Android SDK and the latest version of the Build Tools using the SDK Manager
- Create at least one emulator with the AVM Manager
- Now download the Android Selenium driver from http://selendroid.io
Your system is now ready to run Selenium tests against the Android emulator.
Run A Simple Test
We will now try to run a simple Selenium test against the Android browser. In this post I will use Python as an example but any other programming language will work just as well.
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
import time
driver = webdriver.Remote(desired_capabilities=DesiredCapabilities.ANDROID)
driver.get("http://www.google.com")
elem = driver.find_element_by_name("q")
elem.send_keys("Selenium")
elem.submit()
time.sleep(5)
driver.quit()
To run this test we need to start the Android driver with the command line:
java -jar selendroid-standalone-0.17.0-with-dependencies.jar
Now we can run our test which should automatically launch the emulator, open the browser and execute the test case above.
To run the test on a real device instead, connect the device using USB and unlock it. Now you can run the test on the device as well.
Setup iOS
To get testing on iOS devices working you need to setup a few things. Please note that this is only possible on OS X / macOS at the moment.
- Install Xcode using the AppStore
- Install Java (just type java into the terminal)
- Download the ios-driver at http://ios-driver.github.io/ios-driver/
- Modify the iPhone emulator with these terminal commands (you might need to adapt these based on the iPhoneSimulator you have)
$xcodeVersion=`xcode-select --print-path` $sudo chmod a+rw "$xcodeVersion/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator8.0.sdk/Applications„ $sudo chmod -R a+rw "$xcodeVersion/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator8.0.sdk/Applications/MobileSafari.app"
Your system is now ready to run Selenium tests against the iOS emulator.
Run A Simple Test
We will now run the same test case as above against the iPhone emulator. To do so we only need to change the desired capabilities object.
To run the test we need to start the ios-driver with the command line:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
import time
driver = webdriver.Remote(desired_capabilities=DesiredCapabilities.IPHONE)
driver.get("http://www.google.com")
elem = driver.find_element_by_name("q")
elem.send_keys("Selenium")
elem.submit()
time.sleep(5)
driver.quit()
java -jar ios-server-standalone-0.6.6-SNAPSHOT.jar
Now it is possible to run our test case against the iPhone emulator. To run against a real device you will need to attach it using USB and unlock the device.