Appium From Scratch – Preparing for Automation – Part 3: Writing Your First Test

In this post we are going to build on everything that we have learnt so far about Appium to write out first automated test in our IDE. Before continuing with this post, you should have followed everything in Part 1 and Part 2 first. For this example we will be using the Intellij IDE, if you need some help getting that setup and prepared for automation then do check out my series of posts on that subject here.

Let’s get started right away by launching Intellij and clicking “Create New Project“. Choose to create a Maven project and click Next. Type anything you like for the GroupID and ArtifactID, click Next, give the project a name and set the project folder location, and finally click Finish.

When the IDE loads, remember to tick “Enable Auto Import” in the top right hand popup. You should find that your pom.xml file has loaded right away, if it hasn’t find it in the directory structure on the left hand side and open it. Add the following code to import the dependencies for Appium Java, Selenium and JUnit:

<?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>anything</groupId>
    <artifactId>anything</artifactId>
    <version>1.0-SNAPSHOT</version>

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

        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>2.52.0</version>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
        </dependency>
    </dependencies>

</project>

Now in your directory structure on the left, expand the SRC > TEST folders, right click on the JAVA folder and select NEW > JAVA CLASS . Name the class “MyFirstTest”. Add the following code to setup Appium:

import io.appium.java_client.AppiumDriver;
import io.appium.java_client.android.AndroidDriver;
import org.junit.Before;
import org.openqa.selenium.remote.DesiredCapabilities;
import java.util.concurrent.TimeUnit;

public class MyFirstTest {

    AppiumDriver driver;

    @Before
    public void setupTest() {
        DesiredCapabilities capabilities = new DesiredCapabilities();
        capabilities.setCapability("platormName", "Android");
        capabilities.setCapability("deviceName", "Emulator");
        capabilities.setCapability("app", "C:/APKs/Calculator_v1.2.3_apkpure.com.apk");

        driver = new AndroidDriver(capabilities);
        driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
    }
}

You should be able to leave everything as above, but be sure to change the directory path for your app to wherever it is on your machine. Now add the following test code to the class:

@Test
public void myTest() {
    // Click on number 7
    WebElement sevenKey = driver.findElement(By.xpath("//android.widget.Button[contains(@resource-id,'button7')]"));
    sevenKey.click();

    // Click on '+'
    WebElement plusKey = driver.findElement(By.xpath("//android.widget.Button[contains(@resource-id,'buttonPlus')]"));
    plusKey.click();

    // Click on number 5
    WebElement fiveKey = driver.findElement(By.xpath("//android.widget.Button[contains(@resource-id,'button5')]"));
    fiveKey.click();

    // Click on '='
    WebElement equalsKey = driver.findElement(By.xpath("//android.widget.Button[contains(@resource-id,'buttonEquals')]"));
    equalsKey.click();

    // Check the total is correct
    WebElement total = driver.findElement(By.xpath("//android.widget.EditText[contains(@resource-id,'textField')]"));
    String totalValue = total.getText();
    Assert.assertTrue(totalValue.equals("12"));
}

This is a really simple test that will add the numbers 7 and 5, and check that the result is 12. OK, we are almost ready to run the test, we just need to make sure that the Appium server is running and that we have launched an AVD.

Open a command prompt and start the Appium server by typing:

appium &

Open another command prompt and start your AVD by typing

emulator @training01

Where ‘training01‘ is the name of your AVD that you created previously. Once the AVD has loaded, simply run the test through Intellij by right clicking on the myTest() class and selecting ‘Run myTest()’. The calculator app will load on the AVD and the test will execute according to the code that you specified

Congratulations! You just ran your first automated test in Appium. Obviously this test is incredibly straightforward, but hopefully this tutorial has done enough to show you how to get Appium installed and running on your machine and how you can leverage your existing Selenium automation knowledge for testing on mobile.

  • Bran

    Great blog series, keep up the awesome work!

  • Darsh Shah

    James- Thanks for a great post. I copied and tried to run the exact code as mentioned. I also followed your setup series so that i have everything in place, however getting the following error when i try to run it.

    Information:java: Errors occurred while compiling module ‘MyFirstTest’

    Information:javac 1.8.0_101 was used to compile java sources

    Information:1/5/17, 5:08 PM – Compilation completed with 12 errors and 3 warnings in 2s 973ms

    Warning:java: source value 1.5 is obsolete and will be removed in a future release

    Warning:java: target value 1.5 is obsolete and will be removed in a future release

    Warning:java: To suppress warnings about obsolete options, use -Xlint:-options.

    Error:(29, 12) java: class, interface, or enum expected

    Error:(32, 9) java: class, interface, or enum expected

    Error:(34, 9) java: class, interface, or enum expected

    Error:(35, 9) java: class, interface, or enum expected

    Error:(37, 9) java: class, interface, or enum expected

    Error:(38, 9) java: class, interface, or enum expected

    Error:(40, 9) java: class, interface, or enum expected

    Error:(41, 9) java: class, interface, or enum expected

    Error:(43, 9) java: class, interface, or enum expected

    Error:(44, 9) java: class, interface, or enum expected

    Error:(45, 9) java: class, interface, or enum expected

    Error:(46, 5) java: class, interface, or enum expected