Automate Android App in Appium with Data-Driven Framework

Reading Time: 4 minutes

Hi friends,

Welcome to our appium blog series,

In the last blog, we know about the desired capability which helps a lot in an appium to test whatever android or iOS to know more about the problems related to appium and also for better understanding of Desired Capabilities which are essential for mobile testing so please go through our previous blogs from here but this time we focused on something advanced in appium related to android only because as we know for testing iOS we required a real iOS device so we now choose the android machine but in future, we also try for iOS device.

Initially, In this short blog, we learn about “how we automate android app with DDT“, so as we discussed earlier, Appium is an automation testing tool used for mobile testing as well as it helps to automate tests with the help of a selenium web driver. It also required a connection between mobile and Appium server that things which help to make a proper connection between are known as “Desire Capabilities.” you also get some advanced knowledge how appium work with DDT so first discussed what is DDT framework.

Data-Driven framework(DDT)

Basically, Data-Driven Testing is a software testing method in which test data is stored in table or spreadsheet format. It allows testers to input a single test script that can execute tests for all test data from a table and expect the test output in the same table. so set up a data source and add an excel(.xlsx) file with input into it then call into the main function like this,

ReadData excel = new ReadData("TestDataFile/datadriven.xlsx");

Set up environment with Appium and IntelliJ

As we discussed earlier we need a proper setting for testing in my last blog, you can get references for more here

For proper environment set-up, follow the below steps:-

  1. Install appium server (GUI or command line).
  2. Install nodeJs.
  3. Set android home and java home path
  4. Install android studio.
  5. Download android version and android emulator from android studio.
  6. Install IDE (IntelliJ)
  7. Download and install apk info.apk in emulator or real device.
  8. Download UI automator viewer.

Setup Your Project

Launch IntelliJ, select “File” > “New” > “Maven Project” and follow through the dialogs with the default settings. Next, let’s add the appium and selenium dependency. Dual click on the pom.xml file and edit the <dependencies>…</dependencies> block to look like this:

<dependencies>
  <dependency>
    <groupId>io.appium</groupId>
    <artifactId>java-client</artifactId>
    <version>5.0.4</version>
  </dependency>
  <dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-java</artifactId>
    <version>3.9.1</version>
  </dependency>

Write A Basic Setup Script

In the IntelliJ file explorer, extend “src/main/java” and extend the package.

public class demo1 {
    public static void main(String[] args) throws MalformedURLException {
        DesiredCapabilities  desiredCapabilities = new DesiredCapabilities();
        desiredCapabilities.setCapability("platformName", "Android");
        desiredCapabilities.setCapability("appium:platformVersion", "11.0");
        desiredCapabilities.setCapability("appium:deviceName","4.7_WXGA_API_30");
        desiredCapabilities.setCapability("appium:automationName", "appium");
        desiredCapabilities.setCapability("appium:noRest", true);
        desiredCapabilities.setCapability("appium:udid", "emulator-5554");

As we discuss above we automate an android application so every app has a different appActivity and appPackage, we know both with the help of the “app info” so First installed android application installed in our emulator.

         cap.setCapability("appPackage","com.android.dialer");
        cap.setCapability("appActivity","com.android.dialer.main.impl.MainActivity - Phone");

How to capture main desired capability

After adding this Desire Capability appium can be lunch the special app in an emulator or real device but before that, we need to add localhost port of appium and also add appium-selenium Android driver into code.

         //localhost where appium server run
        URL url= new URL("http://127.0.0.1:4723/wd/hub");
        AndroidDriver<WebElement> driver=new AndroidDriver<WebElement>(url,cap);
        //after lunching the application it wait so we add below function.
                 Thread.sleep(5000);

How to access the button and how we capture it

Half the steps are done towards the automaton and also now accessibility ID is required to locate the element in the emulator or real Android device.

I choose a phone dialer as an android application for our testing. so we need to access and also capture the buttons in our application, so let’s add the resource ID or sometimes accessibility ID of a particular button in the .xlsx file as an external data source. to get resource Id or some other different ID of a particular button we follow certain steps given in our Gif file with the help of one of the following.

  • Appium Inspector
  • UI Automator viewer
          MobileElement el8 = (MobileElement) driver.findElementByAccessibilityId(excel.getData(0,0,1));
        el8.click();
        MobileElement el9 = (MobileElement) driver.findElementByAccessibilityId(excel.getData(0,1,0));
        el9.click();
        MobileElement el10 = (MobileElement) driver.findElementByAccessibilityId(excel.getData(0,1,1));
        el10.click();
        MobileElement el11 = (MobileElement) driver.findElementByAccessibilityId(excel.getData(0,2,0));
        el11.click();
        MobileElement el12 = (MobileElement) driver.findElementByAccessibilityId(excel.getData(0,2,1));
        el12.click();
         System.out.println("Application started........");

Pre-requisties

Before running the above program we need the following things in running state.

  • Appium server
  • emulator or real andriod device

That’s all for this blog, I Hope so learned about appium and how we automate an android device, and in the next blog, we will learn more about appium so stay tuned.

To know more about android automation and get the proper code of data-driven testing using appium please go through our tech-hub link: https://github.com/knoldus/Appium-android-application-automation/tree/mybranch/appium

Thank you!!  

References

https://appium.io/

https://appium.io/docs/en/writing-running-appium/caps/

https://www.browserstack.com/guide/desired-capabilities-in-appium

knoldus

Written by 

I am a Software Consultant in knoldus software limited, I have 6 months past experience and I am a quick learner, self-motivated, and etc