Selenium WebDriver Java Framework Course Limited Time Offer for $20

Selenium WebDriver Java Framework Course Limited Time Offer for $20

 

Print

Use Selendroid to Test Mobile Web Application on Mobile Device

To assure the quality of mobile web application, we need to test mobile web application on real mobile devices instead of testing it on mobile emulator. This example teaches you how to test mobile web application on android mobile device. 

In this article, we use Ebay mobile site as a test application.  To identify web elements, please read the article titled "How to Identify Mobile Web Application Elements for Selendroid" . 

Test Case

Verify that users are able to search merchandise on Ebay mobile web site.

Test Step

Step 1: navigate to http://m.ebay.com

Step 2: type in android in the search box

Step 3: click on search button or press enter.

Step 4: assert that the website returns number of android merchandises.

Automation Test Implementation

Step 1: create a Java project named "SeleniumMasterMobileTest", add a package named "com.seleniummaster.mobiletest", and add a JUnit test class named "MobileWebTesteBayMobile.java". Make sure that in the build path, you referenced all required libraries as well as the Selendroid web driver library "selendroid-standalone-0.9.0-SNAPSHOT-with-dependencies.jar". 

 

Step 2: write the following code in the MobileWebTesteBayMobile.java class.

 
package com.seleniummaster.mobiletest;

import static org.junit.Assert.*;
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.Keys;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
public class MobileWebTesteBayMobile {
  private SelendroidLauncher selendroidServer = null;
  private WebDriver driver = null;
  @Test
  public void ShouldLogin() throws InterruptedException {
      
      driver.get("http://m.ebay.com");
      Thread.sleep(5000);
    //enter items to search
      driver.findElement(By.id("kw")).sendKeys("android");
      Thread.sleep(2000);
      //click enter button
      driver.findElement(By.id("kw")).sendKeys(Keys.ENTER);
      Thread.sleep(5000);
      //assert the Search Count text has the word results 
      for (int second = 0;; second++) {
        if (second >= 60fail("timeout");
        try if (isElementPresent(By.id("SearchCount"))) breakcatch (Exception e) {}
        Thread.sleep(1000);
      }

      assertTrue(driver.findElement(By.id("SearchCount")).getText().contains("results"));
    }
   
  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 3: attach any android device to the computer and make sure that you unlock the device before start the JUnit test

Step 4: run the test as JUnit test and verify the result. 

The above result indicated that Ebay mobile web test passed. 

Watch the video below to check the test on Android Mobile device