Print

Using Selendroid For Mobile Web Test Automation

Selendroid is a test automation framework which drives off the UI of Android native and hybrid applications (apps) and the mobile web. Tests are written using the Selenium 2 client AP. In this example, you will learn how to use Selendroid for testing a mobile web application. Test site for this example is the Mahara ePortfolio demo site "http://demo.mahara.org".

Test case:

Admin user should be able to log on to the system.

Test Implementation

Step 1: Download the Selendroid Standalone Java libraries from this site "http://selendroid.io/". If latest build is available on this location, download the latest snapshot from here "http://ci.selendroid.io/job/selendroid/io.selendroid%24selendroid-standalone/".

Step 2: Install Android SDK from the android development site "http://developer.android.com/sdk/index.html" . And setup the android development environment according to the instruction on this site "http://spring.io/guides/gs/android/".

 

Step 3: Create an Android Virtual Device by using Android Virtual Device Manager after setting up android development environment.

Step 4: Start the android virtual device. Click on the start button to start the android virtual device.

Step 5: Create a Java Project, add Selendroid Standalone libraries and JUnit libraries in the build path.

Step 6: Add a JUnit test class to the project and name it "MobileWebTest.java". Write the following code in this java class.

package com.seleniummaster.mobiletest;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import io.selendroid.SelendroidCapabilities;
import io.selendroid.SelendroidConfiguration;
import io.selendroid.SelendroidDriver;
import io.selendroid.SelendroidLauncher;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
public class MobileWebTest {
  private SelendroidLauncher selendroidServer = null;
  private WebDriver driver = null;

  @Test
  public void ShouldLogin() throws InterruptedException {
      
      driver.get("http://demo.mahara.org/index.php");
      Thread.sleep(20000);
    //clear username filed
      driver.findElement(By.id("login_login_username")).clear();
      //enter user name
      driver.findElement(By.id("login_login_username")).sendKeys("admin");
      //clear password
      driver.findElement(By.id("login_login_password")).clear();
      //enter password
      driver.findElement(By.id("login_login_password")).sendKeys("Testing1");
      //click on submit button
      driver.findElement(By.id("login_submit")).click();
      //assert the Dashboard link text
      for (int second = 0;; second++) {
        if (second >= 60fail("timeout");
        try if (isElementPresent(By.linkText("Dashboard"))) breakcatch (Exception e) {}
        Thread.sleep(1000);
      }

      assertEquals("Dashboard", driver.findElement(By.linkText("Dashboard")).getText());
    }
   
  private boolean isElementPresent(By by) {
      try {
        driver.findElement(by);
        return true;
      catch (NoSuchElementException e) {
        return false;
      }
    }

  @Before
  public void startSelendroidServer() throws Exception {
    if (selendroidServer != null) {
    selendroidServer.stopSelendroid();
   }
    SelendroidConfiguration config = new SelendroidConfiguration();

    selendroidServer = new SelendroidLauncher(config);
    selendroidServer.lauchSelendroid();

    DesiredCapabilities caps = SelendroidCapabilities.android();

    driver = new SelendroidDriver(caps);
 // Thread.sleep(50000);
  }

  @After
  public void stopSelendroidServer() {
    if (driver != null) {
      driver.quit();
    }
    if (selendroidServer != null) {
      selendroidServer.stopSelendroid();
    }
  }
}

Step 7: Run the JUnit test.

Step 8: Check the test Result.

Below is the video of the test run.