Running Appium Test Scripts on Real Devices

Test Native Features of your Mobile Application by running Appium Test Scripts on Real Android & iOS devices

Get Started free
Home Guide How to Run Your First Appium Test Script

How to Run Your First Appium Test Script

By Neha Vaidya, Community Contributor -

Automation is a mandatory part of software testing. From web applications to mobile apps, all software can benefit from automation testing. Given the necessity of automation in QA pipelines, the market is flooded with various tools for automation out of which Appium is frequently used for testing mobile applications.

Its many features make it a particular favorite of app testers. At the very least, understanding the fundamentals of Appium testing goes a long way in helping you shape, execute, and leverage effective QA strategies

If you are new to Appium testing, this is a good place to learn what it is and how it works. This article will discuss how to run your first Appium test script to verify mobile apps for real-world functionality.

Introduction to Appium

Appium is an open-source automation testing framework for applications supported by various platforms like iOS, Android, macOS, and Windows. It enables the tester to write test scripts in different programming languages such as JavaScript, Java, Ruby, Python, PHP, and C#. It also offers cross-platform testing wherein a single API can be used for both Android and iOS platforms.

Appium does not carry dependencies on mobile operating systems. It comprises a wrapper of framework that translates Selenium Webdriver commands into UIAutomation (iOS) or UIAutomator (Android) commands depending on the device type. The OS type does not factor in here.

Key Features of Appium

Appium’s efficacy for app automation testing across native, hybrid, or mobile web applications is well-known by now. It has several features that have led to such favourable reviews of its usefulness:

  • It is basically a server that runs in the background.
  • It is synchronized with the TestNG testing framework providing a wide range of features.
  • Appium can produce detailed information logs and provides a detailed reporting structure for better analysis of test results.
  • Appium supports test code in different languages (Java, JavaScript, PHP, Ruby, Python, and C#).
  • It allows code reusability for different device platforms such as iOS, Android, and Windows.
  • Testers do not modify or recompile the app under test, as Appium uses the standard automation APIs on all platforms. 

Getting Started with Appium Testing

Appium can be installed:

  • using NPM or
  • downloading Appium Desktop, a graphical, desktop-based avenue to launch the Appium server.

In this case, let’s download using Appium Desktop.

  1. Go to release page and download the latest version of Appium, according to your system configuration.
  2. Follow the on-screen instructions and install Appium.First Appium Test Script
    Appium Setup Complete
  3. Once the setup is complete, run the Appium server GUI and check for the host and port configuration as shown below:Appium server GUI
  4. As a next step, go to the Advanced tab and configure the server address and port as shown in the snapshot below. Now, you can start the server by clicking on the Start Server button shown:Appium server details
  5. Once the Appium server starts, you can see the server status as shown below:
  6. Now, you can connect your mobile device to the system through a USB cable. Open the command prompt and run the command: ADB devices to check the details of the device.
  7. You will also need to provide your mobile phone’s Android version number in the Appium script.

    To find this, open your mobile phone and go to Setting > About Phone. In the About Phone screen, you can see the Android version as shown below:

Now that all the prerequisites are set up, let’s now understand how to write the test script on the IDE.

Run Appium Tests for Free

How to run Appium Test Script 

  1. Create a project in Eclipse, add a package and create a class to write the test script.
  2. Make sure the libraries are configured with the Appium dependencies.
  3. The code below helps you understand how to write Appium code to launch the play store application:
import java.net.MalformedURLException;

import java.net.URL;

import org.openqa.selenium.remote.DesiredCapabilities;

import io.appium.java_client.AppiumDriver;

import io.appium.java_client.MobileElement;

import io.appium.java_client.android.AndroidDriver;

public class AppiumTestcase {

public static void main(String[] args) {

//Set the Desired Capabilities

DesiredCapabilities caps = new DesiredCapabilities();

caps.setCapability("deviceName", "My Phone");

caps.setCapability("udid", "device id"); //Give Device ID  caps.setCapability("platformName", "Android");

caps.setCapability("platformVersion", "11.0");

caps.setCapability("appPackage", "com.android.vending");

caps.setCapability("appActivity", "com.google.android.finsky.activities.MainActivity");

caps.setCapability("noReset", "true");

//Instantiate Appium Driver

try {

AppiumDriver driver = new AndroidDriver(new URL("http://0.0.0.0:4723/wd/hub"), caps);

} catch (MalformedURLException e) {

System.out.println(e.getMessage());

}

}

}

This script would launch the Play Store app on your mobile device. The best way to proceed is to run your script and then monitor the Appium Desktop screen. 

You will notice that the Appium Desktop screen will start showing some logs. This is how you will know that your Appium test script is running. 

Now, let’s understand how to write the Appium test script using Browserstack and how BrowserStack eases the manual method of writing the test case.

Run Appium Test Script on BrowserStack

BrowserStack App Automate enables you to test native and hybrid mobile applications using the Appium automation framework on thousands of real Android and iOS devices. It’s easy to run Appium tests written in Java on real Android and iOS devices on BrowserStack. 

If you are new to Browserstack, refer to this document on executing your first Selenium test case on Browserstack cloud:

Run Selenium tests with Java | BrowserStack Docs

Setup 

Run Appium Test on Real Devices for Free

 

// Maven users can add this dependency to project's POM

<dependency>

<groupId>io.appium</groupId>

<artifactId>java-client</artifactId>

<version>7.0.0</version>

</dependency>

 

  • Ensure that you have access to an Android app (.apk or .aab file) or an iOS app (.ipa file)
/* Note the "app_url" value for the sample app. This value uniquely identifies the app on BrowserStack. */

{"app_url":"bs://c700ce60cf13ae8ed97705a55b8e022f13c5827c"}

/* In your test script, use this "app_url" value to specify the application under test using the "app" capability. During test execution, the sample app will automatically be installed and launched on the device being tested. */

caps.setCapability("app", "bs://c700ce60cf13ae8ed97705a55b8e022f13c5827c")
  • If you do not have an application, please use the sample android or iOS app. In this case, let’s use an Android application.Use sample apps to run Appium script
  • As a next step, configure the code as shown in the screenshot below. In this case, lets select a real OnePlus 9 device with Android version 11 from the drop down as shown below.

Also, copy the code snippet from the App Automate dashboard to execute the test case. 

Talk to an Expert

Execution of Appium Test Script:

package android;
import java.net.URL;

import java.util.List;

import java.util.function.Function;

import java.net.MalformedURLException;

import io.appium.java_client.MobileBy;

import io.appium.java_client.android.AndroidDriver;

import io.appium.java_client.android.AndroidElement;

import org.openqa.selenium.support.ui.ExpectedConditions;

import org.openqa.selenium.support.ui.WebDriverWait;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.remote.DesiredCapabilities;

public class BrowserStackSample {

public static void main(String[] args) throws MalformedURLException, InterruptedException {

     DesiredCapabilities caps = new DesiredCapabilities(); 
       // Set your access credentials

     caps.setCapability("browserstack.user", "your_username");

     caps.setCapability("browserstack.key", "your_key");
    
     // Set URL of the application under test

     caps.setCapability("app", "bs://c700ce60cf13ae8ed97705a55b8e022f13c5827c");
    
     // Specify device and os_version for testing

        caps.setCapability("device", "OnePlus OnePlus 9");

        caps.setCapability("os_version", "11.0");
        
     // Set other BrowserStack capabilities

     caps.setCapability("project", "First Java Project");

     caps.setCapability("build", "browserstack-build-1");

     caps.setCapability("name", "first_test");
       
   
     // Initialize the remote Webdriver using BrowserStack remote URL

     // and desired capabilities defined above

        AndroidDriver<AndroidElement> driver = new AndroidDriver<AndroidElement>(

         new URL("http://hub.browserstack.com/wd/hub"), caps);
        
        // Test case for the BrowserStack sample Android app. 

        // If you have uploaded your app, update the test case here. 

        AndroidElement searchElement = (AndroidElement) new WebDriverWait(driver, 30).until(

            ExpectedConditions.elementToBeClickable(

             MobileBy.AccessibilityId("Search Wikipedia")));

        searchElement.click();

AndroidElement insertTextElement = (AndroidElement) new WebDriverWait(driver, 30).until(

             ExpectedConditions.elementToBeClickable(

              MobileBy.id("org.wikipedia.alpha:id/search_src_text")));

        insertTextElement.sendKeys("BrowserStack");

        Thread.sleep(5000);

        List<AndroidElement> allProductsName = driver.findElementsByClassName(

         "android.widget.TextView");

        assert(allProductsName.size() > 0);

        // Invoke driver.quit() after the test is done to indicate that the test is completed.

        driver.quit();

}

}

You are now ready to run your first Appium test on BrowserStack. On your local machine, open the terminal/command prompt and navigate to the folder containing your test script. Build and run the test script just like any other Java program using your project’s preferred build tools (E.g. Maven, Gradle).

BrowserStack App Automate Banner

# Using Maven

mvn test -P <android-first-test>

After executing the test case you can view the results on the BrowserStack dashboard.

That’s how it works.

BrowserStack App Automate offers cloud-based access to both the latest and legacy devices (Android, iOS, and Windows) installed with real operating systems. App Automate also requires no additional setup, helping testers save precious time and meet their deadlines that much faster. Run Appium tests on thousands of real devices so that the app becomes truly cross-platform compatible and user-friendly.

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

Featured Articles

How to Download and Install Appium

Desired Capabilities in Appium

Automation Tests on Real Android & iOS Devices

Seamlessly Run Mobile App Automation Tests on real Android & iOS Devices