How To Get Started With Appium 2.0?

Mobile application testing is a crucial aspect of software development to ensure that applications function seamlessly across various devices and platforms. In this post, we will learn how to get started with Appium 2.0.

What is Appium?

Appium is a popular, open-source and free-to-use tool that offers automation for different platforms like Android, IOS, Windows Desktop, etc. Multiple programming languages, including Java, PHP, Perl, Python, etc are supported by Appium. Therefore, users can create automated scripts using whatever programming language they are familiar with. It can automate Hybrid apps as well along with Native and web apps.

What is Appium 2.0?

Appium 2.0 is the latest version of Appium. That works on the W3C web driver protocol. Earlier Appium APIs used JSON Wire protocol. Please visit this article to understand How to migrate to Appium 2.0.

How Does Appium Work?

Appium follows a client-server architecture, where the server is written in node.js.Appium workflow behaves like below mentioned steps:

1)Post requests or session requests are sent to the server by the client.

2)The server listens to the request and based on the provided information in the desired capabilities argument differentiates among the AndroidRequest, IOSRequest, and DesktopRequest.

3)The request is then further processed by vendor-provided frameworks like UIAutomator2 in the case of Android, XCUI Test in the case of IOS, and then executes commands on the attached device.

5)The JSON Wire Protocol is then used to transmit the test session’s results to the server and then back to the client machine as logs.

Tools Required

1)Install and confiugre JDK

2)Install and confiugre Maven

3)Install and configure Node.Js

4)Install Appium 2.0

5)Install and configure Android SDK

6)Setup Virtual device/Real device

7)IDE (Intellij/Eclipse)

Appium 2.0 Installation

In this section, we will learn how to install Appium 2.0 on a Mac machine.

1)JDK and Maven Setup

Download JDK and Maven and set the path in ~/.zshenv

nano ~/.zshenv
export MAVEN_HOME=~/apache-maven-3.8.8
export PATH=$PATH:$MAVEN_HOME/bin
export JAVA_HOME=$(/usr/libexec/java_home)

2)Node.Js Installation

Go To Terminal and install node.js using Brew

brew install node

Appium setup on Mac – iOS and Android

If the node is already installed on your machine and you want to upgrade the version.

 brew upgrade node

Check the path of node installation and version

node -v
where node

Check npm version

npm -v

node and npm bundles together.

3)Appium 2.0 installation

Run the below command in the terminal using npm.

npm i -g appium@next

Add the required driver.

appium driver install xcuitest

Command to check Appium installation path:

 where appium

4)Appium 2.0 Inspector

In Earlier versions, Appium inspector used to come bundled with the Appium itself. From Appium 2 we can use the web version of Appium Inspector.

5)Android SDK installation

a)Go to https://developer.android.com/studio
b)Download and complete the configuration
c)Set Android_Home

To setup Android_home in MAC OS open the terminal and type

nano ~/.zshrc

and add the following commands at the end.

Android_Home Setup in Mac

Hit ctrl+x and Type Y to save it.

6)Virtual device setup

The next step is to set up the virtual device or emulator. Follow the below steps for creating a virtual device.

  1. Open the Android Studio and click on the More Actions dropdown.
  2. Select the Virtual Device Manager option.
  3. Click Create Device.
  4. Select a Device.
  5. Click Finish.
  6. Select the Android version for the device and download it.
  7. The virtual device setup is complete.

Type “adb devices” on the terminal to check the List of devices attached. It should show the list of devices we have created.

7)Real device set-up

If you want to use a real device for mobile automation then follow the below-mentioned steps:

1)On your phone, Go to Settings then Click the System option
2)Click the “About Phone” option
3)Click on “Build Number” 7 to 8 times
4)Go back to Settings
5)Open Developer Options
6)Enable “USB Debugging”

Execute “adb devices” to check if the device is connected and available or not.

8)Identify the app activity and app package

The next step is to identify the app package and app activity of the application on which we will work. To do that first launch the device from the device manager section, install the desired app on the device and open the application.

Next, go to the terminal and execute the below-mentioned command. This command will grep the activity of the currently focused application on the attached device.

adb shell
dumpsys window displays | grep -E 'mCurrentFocus'

How to use Appium Web Inspector?

Open the Appium web inspector and add the desired capabilities.

{
  "appium:platformName": "Android",
  "appium:automationName": "UIAutomator2",
  "appium:appPackage": "com.swaglabsmobileapp",
  "appium:appActivity": "com.swaglabsmobileapp.MainActivity"
}

Before clicking on the Start session button go to the terminal and start the Appium server using the below command.

appium --allow-cors

Now click on Start session. A new session will start, by using which we can inspect mobile elements and write an automation script.

Appium 2.0 Web inspector

If using appium: app to install during the inspection, then it’s suggested to use appium: no reset so it won’t install again if installed. If you have any updated app to install, then don’t use appium: no reset.

Appium Web Inspector is a very good choice if you don’t want to install additional apps or your device has storage issues.

Alternatively, you can download and install the Appium Desktop utility for interacting with mobile elements.

Appium TestCase for Native(Android) App

Test Scenario:

1)Launch the Mobile App
2)Enter FirstName
3)Click Continue

Setting Up Your Project

1)Open IDE and create a new maven project.
2)Open the pom.xml file and add the Selenium and Appium dependencies mentioned below.

      <dependency>
            <groupId>io.appium</groupId>
            <artifactId>java-client</artifactId>
            <version>8.5.0</version>
        </dependency>
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>7.8.0</version>
            <scope>test</scope>
        </dependency>
       <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>4.1.0</version>
        </dependency>

3. Create a new java class name “androidRunner” and import the package then specify the URL of the Appium server and desired capabilities to connect to the mobile device/emulator. Here’s a sample code snippet:

package com.appium2;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.service.local.AppiumDriverLocalService;
import org.openqa.selenium.By;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import java.net.URL;

public class androidRunner {

        public AndroidDriver wd = null;
        
    @BeforeTest
    public void setup() {
       UiAutomator2Options capabilities = new UiAutomator2Options();
       capabilities.setPlatformName("Android")
                   .setAutomationName("UIAutomator2")
                         .setAppPackage("com.swaglabsmobileapp")
                         .setAppActivity("com.swaglabsmobileapp.MainActivity")
                          .setApp(System.getProperty("user.dir")+"/src/test/resources/SauceLabsMobile.apk");
      try {
        wd = new AndroidDriver(new URL("http://0.0.0.0:4723/"),capabilities);
          }
          catch(Exception e)
            {
                e.printStackTrace();
            }
        }

4. Write your first test script.

Here’s a complete simple example code to launch the app and perform a basic interaction:

package com.appium2;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.service.local.AppiumDriverLocalService;
import org.openqa.selenium.By;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import java.net.URL;

public class androidRunner {

        public AndroidDriver wd = null;
        
        @BeforeTest
        public void setup() {
         UiAutomator2Options capabilities = new UiAutomator2Options();
           capabilities.setPlatformName("Android")
                       .setAutomationName("UIAutomator2")
                       .setAppPackage("com.swaglabsmobileapp")
                       .setAppActivity("com.swaglabsmobileapp.MainActivity")
                            .setApp(System.getProperty("user.dir")+"/src/test/resources/SauceLabsMobile.apk");
      try {
                wd = new AndroidDriver(new URL("http://0.0.0.0:4723/"),capabilities);
            }catch(Exception e)
            {
                e.printStackTrace();
            }
        }

        @Test
        public void applicationrunb() throws Throwable {
            System.out.println("HELLO App launched");
            wd.findElement(AppiumBy.id("loginbutton")).click();
            
          }

        @AfterTest
        public void stopappium()
        {
            wd.quit();
        }
    }

Let’s understand the code. First, create the AndroidDriver reference then add the setup method which will execute before every test execution. inside this setup method add the desired capabilities, these capabilities are required by the Appium server to understand the desired session.

5. Now Start the Appium server, open Android Studio, launch the emulator and execute the demo test class.

How to Start Appium Server Using Code

AppiumDriverLocalService is a class in Appium that has a method buildDefaultService(). By using this we can start the Appium Server programmatically. In DemoTest Class add the below code in @BeforeSuite and @AfterSuite annotations.

Visit this article to learn how to start and stop the Appium server using Java code.

Now we don’t need to start the Appium server manually. Before Test Execution, it will up the service and once the session is created successfully test execution will start on the attached emulator.

Mobile Automation Using Appium 2.0

Tips To Remember

1)Always use the compatible Appium version with the Selenium version otherwise will face compatibility issues during execution. Refer to this for compatible versions.

Discover more from AutomationQaHub

Subscribe now to keep reading and get access to the full archive.

Continue reading