Mobile Test Automation In Java With Appium- Part 2

Mobile Test Automation In Java With Appium- Part 2

2019 / 9 / 5

Load Testing, Performance Testing, Best Practices

    If you have completed all the steps successfully in the first step, then your environment should be ready to write, compile and run test cases. In this blog post, we will be guiding you through the following steps:

  1. Setting up the project
  2. Writing a basic test case
  3. Running your basic tests on real devices

So, Let’s get started and start with step one and create a new project.

Setting up the project:

  1. Launch Eclipse. Create a New Maven Project by Clicking on File->New->Maven Project. If Maven Project option is not available in the list, you can find this option by clicking on the others option in the list.

Eclipse IDE

Maven Project in Eclipse

Maven Project in Eclipse

  1. On clicking next, you can find 2 fields Group Id and Artifact Id. Group Id is an identifier that uniquely identifies your project among all other projects in the workspace and Artifact Id is the name of the jar file without version. I have set Group Id as com.frugaltesting.appium and Artifact Id as sampleproject (for Artifact Id I am setting the name of the project).
  2. Next step is to add the required JAR Files to the project. Double click pom.xml file and edit the block.
    <dependencies>
      <dependency>
        <groupId>
          io.appium
        </groupId>
        <artifactId>
          java-client
        </artifactId>
        <version>
          5.0.4
        </version>
      </dependency>
      <dependency>
        <groupId>
          org.seleniumhq.selenium
        </groupId>
        <artifactId>
          selenium-java
        </artifactId>
        <version>
          3.9.1
        </version>
      </dependency>
      <dependency>
        <groupId>
          org.apache.commons
        </groupId>
        <artifactId>
          commons-lang3
        </artifactId>
        <version>
          3.7
        </version>
      </dependency>
      <dependency>
        <groupId>
          org.testng
        </groupId>
        <artifactId>
          testng
        </artifactId>
        <version>
          6.14.3
        </version>
        <scope>
          test
        </scope>
      </dependency>
    </dependencies>
  1. In order to download the dependencies, you need to update the project. Right Click on the project-> Maven-> Update Project.
  2. This will start downloading the dependencies as mentioned in the pom.xml file. Upon successful completion you will be able to see a lot more .jar files in the Maven Dependencies dropdown.

Maven Dependencies

This defines dependencies for Maven to download in this Project.

Writing a basic Test Case:

  1. Open the .java file under the src directory. Expand the src directory, you will see the package org.frugaltesting.appium, under which you will find the .java file TestApp.java.
  2. Copy Paste the following code:
    package org.frugaltesting.appium;
    import java.net.MalformedURLException;
    import java.net.URL;
    import java.util.concurrent.TimeUnit;
    import org.openqa.selenium.remote.DesiredCapabilities;
    import org.testng.annotations.BeforeSuite;
    import org.testng.annotations.AfterSuite;
    import org.testng.annotations.BeforeTest;
    import org.testng.annotations.Test;
    import io.appium.java_client.MobileElement;
    import io.appium.java_client.TouchAction;
    import io.appium.java_client.android.AndroidDriver;
    import io.appium.java_client.remote.MobileCapabilityType;
    public class TestApp{
      public static URL url;
      public static DesiredCapabilities capabilities;
      public static AndroidDriver<MobileElement> driver;
      //Step 1
      @BeforeSuite
      public void setupAppium() throws MalformedURLException {
        //Step 2
        final String URL_STRING = "http://0.0.0.0:4723/wd/hub";
        url = new URL(URL_STRING);
      //Step 3
        capabilities = new DesiredCapabilities();
          capabilities.setCapability(MobileCapabilityType.DEVICE_NAME, "Android Device");
        capabilities.setCapability(MobileCapabilityType.APP, "https://github.com/afollestad/material-dialogs/raw/master/sample/sample.apk");
          capabilities.setCapability(MobileCapabilityType.NO_RESET, true);
          capabilities.setCapability(MobileCapabilityType.AUTOMATION_NAME, "UiAutomator2");
        //Step 4
        driver = new AndroidDriver<MobileElement>(url, capabilities);
          driver.manage().timeouts().implicitlyWait(2, TimeUnit.SECONDS);
        driver.resetApp();
      }
      //Step 5
      @AfterSuite
      public void uninstallApp() throws InterruptedException {
        driver.removeApp("com.example.android.contactmanager");
      }
      //Step 6
      @Test (enabled=true) public void myFirstTest() throws InterruptedException {
        driver.resetApp();
      }
    }

Let’s understand this code, so that you can edit it as per your software attributes.

  1. Step 1: @BeforeSuite functions will be executed before every test suite run. Such a function can be used to setup the environment before the individual test cases run.
  2. Step 2: sets the URL of the Localhost, where Appium will be running. For my case it is 0.0.0.0 and Port No. is 4723.

NOTE: You can find the Host and Port by Launching the Appium. You will see the Host and Port on the main screen.

Appium

  1. Step 3: This is the most important step. As in this step, we will be creating the object to hold the Appium capabilities.
  2. Step 4: Here, we are initializing the driver object with url and capabilities object. This driver will be the main hero who will be interacting with device.
  3. Step 5: functions with @AfterSuite annotation will be executed after every test suite run. This will help to do the cleanup task before any new test suite run.
  4. Step 6: Function with @Test will be identified as TestNG Test. You can enable or disable the Test by Toggling the (enabled=True) value.

Running your basic tests on real devices

  1. Connect your Android device to the computer. You will need to enable the USB Debugging on your Android device.
  2. Launch Command Prompt.
  3. Type the following command
    adb devices
  4. You will see the list of devices attached to the computer. In this case, I have only one device attached, you will see only one device in the list of adb devices.
  5. Now start the Appium by typing the command in the command prompt
    appium
  1. Return to the Eclipse Project. Right Click on the .java file->Run As->1 TestNG Test.

Run Test in Eclipse

  1. You will see a successful test run with no errors in the console. This test case is basically installing and uninstalling the application in the device. So, you will be seeing the flickering in the device. With this we are done with installation and running a first test case.

You might also like