Run Appium Tests on Real Device - Android [Mobile WebApp]

To execute Appium tests on Real device, we need to make sure that device is connected to PC and has Developer Mode option enabled. Once device and app are configured, we can run our tests on that device by passing the desired capabilities.

I Assume all the below Prerequisites are installed and configured :-

1. JDK should be installed
2. Android should be installed and path should be setup in your machine. If not done, Please refer this Install Android tutorial.
3. Appium should be installed. If not done, Please refer this Install Appium
4. Configure Device with Developer Mode option enabled. If not done, Please refer this How to Connect Real Android device To PC by enabling USB debugging mode

And also I assume you are already familiar with Eclipse, TestNG and Selenium, as we will use them to run Appium automated tests on Android Real device in the below example.

Let us create new class with name as 'AppiumChromeExample.java' and we will add selenium jar to the project.

package com.appium.adnroid;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.Assert;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

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

public class AppiumChromeExample {
	WebDriver driver;
	WebDriverWait wait;
	String AppURL = "http://www.seleniumeasy.com";

	@BeforeTest
	public void setup() throws MalformedURLException {

		// Create an object for Desired Capabilities
		DesiredCapabilities capabilities = new DesiredCapabilities();

		// Name of mobile web browser to automate. ‘Safari’ for iOS and ‘Chrome’
		// or ‘Browser’ for Android
		capabilities.setCapability("browsername", "chrome");

		// The kind of mobile device or emulator to use - iPad Simulator, iPhone
		// Retina 4-inch, Android Emulator, Galaxy S4 etc
		capabilities.setCapability("deviceName", "WKZLMJLB7L8TR8SW");

		// Which mobile OS platform to use - iOS, Android, or FirefoxOS
		capabilities.setCapability("platformName", "Android");

		// Java package of the Android app you want to run- Ex:
		// com.example.android.myApp
		capabilities.setCapability("appPackage", "com.android.chrome");

		// Activity name for the Android activity you want to launch from your
		// package
		capabilities.setCapability("appActivity", "com.google.android.apps.chrome.Main");

		// Initialize the driver object with the URL to Appium Server and
		// passing the capabilities
		driver = new RemoteWebDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
		wait = new WebDriverWait(driver, 5);
	}

	@Test
	public void testSearchAppium() {
		driver.get(AppURL);
		WebElement titleElement = driver.findElement(By.cssSelector("#site-name>a"));
		String homePageTitle = titleElement.getText();
		Assert.assertEquals(homePageTitle, "Selenium Easy");

		WebElement searchElement = driver.findElement(By.name("search_block_form"));
		searchElement.sendKeys("Appium Tutorials");

		WebElement searchBtnEle = driver.findElement(By.id("edit-submit"));
		searchBtnEle.click();

		By byPageTitle = By.id("page-title");
		wait.until(ExpectedConditions.presenceOfElementLocated(byPageTitle));

		String searchPageTitle = driver.findElement(byPageTitle).getText();
		Assert.assertEquals(searchPageTitle, "Search");
	}

	@AfterTest
	public void tearDown() {
		driver.quit();
	}
}

In order to run our appium tests, we will need to specify the desired capabilities. We can either do this in our test code, or we can have them in appium.txt files. Basically, Desired capabilities is a JSON object (a set of keys and values) sent by the client to the server. Using Desired capabilities, we can create a session for a browser with our own set of desired capabilities to have more control on the server during automation.

To work with Desired capabilities, we will import "import org.openqa.Selenium.remote.DesiredCapabilities" library.

In the above example, we have used desired capabilities which are based on my app. Please change the values as per you needs.

If the device is not connected to PC, you will see error as "org.openqa.selenium.SessionNotCreatedException: A new session could not be created. (Original error: Could not find a connected Android device.) (WARNING: The server did not provide any stacktrace information)".

Hope this tutorial helps you to run your first test on real device. Please let us know in the comments if you face any issues.

Appium Tutorials: 

Comments

Great, it worked for me :)

Hii,
I am following all steps which mentioned by you. But issue is that chrome browser unable to get and open link. It open open browser, display "data0" message in searchbar and close the browser. It happens 3 times then test fail. Please suggest me solution on it. Thanks in advance..

Add new comment

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.