Mobile app testing by Appium :: Part 2

Writing script using POM

Open IntellIJ

If you don’t have IntellIJ, then download the community version from here

Start a new project with Gradle.

You can also start with maven, but here I am showing the process using Gradle. Actually, both Gradle and Maven are dependency package managers. So you can choose anyone you like.

Click on next and give your project name

Click on Finish button

The homepage will look like that:

If your defender makes interruption, allow it automatically.

Now you have to set GRADLE_HOME

You can find the details of setting up Gradle in this tutorial

Now open the build.gradle file

Add following lines under dependencies

compile group: 'org.seleniumhq.selenium', name: 'selenium-java', version: '2.41.0'
testCompile group: 'org.testng', name: 'testng', version: '7.1.0'
compile group: 'io.appium', name: 'java-client', version: '2.2.0'

Then expand the Gradle tab in right sidebar and click on import button

After then, all of your selenium and Appium dependencies will be imported

Now it’s time to start writing codes.

Expand test folder. Under java, create a java class named AppLaunch. This class will be used to setup app desired capabilities and launching the app.

Then write following codes:

import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;


public class AppLaunch {
    public AndroidDriver driver;
    public AndroidDriver setUp() throws InterruptedException, MalformedURLException {
        DesiredCapabilities cap = new DesiredCapabilities();
        cap.setCapability("deviceName", "My Device");
        cap.setCapability("platformName", "Android");
        cap.setCapability("uuid", "672125e00305");
        cap.setCapability("appPackage", "");
        cap.setCapability("appActivity", "");
        cap.setCapability("autoGrantPermissions", true);
        URL url=new URL("");
        driver=new AndroidDriver(url,cap);
        return driver;
    public void closeApp()

Now create another java file named TestCase and write the following codes:

import io.appium.java_client.MobileElement;
import io.appium.java_client.pagefactory.AppiumFieldDecorator;
import org.testng.annotations.AfterTest;


public class TestCase {
    @FindBy(id = "")
    MobileElement btn_8;
    MobileElement btn_5;
    MobileElement btnPlus;
    MobileElement result;
    public TestCase(AndroidDriver driver) throws IOException, InterruptedException
        PageFactory.initElements(new AppiumFieldDecorator(driver), this);
    public String openApp() throws InterruptedException, IOException {;;;
        return result.getText();


Now create another Class named TestRunner and write the following codes:

import org.testng.Assert;
import org.testng.annotations.Test;


public class TestRunner extends AppLaunch {
    public void runTest() throws IOException, InterruptedException {
        TestCase testCase=new TestCase(driver);
        String result= testCase.openApp();


Run the Script

Now click on Add Configuration button on the top and click on “+” button to select TestNG

Give the configuration name as “Run”

Set the following configuration:

Test Kind: Class
Class: TestCase
Use classpath of module: CalculatorApp.test

Now click on play button to run

Make sure, your phone device is unlocked

Now after a moment, you will see that your app has launched and doing calculation automatically for 5 and 7

Run from the command line

Create a file named testng.xml at the root folder

Write the following code here:

<suite thread-count="1" verbose="1" name="Calculator Automation">

    <test name="Calculator automation">
            <class name="TestRunner"/>


Then add this line at the end into file

test {
    useTestNG() {
        suites "./testng.xml"

Now give command from CMD:

gradle clean test

You will get the full project here

about author


If you like my post or If you have any queries, do not hesitate to leave a comment.

Leave a Reply

Your email address will not be published. Required fields are marked *