2019 / SEPTEMBER / 5


Appium TestNG API Mobile TestingAutomation Testing

How to setup Appium for Mobile Automation


How to setup Appium for Mobile Automation

Appium is an open-source test automation tool which allows you to write functional tests to automate iOS and Android mobile applications. One big advantage of Appium over other test automation tools is that Appium tests can be written in any language that has Selenium Client Library including Python, Ruby, Node.js.

Appium supports native, hybrid and web application testing and also supports testing on emulators, simulators and real devices.

It supports cross-platform application testing i.e. single API works for both Android and iOS platform test scripts.

    Part 1: Getting environment setup with Appium and TestNG.

    Part 2: Writing your Test Cases.

The most difficult part of getting automated mobile testing, is to determine the correct set of tools to use, which version to use and how to install everything correctly so that it works seamlessly together. Sadly, it is not that easy, not a job of just a few clicks to install everything you need.
In this tutorial, we have discussed the recommended tools, that we will be using to show how to automate an Android app. But, there are plenty of other variations also. Below is a comprehensive list of all the tools, which we will be discussing one by one in detail.

  1. Chocolatey
  2. Node & NPM
  3. JDK
  4. Android Studio and Android SDK
  5. Appium
  6. Appium Doctor
  7. Gradle
  8. Maven
  9. Eclipse
  10. TestNG

Steps to install Chocolatey:
Chocolatey is simply a package management software that makes it easier for us to install and manage other software.

  1. Go to the link https://chocolatey.org/install#installing-chocolatey.
  2. Open command prompt or powershell using administrative privileges.
  3. From the start menu search command to open a command prompt and search power to open powershell.
  4. For both the cases right click and select “Run as administrator” option.

Installing Chocolatey

Installing Chocolatey

  1. Click on “Yes” button in the dialog that appears.

  2. In case of command prompt enter the following command to install chocolatey

    @"%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe" -NoProfile -InputFormat None -ExecutionPolicy Bypass -Command "iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))" && SET "PATH=%PATH%;%ALLUSERSPROFILE%\chocolatey\bin" 
  3. In case of powershell enter following command to install chocolatey

    Set-ExecutionPolicy Bypass -Scope Process -Force; iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))

Steps to Install Node and NPM:
Node is a javascript run-time environment and npm is the node package manager. And why we need these? Because Appium is a node application.

  1. Use chocolatey to install Node. NPM comes installed with Node. SO, you don’t need to install it separately.

  2. Use command:

    choco install nodejs.install

Installing NodeJS

Steps to install JDK and set JAVA_HOME:
Since our tests will be written using Java language, so we will be requiring JDK- Java Development Kit.

  1. Go to the link https://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html and Download JDK.
  2. Double click the JDK and follow the installation steps.
  3. Next step is to setup the Path variable in System Variables.
  4. Set Path variable to the path of bin folder in jdk directory like “C:\Program Files\Java\jdk-12.0.2\bin”
    NOTE: Replace this with path of bin folder in your jdk directory. This is for illustration purpose only.
  5. Open a command prompt and type java. If you can see some output, it means the path is set correctly and now jdk ready for use.

JAVA jdk

Steps to install Android Studio and Android SDK:
We need Android Studio to write and run tests on Android Apps and Devices.

  1. Go to the link https://developer.android.com/studio/ and Download Android Studio.
  2. Launch Android Studio and complete the setup process until you reach the welcome page.
  3. You can find the SDK Manager icon from the top rightmost menu.

Android Studio

  1. From the “SDK Tools”, ensure following options are selected:
    a. Android SDK Build-Tools
    b. Android SDK Platform-Tools
    c. Android SDK Tools
    d. Intel x86 Emulator Accelerator (HAXM installer)
    e. Android Support Repository
    f. Google Repository

  2. Click “OK” and install if there are any additions.

  3. Edit the Path Variable under the System Variables to add path of Sdk folder under Android directory “C:\Users\Rashi\AppData\Local\Android\Sdk\”

Steps to install Appium:

Appium is an open-source test automation framework and the hero of today’s tutorial.

  1. Open the Node.js command prompt from the start menu.

  2. Type the following command in the terminal:

    npm install -g [email protected]
  3. Successful installation of appium can be checked by entering the command- appium in command prompt. If no error message is displayed, it means appium is successfully installed.

Steps to install Appium Doctor:

Appium Doctor is the mini software, whose job is to check whether all of the preconditions for the execution of appium are true or not.

  1. Open the Node.js command prompt.

  2. Type the following command in the terminal

        npm install -g appium-doctor

    Installing Appium

  3. To check the successful install, type the command: appium-doctor in the terminal.

  4. The output will be like this:

Appium Doctor

Steps to install Gradle:
Gradle is used to create apk and jar files. It is an open-source build automation system.

  1. Open the command prompt.

  2. Type the following command:

    choco install gradle

Installing Gradle

  1. This command will automatically edit the Path variable under Environment Variable to add Gradle Path.

Steps to install Maven:
Maven is a tool used to build and manage Java based projects.

  1. Open the command prompt.

  2. Type the following command:

    choco install maven

Installing Maven

  1. This command will automatically edit the Path variable under Environment variable to add path of Maven.

Steps to install Eclipse:
Eclipse is an Integrated Development Environment (IDE). We are going to use eclipse to write, compile and run test scripts.

  1. Go to the link https://www.eclipse.org/downloads/packages/ and Download the Eclipse.
  2. Double click the downloaded file and follow the steps to install eclipse.

Steps to install TestNG:

  1. Open the Eclipse.
  2. Go to the Help option in the top main horizontal menu.
  3. Select Install New Software from the list.
  4. Paste “http://beust.com/eclipse” in the “Work with” field and press enter on the keyboard.
  5. Eclipse will show the name of the relevant package.
  6. Select the checkbox next to “TestNG” and click on Next.

TestNG

  1. Eclipse will take a while to download and install TestNG. You will see a prompt to restart the Eclipse.
  2. Restart Eclipse and now you are ready for the actual coding.

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.

Share:


Recommended Posts