How to run Appium tests on Android devices

Learn how to run Appium tests on real devices. Test on real android devices with BrowserStack

Get Started free
How-to-run-Appium-tests-on-Android-devices
Home Guide How to run Appium tests on Android devices

How to run Appium tests on Android devices

Appium is a powerful open-source tool for automating mobile application testing across various platforms like Android and iOS. It allows developers to run tests on native and hybrid mobile applications using the same codebase, making it a highly versatile framework.

This article will guide you through running Appium tests on real Android devices, including setup, writing test scripts, and understanding the importance of real device testing for accurate results.

What is Appium?

Appium 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.

Benefits of Appium

Below are the reasons why Appium is important:

  • Cross-Platform Testing: Appium allows testing on Android, iOS, and Windows using a single API, saving time and effort by reusing code across platforms.
  • Language Flexibility: It supports multiple languages, such as Java, Python, Ruby, JavaScript, and C#, enabling developers to work in their preferred language.
  • No App Modification Needed: Appium allows testing of native, hybrid, and mobile web apps without altering the app code, making it convenient for production testing.
  • Real and Virtual Device Support: It supports real devices and emulators, providing the flexibility to test apps under different conditions.
  • Open-Source with Active Community: Free and open-source, Appium is continuously updated by a large community, ensuring it remains reliable and cutting-edge.
  • CI/CD Integration: Easily integrates with CI/CD tools like Jenkins, enabling automated mobile testing as part of the development pipeline.
  • Feature-Rich: Supports gestures, element interactions, and multiple orientations, covering various mobile testing needs.

How to Run Appium Tests on Android Devices

To run Appium tests on Android, you need to set up the necessary tools and components, including the Appium server, Java, TestNG, and the Appium client library. Once set up, write your test scripts using Java and the Appium WebDriver.

Here’s the basic process for setting up and writing Appium tests:

  1. Install the required tools (Appium, Java, TestNG, Appium Client Library, etc.)
  2. Enable developer mode on your Android device.
  3. Define Desired Capabilities for the test device.
  4. Write and execute test scripts using the Appium Java Client API.

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");
Copied
// 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();
}
}
Copied

Run Appium Tests on Real Android Devices for Free

Why use Real Android Devices for Appium Tests?

Although emulators are useful in the early stages of development, real Android devices provide the most accurate representation of how your app will perform in real-world conditions. Emulators often fail to replicate critical factors such as network throttling, battery performance, or actual hardware configurations, which can impact the end-user experience.

Testing on real Android devices offers several advantages:

  • Accurate user experience simulation: Ensures the app functions as expected under real conditions.
  • Variety of devices: Test on various devices, including multiple Android OS versions, screen sizes, and hardware configurations.
  • Better performance: Real devices offer faster and more reliable testing than emulators, leading to more precise results.

Use BrowserStack App Live and App Automate for Appium Tests

BrowserStack’s Real Device Cloud allows you to run manual and automated mobile app tests on real Android devices, ensuring that your tests reflect true user experiences.

With seamless integration of Appium, you can automate your tests on various Android devices without needing an in-house device lab.

BrowserStack App Automate Banner

Benefits of using BrowserStack for Appium Testing:

  • Real Devices: Test on thousands of real, physical Android devices, ensuring accurate, real-world results.
  • Cross-Device Compatibility: Easily test on multiple devices, OS versions, and screen sizes to ensure consistent app performance.
  • Geolocation Testing: Simulate various geographic locations for location-based app functionality testing.
  • Network Simulation: Test your app under different network conditions, such as 2G, 3G, or 4G.
  • Push Notifications: Test how your app handles push notifications in real user conditions.
  • Advanced Debugging Tools: Access a range of logs and video recordings for effective debugging.
  • Instant Access: Sign up for free and get instant access to the devices you need for testing, with no setup required.

Talk to an Expert

Useful Resources for Appium

Tutorials

Best Practices, Tips, and Tricks

Getting Started with

Differences and Comparisons

Conclusion

You can also leverage a plethora of debugging options for Appium testing on BrowserStack such as Text logs, Network Logs, Device Logs, Appium Logs, Visual logs, and Video Recordings of tests. Explore Debugging Options on BrowserStack App Automate

Since users demand high-functioning and engaging apps, Appium testing is an absolute requirement before releasing any apps. By running Appium tests on real Android devices, testers can ensure that apps are working as expected in real user conditions. Run as many tests as possible on as many real Android devices as possible to offer a consistently optimal user experience.

Tags
Appium Automation Testing Mobile App Testing