The Talent500 Blog
mobile app

Mastering Mobile App Test Automation with Appium: A Comprehensive Guide

Mobile applications have become an integral part of our daily lives, and ensuring their quality and performance is of utmost importance. Test automation plays a vital role in delivering high-quality mobile apps efficiently. Appium, an open-source test automation framework, has gained immense popularity due to its cross-platform support and seamless integration with different programming languages. In this blog, we will explore why Appium is the preferred choice for mobile app automation, how to set it up, write a demo script with real-time .apk

Introduction

Appium is an open-source framework for automating native, mobile web, and hybrid applications on Android mobile, iOS mobile, and Windows desktop platforms. For starters, Appium was developed by Dan Cuellar and Jason Huggins and today has about 15.2K stars and 5.7K Fork on GitHub. It has the highest market share compared to all the other mobile automation frameworks.

Amongst all the mobile automation tools (or frameworks), Appium is one of the most popular open-source test automation frameworks for mobile app testing. It is cross-platform and compatible with several popular programming languages (e.g., Java, Python, JavaScript, etc.) for app test automation. The best part is that you don’t have to recompile your app or modify it due to the usage of standard automation APIs on the supported platforms.

How to install Appium?

Appium can be installed in one of two ways: via NPM or by downloading Appium Desktop.

A. Installation via NPM

If you want to run Appium via an npm install, hack with Appium, or contribute to Appium, you will need Node.js and NPM (use nvm, n, or brew install node to install Node.js. Make sure you have not installed Node or Appium with sudo, otherwise you’ll run into problems).

npm install -g appium

B.Installation via Desktop App Download

Simply download the latest version of Appium Desktop from the releases page.

To verify that all of Appium’s dependencies are met we can use appium-doctor.
Install it with command npm install -g appium-doctor, then run the appium-doctor command, supplying the –ios or –android flags to verify that all of the dependencies are set up correctly.

How to Start an Appium Server?

The Appium server processes the request and then responds with the test results. Now we can kick up an Appium server, either by running it from the command line or by using Appium Desktop (assuming the npm install was successful).

  1. We can use the below command to start the Appium server:
  2. appiumYou will get to see Welcome to Appium like the below screenshot:Mastering Mobile App Test Automation with Appium: A Comprehensive Guide 1
  3. If you have installed Appium Desktop, then we can start by clicking on the ‘Start Server’ button inside of Appium Desktop as shown below.Mastering Mobile App Test Automation with Appium: A Comprehensive Guide 2Appium will now show you a little welcome message showing the version of Appium you’re running and what port it’s listening on (the default is 4723: You can use “netstat” to check whether a port is available or not).
  4. You can use port number 4726 instead of 4723. However, the server needs to be configured to run on the said port. This port information is vital since you will have to direct your test client to make sure to connect to Appium on this port. If you want to change the port, you can do so by using the -p flag when starting Appium (be sure to check out the full list of server parameters).

Appium testing with Demo Code

Here, we are using simple code, which can be used by anyone for their first test on Appium. To get detailed steps on installing the emulator and uploading the .apk with Inspector setup, please refer to the video provided here. These steps are essential prerequisites for running the Appium script on your local machine.

Sometimes it’s difficult to start from scratch, so to make life easier, you can clone the LambdaTest’s 🔗LT-appium-java repository and then replace the vanilla_android.java class with the code mentioned below.

In the below code there are total 4 section, which has also been highlighted in the code section with green colour:

  • Set desired capabilities for the Android device
  • Create an instance of the AppiumDriver
  • Perform some actions on the app
  • Quit the driver and close the app

Now under the  “Perform some actions on the app” we will automate the below test steps, which has also been highlighted in the code section with green colour and number sequencing:

  • click on the geolocation
 
  • validate Find is present

  • enter the url as google.com
  • click on find button

  • validate the logo

There are many websites that provide the sample apk, which can be used for testing. If you do not have any .apk or .ipa files, you can run your sample tests on LambdaTest using our sample 🔗 Android app or sample 🔗 iOS app.

import io.appium.java_client.AppiumDriver;
import io.appium.java_client.MobileElement;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.remote.MobileCapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.Assert;

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

public class mobileTests {

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

       // Set desired capabilities for the Android device
      DesiredCapabilities desiredCapabilities = new DesiredCapabilities();
      desiredCapabilities.setCapability(MobileCapabilityType.PLATFORM_NAME, “android”);
      desiredCapabilities.setCapability(MobileCapabilityType.PLATFORM_VERSION, “8.1.0”);
      desiredCapabilities.setCapability(MobileCapabilityType.DEVICE_NAME, “Pixel6_LambdaTestAppiumSeries”);
      desiredCapabilities.setCapability(MobileCapabilityType.AUTOMATION_NAME, “UiAutomator2”);
      desiredCapabilities.setCapability(MobileCapabilityType.APP, “/Users/sishukla/Downloads/proverbial_android.apk”);

      // Create an instance of the AppiumDriver

      AppiumDriver<MobileElement> driver = null;
      try {
          driver = new AndroidDriver<>(new URL(“http://localhost:4723/wd/hub”), desiredCapabilities);
      } catch (MalformedURLException e) {
          e.printStackTrace();
      }

      // Perform some actions on the app


      //1. click on the geolocation


      MobileElement geoLocationElement = driver.findElementById(“com.lambdatest.proverbial:id/geoLocation”);
      geoLocationElement.click();
      System.out.println(“clicked on geoLocation”);
      Thread.sleep(10000);
      MobileElement findButton = driver.findElementById(“com.lambdatest.proverbial:id/find”);


      //2. validate Find is present
      Assert.assertEquals(findButton.isDisplayed(), true, “Find button is not displayed”);
      System.out.println(“Find button is displayed”);


      //3. enter the url as google.com
      Thread.sleep(10000);
      MobileElement editUrlText = driver.findElementById(“com.lambdatest.proverbial:id/url”);
      editUrlText.sendKeys(“https://www.google.com”);
      System.out.println(“Entered the google url”);


      //4. click on find button
      Thread.sleep(10000);
      findButton.click();
      Thread.sleep(10000);
      System.out.println(“clicked on Find button”);


      //5. validate the logo
      MobileElement logo = driver.findElementByXPath(“//android.webkit.WebView[@content-desc=\”Google\”]/android.view.View[1]/android.view.View/android.view.View[2]”);
      Assert.assertEquals(logo.isDisplayed(), true, “Logo is not displayed”);
      System.out.println(“logo is displayed”);
      Thread.sleep(10000);


      // Quit the driver and close the app
      driver.quit();
  }
}

Note: To ensure smooth and stable execution, I have included thread.sleep(). In the subsequent blogs of this series, we will replace thread.sleep with explicit wait for improved performance.

Now we are halfway done. We just need to hit the below two commands to run our sample test for Appium testing on Android APK emulator online. The first command is to install dependencies and second one to run our suite.

To install the required dependencies:

mvn clean install

To run your test just type below command in your command prompt:

mvn compile exec:java -Dexec.mainClass=vanilla_android -Dexec.classpathScope=“test”

Conclusion

By offering a robust and adaptable framework that supports numerous platforms and programming languages, Appium has completely changed the way that mobile app tests are automated. In this article, we looked into why Appium is a popular option for automating mobile apps, how to set it up, write a demo script, recognise items, and carry out advanced tasks. Accept Appium for easy, effective, and trustworthy mobile app testing that improves the overall quality and user experience of the app.

To guarantee that your app remains stable and usable in a changing mobile ecosystem, keep in mind that testing mobile apps is a constant activity. Appium supports your testing efforts in this regard.

In this initial section of the Appium tutorial for Mobile Automation testing, we covered the basics. In the upcoming session, we will explore Advanced actions, Element Identification, and Framework setup with Appium.

I’m hoping this blog article will make Mobile Automation with Appium more clear to you. If you are looking for jobs related to Automation, QA or SDET roles then do checkout jobs with high salary at https://talent500.co/.

Happy Testing! 📲🧪

2+
Sidharth Shukla

Sidharth Shukla

Currently working as a SDET. He is an Automation enabler who provides solutions that mitigates quality risk. Passionate about technical writing and contribution towards QA community.

Add comment