Selenium Mobile Web Testing Tutorial

Home/Selenium/Selenium Mobile Web Testing Tutorial

Selenium Mobile Web Testing Tutorial

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.

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

These first steps need to be done no matter if you want to test with Selendroid or the ChromeDriver.

  1. Download the latest ChromeDriver
  2. 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.
  3. 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
  4. On your Android phone activate the developer mode and usb debugging.
  5. Now connect your phone via an USB cable to your computer which is supposed to run the Selenium tests on your phone.
  6. Launch the command prompt and cd to the folder “platform-tools” ind your SDK-Folder.Cmd: cd to platform-tools
  7. Execute the command: “adb devices”. The result should give you a list of one device (your phone that is connected to the computer.)Cmd Adb Device List
  8. 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.
  9. Execute the command: “adb start-server”
  10. Now you are ready to automate the web on your device.

 

Mobile Web Automation with ChromeDriver

Now we look into how to automate your mobile device with the ChromeDriver and -browser.

  1. If you have not already for your previous Selenium testing download the latest ChromeDriver. Remember the location where you have downloaded the ChromeDriver.
  2. 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:Chrome Driver On Mobile Android Phone

 

Mobile Web Automation with Selendroid

And last but not least we will automate our mobile android browser with Selendroid.

  1. Download the Selendroid Stand Alone Server
  2. Launch the command prompt an cd to the folder where you downloaded the jar.
  3. Execute the command: java -jar selendroid-standalone-0.16.0-with-dependencies.jar
  4. Just leave the command prompt window open with the selendroid server running which usually creates quite a bit of output. Cmd Selendroid Stand Alone Server
  5. Open a second command prompt window.
  6. cd again to …/sdk/platform-tools and execute the command “adb devices” again to refresh the adb server.
  7. 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:Selendroid On Mobile Android Device

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.

By |October 5th, 2015|Selenium|0 Comments

Leave A Comment