How to run Appium tests on Android devices
Published: 2022-03-22 05:15:34

Automation is quickly becoming a mandatory part of software testing. From web applications to mobile apps, all software can benefit from [automation testing](https://www.browserstack.com/guide/automation-testing-tutorial). The market is flooded with various tools for automation out of which Appium is frequently used for testing mobile applications.

This article will dive deep into how Appium can be used to run tests on an Android device so that testers can verify their apps for real-world functionality.

## Table of Contents

- What is Appium?
- Run Appium Tests on Android Devices
- Why use Real Android Devices for Appium Tests?

## What is Appium?
[Appium](https://www.browserstack.com/guide/appium-tutorial-for-testing) is an open-source framework that allows us to conduct automation testing of mobile applications on different platforms like Android, iOS, and Windows.

It automates the testing for:

- Native Mobile Applications that are written using iOS, Android, or Windows SDKs
- Mobile Web Applications that can be accessed using mobile phone browsers such as Safari, Chrome, or the in-built native browser applications for android devices
- Hybrid Mobile Applications that have a native wrapper around the web view
- Appium is a cross-platform testing framework that is flexible, enabling you to write the testing code against multiple platforms such as iOS, Windows, and Android using the same API. This means you can use the same code for iOS that you have written for Android, saving lots of time and effort.

## Run Appium Tests on Android Devices
Let’s start this with the installation process. Follow the steps below for installation.

Download the following components to run Appium tests on Android:

- Appium Jar files for Java
- Latest Appium Client Library
- Appium Server
- Java
- TestNG
- Install Java on the system. Don’t forget to set the environment variables.
- Configure Device with Developer Mode option enabled.

The code below demonstrates how to write Appium test scripts. The example here uses Eclipse IDE to run Appium tests on Android devices.

`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 AppiumExample{
WebDriver driver;
WebDriverWait wait;
String AppURL = "https://www.browserstack.com/";
@BeforeTest
public void setup() throws MalformedURLException {
// Create an object for Desired Capabilities
DesiredCapabilities capabilities = new DesiredCapabilities();
// Name of mobile web browser to automate. capabilities.setCapability("browsername", "chrome");
//device name
capabilities.setCapability("GalaxyS4", "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 capabilities.setCapability("appPackage", "com.android.chrome");
// Activity name for the Android activity you want to launch from package capabilities.setCapability("appActivity", "com.google.android.apps.chrome.Main");
// Initialize the driver object with the URL to Appium Server passing the capabilities. Server Url may vary
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);
String homePageTitle = titleElement.getText();
Assert.assertEquals(homePageTitle, Most Reliable App & Cross Browser Testing Platform | Browserstack ");
WebElement LinkElement = driver.findElement(By.xpath("//body[@class='page-template-default page page-id-593 page-parent live persistent-header wpb-js-composer js-comp-ver-6.0.5 vc_responsive']")).click();
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 Appium tests, one must specify the desired capabilities. This can either be done in the test code, or one can have them in appium.txt files. Basically, desired capabilities refer to a JSON object (a set of keys and values) sent by the client to the server. Testers can create a session for a browser with its own set of desired capabilities to have more control over the server during automation.

## Why use Real Android Devices for Appium Tests?
Now that you know how to start off with running Appium tests on Android devices, let’s discuss a significant element of app testing – using real devices. Since this article discusses tests on Android devices, let’s focus on that.

Android emulators can be quite useful when testing code and features in the initial stages of development. However, when it comes to testing for public release, emulators are absolutely inadequate. They simply cannot replicate real user conditions such as low battery, IP geolocation, network throttling, and many other features.

Additionally. Android devices come in a wide variety of screen sizes and resolutions, any app would have to adapt to each in order to provide positive user experiences. Test responsive web design on emulators is quite difficult and tedious, as you would have to keep resizing the screen. Additionally, not every Android OS version or device actually has an emulator available in the first place.

In honesty, Android emulators are unreliable when it comes to testing Android apps. Given their various limitations, tests run on them often lead to inconclusive and inaccurate results. They cannot be trusted to test apps for public release. It is far easier and more trustworthy to test on real Android devices. That way, you know exactly how the site or app will operate in the hands of end-users.

Instead of building an in-house device lab that needs to be updated and maintained consistently, use a cloud-based testing platform like BrowserStack. With thousands of real Android devices (latest and older) available for instant access and testing, QAs and devs never have to buy a device or use an emulator for testing again.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vj5qsis65ia07epzil9c.png)
On BrowserStack’s real device cloud,  QAs can run both manual app testing as well as automated app testing (via Appium) on the cloud. 

BrowserStack also offers a host of advanced features that enable seamless, easy, and effective testing. A few of these features include:

- Geolocation Testing
- Network Simulation
- In-App Purchase
- Testing on dev environments
- Push Notifications
- App Uninstallation

Simply [sign up](https://www.browserstack.com/) for free, choose the Android device you want to test on, and start testing.