How to run the Appium test on Android mobile browser using Selenium?

We have already discussed all the Appium basics required before running the Appium test on Android mobile browser. In general, we use Google Chrome on Android so in this tutorial we will learn to write code to launch Chrome browser in an Android mobile.

Concept

Here the test app is the same web application which we tested in desktop; hence, there won’t be any change in web elements. The difference with mobile testing of web app is the screen size, device, and resolution.

If you have worked on Selenium Grid, then it will be quite easier for you; because we can either use RemoteWebDriver or AndroidDriver or iOSDriver. It depends on your choice. So, we will discuss two coding techniques, the first technique will be by using AppiumDriver (AndroidDriver) and the second technique will be by using RemoteWebDriver.

Here we won’t require UiAutomatorViewer tool to inspect the object, because it’s a web application and almost all the elements will be the same as your desktop view. For example, the id of the signup button will not be changed, but its resolution will be different in mobile view as compared to a desktop view.

Recommended Reading: How to set up Appium in Eclipse?

Desired capabilities for Appium Test

We use desired capability in Selenium Grid to set the properties of the devices by using key-value pair. Since Appium works on the concept of Grid and Selenium server so here also we will set the properties of mobile devices through Desired capability.

Click here to know more about Capability in Appium for Android and iOS

Sample DesiredCapability Code to trigger Appium Test

DesiredCapabilities cap = new DesiredCapabilities();
cap.setCapability("deviceName", "device_name");
cap.setCapability("udid", "unique_device_id");
cap.setCapability("platformName", "Android_iOS");
cap.setCapability("platformVersion", "OS_Version");
cap.setCapability(CapabilityType.BROWSER_NAME, "Chrome");

It looks similar to the code for Grid.

Sample Testing Scenario of Appium test

In this scenario, we are just launching the Chrome browser in a mobile device and opening inviul.com. Driver retrieves the title of the home page and prints the same to the console.

Technique# 1: Running Appium Test with AndroidDriver

Following code is the sample program to launch the chrome browser in Android device by using AndroidDriver.

package AppiumPractice;

import org.testng.annotations.Test;

import io.appium.java_client.AppiumDriver;
import io.appium.java_client.MobileElement;
import io.appium.java_client.android.AndroidDriver;
import junit.framework.Assert;

import org.testng.annotations.BeforeMethod;

import java.net.MalformedURLException;
import java.net.URL;

import org.openqa.selenium.By;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.AfterMethod;

public class AppiumBrowserTestDemo {
	AppiumDriver<MobileElement> driver;
	DesiredCapabilities cap;
	
	
  @Test
  public void mobileTest() throws Exception {
	  URL url = new URL("https://127.0.0.1:4723/wd/hub");
		driver = new AndroidDriver<MobileElement>(url,cap);
		driver.get("https://www.inviul.com");
		System.out.println("Appium Test Started");
		String title = driver.getTitle();
		System.out.println("Title is- "+title);	  
  }
  
  
  @BeforeMethod
  public void beforeMethod() {
	  System.setProperty("webdriver.chrome.driver", "C:\\Appium\\\chromedriver.exe");	
		cap = new DesiredCapabilities();
	    cap.setCapability("deviceName", "vivo 1818");
		cap.setCapability("udid", "d7206a48");
		cap.setCapability("platformName", "Android");
		cap.setCapability("platformVersion", "9");
		cap.setCapability(CapabilityType.BROWSER_NAME, "Chrome");
  }

  @AfterMethod
  public void afterMethod() {
	  driver.close();
	  driver.quit();
  }

}

Technique# 2: Running Appium Test with RemoteWebDriver

This technique also demands a server so here we have Appium server. Code looks like Selenium Grid but command goes through Appium server, instead of Selenium server.

package AppiumPractice;

import java.net.URL;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;


public class AppiumTestWithRemoteDriver {
	WebDriver driver;
	DesiredCapabilities cap;
	
	
  @Test
  public void mobileTest() throws Exception {
	  URL url = new URL("https://127.0.0.1:4723/wd/hub");
		driver = new RemoteWebDriver(url,cap);
		driver.get("https://www.inviul.com");
		System.out.println("Appium Test Started");
		String title = driver.getTitle();
		System.out.println("Title is- "+title);  
  }
  
  
  @BeforeMethod
  public void beforeMethod() {
	  System.setProperty("webdriver.chrome.driver", "C:\\Appium\\\chromedriver.exe");	
		cap = new DesiredCapabilities();
	   cap.setCapability("deviceName", "vivo 1818");
		cap.setCapability("udid", "d7206a48");
		cap.setCapability("platformName", "Android");
		cap.setCapability("platformVersion", "9");
		cap.setCapability(CapabilityType.BROWSER_NAME, "Chrome");
  }

  @AfterMethod
  public void afterMethod() {
	  driver.close();
	  driver.quit();
  }
}

Console Output

Mobile Browser Appium Test Console

Video Recording

Join Inviul fb group

Leave a Reply