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 io.appium.java_client.android.AndroidDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;

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

public class AppLaunch {
    public AndroidDriver driver;
    @BeforeTest
    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", "com.google.android.calculator");
        cap.setCapability("appActivity", "com.android.calculator2.Calculator");
        cap.setCapability("autoGrantPermissions", true);
        URL url=new URL("http://127.0.0.1:4723/wd/hub");
        driver=new AndroidDriver(url,cap);
        Thread.sleep(1000);
        return driver;
    }
    @AfterTest
    public void closeApp()
    {
        driver.quit();
    }
}

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

import io.appium.java_client.MobileElement;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.pagefactory.AppiumFieldDecorator;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
import org.testng.annotations.AfterTest;

import java.io.IOException;

public class TestCase {
    @FindBy(id = "com.google.android.calculator:id/digit_8")
    MobileElement btn_8;
    @FindBy(id="com.google.android.calculator:id/digit_5")
    MobileElement btn_5;
    @FindBy(id="com.google.android.calculator:id/op_add")
    MobileElement btnPlus;
    @FindBy(id="com.google.android.calculator:id/result_preview")
    MobileElement result;
    public TestCase(AndroidDriver driver) throws IOException, InterruptedException
    {
        PageFactory.initElements(new AppiumFieldDecorator(driver), this);
    }
    public String openApp() throws InterruptedException, IOException {
        btn_8.click();
        btnPlus.click();
        btn_5.click();
        return result.getText();
    }

}

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

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

import java.io.IOException;

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

    }
}

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">
        <classes>
            <class name="TestRunner"/>
        </classes>
    </test>

</suite>

Then add this line at the end into gradle.build file

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

Now give command from CMD:

gradle clean test

You will get the full project here

https://github.com/salmansrabon/android-appium-calculator-app

about author

admin

salmansrabon@gmail.com

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 *