This tutorial will show you how to do automate website testing with Selenium on Mobile devices. Before you go through this tutorial you should be some what familiar with Selenium or have at least completed our Selenium WebDriver Tutorial.
Related Articles
So, we assume you have Eclipse installed and a simple Selenium Project set up. If not, go through the Selenium WebDriver Tutorial.
For different browsers on Android devices there are different best practices to automate them. In this tutorial I will show you the in my opinion most important ones with Android from a Windows PC:
- Automating the built in Android browser with selendroid.
- Automating the mobile Chrome browser with ChromeDriver
Prerequisites for both options: Installting the Android SDK and making the device ready for testing
- Download the latest ChromeDriver
- Install the Android SDK. You can either download and install the full Android Studio which includes the SDK or just download and install the Stand-alone SDK Tools. You find instructions for the installation on the linked pages.
- Make sure you remember where the SDK is installed. I installed the full Android Studio package and this way the SDK has been installed to C:\Users\PeterParker\AppData\Local\Android\sdk
- On your Android phone activate the developer mode and usb debugging.
- Now connect your phone via an USB cable to your computer which is supposed to run the Selenium tests on your phone.
- Launch the command prompt and cd to the folder “platform-tools” ind your SDK-Folder.
- Execute the command: “adb devices”. The result should give you a list of one device (your phone that is connected to the computer.)
- If your connected mobile device does shows up aus “unauthorized” in the list you might want to check on your phone if you need to authorize the pc. In that case there should be some kind of pop up that asks you if you want to authorize e.g. “some kind of mac-address” for USB-Debugging.
- Execute the command: “adb start-server”
- Now you are ready to automate the web on your device.
Mobile Web Automation with ChromeDriver
- If you have not already for your previous Selenium testing download the latest ChromeDriver. Remember the location where you have downloaded the ChromeDriver.
- Insert the location of your ChromeDriver for the system properties in the following code sample and then execute the code sample in a project that has reference the Selenium libs.
import java.net.MalformedURLException; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.chrome.ChromeOptions; import org.openqa.selenium.remote.DesiredCapabilities; /** * @author Nils Schuette via frontendtest.org */ public class MobileWebTestChrome { public static void main(String[] args) throws MalformedURLException { // Telling the system where to find the chrome driver System.setProperty( "webdriver.chrome.driver", "C:/Users/Nils/Programm Daten/Eclipse Libs/WebDriver/ChromeDriver/chromedriver.exe"); // Using chrome options for mobile test set-up ChromeOptions chromeOptions = new ChromeOptions(); chromeOptions.setExperimentalOption("androidPackage", "com.android.chrome"); DesiredCapabilities capabilities = DesiredCapabilities.chrome(); capabilities.setCapability(ChromeOptions.CAPABILITY, chromeOptions); // Launching the mobile browser WebDriver driver = new ChromeDriver(capabilities); // navigating to Google driver.get("http://www.google.com"); try { Thread.sleep(10000); } catch (InterruptedException e) { e.printStackTrace(); } // Check the title of the page System.out.println("Page title is: " + driver.getTitle()); driver.quit(); } }
The result on your phone:
Mobile Web Automation with Selendroid
- Download the Selendroid Stand Alone Server
- Launch the command prompt an cd to the folder where you downloaded the jar.
- Execute the command: java -jar selendroid-standalone-0.16.0-with-dependencies.jar
- Just leave the command prompt window open with the selendroid server running which usually creates quite a bit of output.
- Open a second command prompt window.
- cd again to …/sdk/platform-tools and execute the command “adb devices” again to refresh the adb server.
- Execute the following code sample in a project that has reference the Selenium libs.
import org.openqa.selenium.WebDriver; import org.openqa.selenium.remote.DesiredCapabilities; import org.openqa.selenium.remote.RemoteWebDriver; /** * @author Nils Schuette via frontendtest.org */ public class MobileWebTest { public static void main(String[] args) { //Launching the mobile browser WebDriver driver = new RemoteWebDriver(DesiredCapabilities.android()); //Navigating to Google driver.get("http://www.google.com"); try { Thread.sleep(10000); } catch (InterruptedException e) { e.printStackTrace(); } // Check the title of the page System.out.println("Page title is: " + driver.getTitle()); driver.quit(); } }
The result on your phone:
Now you know how to automate mobile testing on android phones. How to do that on iOS with an IPhone will be covered in later posts.