Skip to main content

Mobile Test Automation using Appium and Android studio

In this article we will discuss about how to automate Mobile application using Appium Server and Android Studio.
Appium supports both iOS and Android. Appium is HTTP server. It receives the commands from client, executes the command on Mobile device and send a HTTP response representing the result of the command execution.

I am going to use Android studio to create a virtual emulator. And Selenium WebDriver will be used to write the Client. I would prefer to use the virtual emulator instead of using real device. In actual devices you may find difficulties to connect/detect it.

Pre-requisites

  • .apk file is required to install in Mobile device (Virtual emulator).
  • Android Studio, Appium desktop server and intelliJ IDEA have to be installed.

Virtual emulator

This post guides you to create the Virtual emulator using Android Studio.
Once you launch the emulator now It's time to install the mobile application which is going to be automated. Drag and drop the apk file and do the installation in the Mobile device.

Appium Server

Once the installation is done, Server can be started with the default Host and Port. If you want to make any changes, click the Advance button. Here I am going to use the default Host and Port.
Appium Server successfully started on port 4723.
Now we have to connect the Appium server and the Virtual emulator. Click on Start Inspector Session to provide the Emulator/ App information.
You can provide the details as key value pair and save it for future use, Which saves as JSON file. Once the information given you can start the session to inspect the Mobile app elements.

  • Device name can be found in Android Studio virtual emulator.
          Android Studio -> Tools -> AVD manager 
  • To find udid type 'adb devices' in your terminal.
  • Platform name and Platform version can be found in emulator info.
         Android Studio -> Tools -> AVD manager -> Select Virtual emulator -> Click on edit button
  • To get the information regarding appPackage and appActivity install APK application in your emulator. 
    • Open the APK app and select the mobile app which you have been installed. Under Activities you can find the details. This may differ application to application.

Client code

We are going to use intelliJ IDEA as the developing environment. And make sure you are creating a Maven Project So the relevant libraries can be imported as Maven dependencies in POM file. We will use Selenium WebDriver, Java, Appium and TestNG.


Once you imported the maven dependencies your pom file will look like this.
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>appiumAndroidProject1</groupId>
    <artifactId>appiumAndroidProject1</artifactId>
    <version>1.0-SNAPSHOT</version>
    <dependencies>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-support</artifactId>
            <version>3.14.0</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>io.appium</groupId>
            <artifactId>java-client</artifactId>
            <version>7.0.0</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>6.14.3</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>

Create a Java Class by giving a meaningful name, To get connect with the server and emulator we have to provide the information regarding the Virtual emulator and the Appium Server port number in Client code.

I have used @BeforeMethod annotation since I want to create the connection between the server and client before execute the command.

Elements have been found from the Appium Inspect element.
import io.appium.java_client.MobileElement;
import io.appium.java_client.android.AndroidDriver;
import org.openqa.selenium.By;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

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

public class Test001 {

    public AndroidDriver<MobileElement> driver;
    public WebDriverWait wait;

    @BeforeMethod
    public void setup() throws MalformedURLException {
        DesiredCapabilities caps = new DesiredCapabilities();
        caps.setCapability("deviceName", "Nexus 5X API 25");
        caps.setCapability("udid", "emulator-5554");
        caps.setCapability("platformName", "Android");
        caps.setCapability("platformVersion", "7.1.1");
        caps.setCapability("skipUnlock", "true");
        caps.setCapability("appPackage", "com.mock.app");
        caps.setCapability("appActivity", "com.mock.app.ui.activities.DomainActivity");
        caps.setCapability("noReset", "false");
        driver = new AndroidDriver<MobileElement>(new URL("http://0.0.0.0:4723/wd/hub"), caps
            );
        wait = new WebDriverWait(driver, 10);
    }

    @Test
    public void login() throws InterruptedException {
        driver.findElement(By.id("com.mock.app:id/txt_companyname")).sendKeys("comp");
        driver.navigate().back();
        driver.findElement(By.id("com.mock.app:id/btn_domain")).click();

        wait.until(ExpectedConditions.visibilityOfElementLocated
                (By.id("com.mock.app:id/username"))).sendKeys("user");
        driver.findElementById("com.mock.app:id/password").sendKeys("password");
        driver.findElementById("com.mock.app:id/login").click();
        String title = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("com.mock.app:id/mainTitle"))).getText();
        Assert.assertEquals(title, "title");
        System.out.println("Successfully logged in to the system!");

    }
    @AfterMethod
    public void teardown(){
        driver.quit();
    }
}
When you run the test case it automatically opens the app in you emulator and start execute the commands which you have written in @Test.

Comments

Popular posts from this blog

Katalon studio Execution Profiles

Hi Folks, I want to share my experience regarding the learning, My project manager asked me to find the best way to automate the web application for the regression/retest purpose. So I had a research on the tools and technologies regarding automation. So I found this :) In this post I would like to share my knowledge on Katalon Studio Profiles.  Let's have a quick idea regarding Katalon studio. Katalon studio is a free automation testing tool and supports the web and mobile environment, and built on top of the open-source automation frameworks Selenium, Appium with a specialized IDE. Pre requests - You can download Katalon studio from this link. I am using the latest version in order to avoid unnecessary errors. First, I will show how to record the Web application via Katalon studio. Find below mentioned test scenarios which are going to be recorded Launch the application (URL : http://demoaut.katalon.com/ ) Make an Appointment Login using provided credentials...

Data Driven Protractor Testing

In this article I would like to share knowledge regarding Data driven testing. When using a Data driven automation test framework, we do not need to hard code the test data. Instead it enables to access the test data from a separate data file and use them in the automated test cases. First, let's take a look at a simple Protractor script without Data Driven framework describe('Angular home page', function () {       it('should add the String in todo list ', function () {          browser.get('https://angularjs.org/');          element.all(by.css("[placeholder='add new todo here']")).sendKeys("Add POM");          element(by.css('input.btn-primary')).click();          var getText = element.all(by.css('label.checkbox'));          getText.get(2).getText().then(function (results) {          expect(results).toEqua...

Protractor with Gitlab and Jenkins

Hi everyone.. welcome to the first article in my new series of articles on Automation Testing. For a change, let's start from the End. Let's assume you have written your automation code in protractor and your project is ready to be pushed in to the repository. So let's start with a brief introduction on how to push our automation code to a Git repository. In this lesson I will be using GitLab repository. First things first.. You can't just push the code to a repository. Yeah.... you have to install Gitlab and make sure you have configured it properly. Try following command to make sure Git have been installed in your machine. git --version Hope Git have been installed in your machine, Let's start with GitLab configuration. Sign up with GitLab account Go to GitLab account and get register with your email account To identify you as the author configure your Git username and email address in your machine git config -- global user.name git config -- g...