Tuesday, August 27, 2019

How to automate Native Android app using Appium | Mobile Automation




In this post,  we are going automate Native Android app using Appium. I’m  going to take an example of the Android calculator app and i will take a look at the addition of two numbers:








Perform the following steps to automate the calculator app:

1. First create maven project:

If you are new to Maven, Follow this post “How to create Maven project for automation


2. Add the following Appium & TestNg Maven dependency in pom.xml file:

TestNg Dependency:
                        <dependency>
                                    <groupId>org.testng</groupId>
                                    <artifactId>testng</artifactId>
                                    <version>6.9.13.6</version>
                        </dependency>

Appium dependency:

                        <dependency>
                                    <groupId>io.appium</groupId>
                                    <artifactId>java-client</artifactId>
                                    <version>7.0.0</version>
                        </dependency>

3. Update the desired capabilities in the setup() method to launch the calculator app:

    cap.setCapability("deviceName", "emulator-5554");
    cap.setCapability("udid", "emulator-5554");
    cap.setCapability("appActivity", "com.android.calculator2.Calculator");
    cap.setCapability("appPackage", "com.android.calculator2");
    cap.setCapability("platformName", "Android");
    cap.setCapability("platformVersion", "9.0");

4. Now, we need to find the numbers; we are going to find them by id:


MobileElement oneBtn=driver.findElement(By.id("com.android.calculator2:id/digit_1"));




MobileElement fiveBtn=driver.findElement(By.id("com.android.calculator2:id/digit_5"));

5. We need to find the + sign and the = sign; we are going to find them by id:




MobileElement plusBtn=driver.findElement(By.id("com.android.calculator2:id/op_add"));




MobileElement equalToBtn=driver.findElement(By.id("com.android.calculator2:id/eq"));

6. Now, we need to perform click on the element:

oneBtn.click();
plusBtn.click();
fiveBtn.click();
equalToBtn.click();

Complete code should look like the following block of code:

package practise;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import io.appium.java_client.AppiumDriver;
import io.appium.java_client.MobileElement;

public class AndroidNativeApp {
           
            AppiumDriver<MobileElement> driver;
           
            @BeforeClass
            public void setUp() throws MalformedURLException
            {
                DesiredCapabilities cap=new DesiredCapabilities();
                cap.setCapability("deviceName", "emulator-5554");
                cap.setCapability("udid", "emulator-5554");
                cap.setCapability("appActivity", "com.android.calculator2.Calculator");
                cap.setCapability("appPackage", "com.android.calculator2");
                cap.setCapability("platformName", "Android");
                cap.setCapability("platformVersion", "9.0");
                driver=new AppiumDriver<MobileElement>(new URL("http://0.0.0.0:4723/wd/hub"), cap);
                driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
            }
           
            @Test
            public void testExample(){
                       
                        MobileElement oneBtn=driver.findElement(By.id("com.android.calculator2:id/digit_1"));
                        oneBtn.click();
                        MobileElement plusBtn=driver.findElement(By.id("com.android.calculator2:id/op_add"));
                        plusBtn.click();
                        MobileElement fiveBtn=driver.findElement(By.id("com.android.calculator2:id/digit_5"));
                        fiveBtn.click();
                        MobileElement equalToBtn=driver.findElement(By.id("com.android.calculator2:id/eq"));
                        equalToBtn.click();
           
            }

            @AfterClass
            public void tearDown(){
                        driver.quit();
            }
}


7. Run your script using TestNG:


Console Output:




TestNg Output:









No comments:

Post a Comment

If any suggestions or issue, please provide