Java Appium Automation

1.Project Overview

This guide will cover creating a Java Appium automation test suite.

The code generated in this guide is hosted at https://github.com/kirbycope/java-appium

Note: This guide does not use/supply an actual app, you will need to supply your own and change values as needed (Ex., apk_path, element selectors, etc.).

2.Installing the JDK

  1. Navigate to http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html
  2. Accept the license agreement
  3. Select the download that is appropriate for your system (this guide uses Windows 10)
  4. Select “Save”
  5. After the download completes, select “Run”
  6. (If prompted) Select “Yes”
  7. Select “Next >”
  8. Select “Next >”
  9. Select “Next >”
  10. Select “Close”
  11. Verify the install by opening CMD and running java -v

3.Installing the IDE

  1. Navigate to https://www.jetbrains.com/idea/download/#section=windows
  2. Select “Download” for the Community edition
  3. Select “Save”
  4. Select “Run”
  5. (If prompted) Select “Yes”
  6. Select “Next >”
  7. Select “Next >”
  8. Select “64-bit launcher” (optional) and then select “Next >”
  9. Select “Install”
  10. Select “Finish”

4.Installing Android Studio

  1. Navigate to https://developer.android.com/studio/index.html
  2. Select “DOWNLOAD ANDROID STUDIO”
  3. Select the checkbox next to “I have read and agree with the above terms and conditions” and then select “DOWNLOAD ANDROID STUDIO FOR WINDOWS”
  4. Select “Save”
  5. Select “Run”
  6. (If prompted) Select “Yes”
  7. Select “Next >”
  8. Select “Next >”
  9. Select “I Agree”
  10. Select “Next >”
  11. Select “Install”
  12. After the install completes, select “Next >”
  13. Select “Finish”
  14. Select “OK”
  15. Select “Next”
  16. Select “Next”
  17. Select “Finish”
  18. Select “Finish”

4.1.Creating an AVD

This guide uses an AVD (Android Virtual Device) to run tests on.

4.1.1.Create an Android App

We will create a very simple Android app and in doing so, get the tools we need to test.

  1. Select “Start a new Android Studio project”
  2. Select “Next”
  3. Select the last available Minimum SDK and then select “Next”

    • During the installation of Android Studio the latest SDK is downloaded. If you pick an older SDK you’ll have to get those packages from the SDK Mananger.
  4. Select “Next”
  5. Select “Next”
  6. Select “Finish”
  7. (If prompted) Select all check boxes and the select “Allow access”
  8. Select “Close”
  9. Wait for the IDE’s background tasks to finish (Gradle Build and then Index, typically). You will know you are ready for the next steps when you see the following:

4.1.2.Create an AVD

  1. Select “Tools” → “Android” → “AVD Manager”
  2. Select “Create Virtual Device…”
  3. Select “Next”
  4. Select “Next”
  5. Select “Finish”
  6. Select the ▶ button

    • If you have Hypervisor enabled, you’ll have to disable it first.

5.Installing Appium

  1. Navigate to http://appium.io/
  2. Select “Download”
  3. Select “appium-desktop-Setup-1.0.0.exe”
  4. Select “Save”
  5. Select “Run”
  6. Close the application after the installer finishes

5.1.Setting the PATH Variables

  1. Open “File Explorer” (Windows+E)
  2. Right-click on “This PC” and then select “Properties”
  3. Select “Advanced system settings”
  4. Select “Environment Variables…”
  5. Under “System Variables”, select “New…”
  6. Enter ANDROID_HOME for the “Variable name” and C:\Users\{yourUserName}\AppData\Local\Android\sdk\ for the “Variable value” and then select “OK”
  7. Restart the computer

6.Creating the Framework

6.1.Create a Project

  1. Open IntelliJ IDEA
  2. Select “Do not import settings” and then “OK”
  3. Select “Accept”
  4. Select your desired theme (I recommend Darcula) and then select “Skip All and Set Defaults”
  5. Select “Create New Project”
  6. Select “Maven”
  7. (If prompted) Select “Allow Access” on any/all Windows Security Alerts
  8. Select “New…”
  9. Navigate to the root folder of the JDK and then select “OK”
  10. Select “Next”
  11. Enter com.timothycope for the GroupId, example for the ArtifactId, and then select “Next”

  12. Select “Finish”
  13. Select “Close”

    • Wait for the background tasks to complete
  14. Select “Import Changes”

6.2.Modify the Project

  1. Expand the Project tree and delete the “Java” package from the “main” folder
  2. Delete the “resources” file from the “main” folder
  3. In pom.xml (should already be open) and under the <version>, paste in the following:

    • This will add TestNg and Appium libraries to the project.
  4. Click on “Auto-Import” in the prompt that will show after you paste in the code
  5. Under that, paste in the following:

    • This will create a .zip file of your tests when a specific Maven Goal is executed (more on this in the Amazon Device Farm section)
  6. Save and close the pom.xml file/tab
  7. Right-click on “main” (under “src”) and select “New” → “Directory”
  8. Name the directory assembly and then select “OK”
  9. Right-click on the new folder and select “New” → “File”
  10. Name the file zip.xml and then select “OK”
  11. In that file, paste in the following:

    • This will tell the Maven Goal where to save the .zip file (for Amazon Device Farm)
  12. Save and close the zip.xml file/tab

6.3.Add Framework Classes

The framework contains 3 class files; App, AppControl, and TestBase.

6.3.1.The App Class

This class will act as a wrapper of the driver.

  1. Right-click on “java” (under “test”) and then select “New” → “Package”
  2. Name the package Framework and then select “OK”
  3. Right-click on “Framework” and then select “New” → “Java Class”
  4. Name the class App and then select “OK”
  5. In that new class, paste the following:
  6. Select the red text “AndroidDriver” and press [Alt]+[Enter] twice (fixing all issues)
  7. Save and close the App.java file/tab

6.3.2.The AppControl Class

This class will act as a wrapper of an element.

  1. Right-click on the “Framework” package and then select “Add” → “Java Class”
  2. Name the class AppControl and then select “OK”
  3. In the class, paste in the following:

    • Fix the errors (using [Alt]+[Enter]) to include the using directives
      • import org.openqa.selenium.*;
      • import java.util.List;
  4. Save and close the AppControl.java file/tab

6.3.3.The TestBase Class

This class is extended by test classes and limits the amount of code needed for each test class.

  1. Right-click on the “Framework” package and then select “New” > “Java Class”
  2. Name the class TestBase and then select “OK
  3. In the class, paste in the following:

    • Fix the errors (using [Alt]+[Enter]) to include the using directives
      • import io.appium.java_client.MobileElement;
      • import io.appium.java_client.android.AndroidDriver;
      • import org.openqa.selenium.remote.DesiredCapabilities;
      • import org.testng.annotations.*;
      • import java.net.MalformedURLException;
      • import java.net.URL;
      • import java.util.concurrent.TimeUnit;
  4. Save and close the TestBase.java file/tab

7.Creating a Test Suite

A Test Suite is a collection of tests cases intended to test an application. The Test Suite will contain the tests and necessary page object models (POMs).

7.1.Creating a Page Object Model

  1. Right-click on “java” (under “test”) and select “Add” → “Package”
  2. Name the package Pages and then select “OK”
  3. Right-click on “Pages” and then select “New” → “Java Class”
  4. Name the class HomePage and then select “OK”
  5. In the class, add an empty constructor
  6. Below that, add a couple of empty regions for “Page Elements” and “Page Methods”
  7. Save and close the HomePage.java file/tab

7.1.1.Add POM Reference to App

  1. Open the App.java file
  2. In the region “App POMs” add the following:

    • Fix the missing import directive: using Pages.*;
  3. Save and close the App.java file/tab

7.2.Create a Test

  1. Right-click on “java” (under “test”) and select “New” → “Package”
  2. Name the package Tests and then select “OK”

7.2.1.Creating a Test Class

  1. Right-click on “Tests” and then select “New” → “Java Class”
  2. Name the class HomePageTest and then select “OK”
  3. After the class name, add extends TestBase

    • Fix the missing import directive: import Framework.TestBase;
  4. In the class add the following:

    • Fix the missing import directive: import org.testng.annotations.Test;

7.2.2.Writing the Test

Using Appium’s Inspector you can get all the selectors for the current view’s element. To do so, launch your app by creating a new session in Appium. In Appium Desktop the inspector automatically opens once the session has been created. In the older version, you’ll need to click the hourglass button after the session has been created. We will need to add the element we want to interact with to the POM, first. Then we can use it in the test class.

  1. Open “HomePage.java”
  2. In the “Page Elements” region, add public AppControl elementName = new AppControl(By.id("elementID"));

    • Fix the missing import directives:
      1. import Framework.AppControl;
      2. org.openqa.selenium.By;
  3. Swap back to the HomePageTest.java tab
  4. In the test method, add App.HomePage().elementName.click();

    • Fix the missing import directive: import Framework.App;
  5. Below that, add Assert.assertFalse(App.HomePage().elementName.isDisplayed());

    • Fix the missing import directive: import org.testng.Assert;

7.3.Running the Tests

The test created in the previous section will:

  1. Install and start the app via the extended TestBase class’ @BeforeSuite method, setUpAppium()
  2. Restart the app via the extended TestBase class’ @BeforeTest method, restartAppium()
  3. Clicks the elementName AppControl
  4. Asserts that the elementName AppControl is no longer displayed

7.3.1.Run Locally

To run a test locally you will need two things setup first:

  1. Start Appium server
    1. Open Appium Desktop
    2. Select “Start Server vX.X.X
  2. Start Android Virtual Device
    1. Open Android Studio
    2. Select “Tools” → “Android” → “AVD Manager”
    3. Select ▶ (Play) for the AVD you wish to use
      • After the device has booted, you can close Android Studio and the Android Virtual Device Manager window

Now you should be ready to run the test.

  1. Next to the test method click ▶ (Play)
  2. Select “Run ‘test001()'”

    • The project will build and then TestNg will run the selected test
  3. The results will be displayed in the TestNg pane of Intellij

7.3.2.Run on Amazon Device Farm

Your Appium Java TestNG tests must be contained in a .zip file before you upload them to Device Farm. The  necessary code has already been added to the project in previous sections (above).

  1. Open TestBase.java
  2. Comment out the lines of code starting with capabilities.setCapability

    • The capabilities will be defined by Amazon
  3. Select “View” → “Tool Windows” → “Maven Projects”
  4. Select the button for “Execute Maven Goal”
  5. Type clean install -DskipTests=true and then select “Execute”
  6. In the console output look for where the “zip-with-dependencies” was saved

    • This .zip file is the one you’ll upload (along with the .apk) to Device Farm

Use the Device Farm console to upload your tests:

  1. Sign in to the Device Farm console at https://console.aws.amazon.com/devicefarm.
  2. In the list of projects, choose the option next to the project where you want to upload your tests.
    • Tip: If the list of projects is not displayed, then on the secondary navigation bar, for Projects, choose the name of the project where you want to upload your tests.
      To create a new project, follow the instructions in Create a Project.
  3. If the Create a new run button is displayed, then choose it.
  4. On the Choose your application page, choose Upload.
  5. Browse to and choose your Android app file. The file must be an .apk file.
  6. Choose Next step.
  7. On the Configure a test page, choose Appium Java TestNG, and then choose Upload.
  8. Browse to and choose the .zip file that contains your tests. The .zip file must follow the format described in Prepare Your Android Appium Java TestNG Tests.
  9. Choose the Appium version you are using from the Appium version dropdown list.
  10. Choose Next step, and then complete the remaining on-screen instructions to select devices and start the run.
Suggest Edit