Introduction

Embark on a journey to revolutionize your mobile automation skills with Appium and Java! In this comprehensive guide, you’ll dive deep into the world of mobile testing, exploring the magic of Appium and its seamless integration with Java. Whether you’re a seasoned QA engineer or just starting your automation voyage, this article is a must-read for anyone looking to elevate their mobile testing game.

What is Appium?

Discover the true potential of Appium, an open-source automation framework that empowers you to automate Android and iOS applications effortlessly. Let’s explore the core concepts, key features, and the flexibility that Appium brings to the table, making it the preferred choice for mobile app testing across the globe

Appium is an open-source test automation framework for use with native, hybrid, and mobile web apps.
It drives iOS, Android, and Windows apps using the WebDriver protocol.

Appium workflow diagram

Appium architecture

Appium architecture

Appium works on a client-server architecture, where the client (test script) communicates with the Appium server, enabling the automation of mobile apps on both Android and iOS platforms. The Appium server, running as a standalone entity, listens for commands from the client over HTTP. Test scripts send desired capabilities, specifying device information and test environment settings, to initiate a test session. Appium uses platform-specific drivers, such as UIAutomator2 for Android and XCTest for iOS, to interact with mobile app elements based on locator strategies like ID, XPath, and name. Automation commands from the client, such as “click” or “sendKeys,” are translated by the server into low-level device-specific actions, seamlessly executing test scenarios. The process allows testers to write automation scripts once and run them across various devices and operating systems, making Appium a powerful and widely-used mobile automation framework.

Pre-requisites

Before we embark on our Appium adventure, let’s ensure you have the right setup by covering all the prerequisites, including the installation of necessary tools, environment setup, and any essential dependencies, so you can start building your mobile test automation foundation confidently.

  1. Download the Java JDK (Java Development Kit) installation package.
    https://www.oracle.com/java/technologies/javase/javase-jdk8-downloads.html
  2. Install the Java JDK on your system.
  3. Set up the “JAVA_HOME” environment variable to point to the installation directory of the Java JDK.
  4. Update the system’s “PATH” variable to include the “%JAVA_HOME%\bin” directory.
  5. Download the Maven installation package from the official website.
    https://maven.apache.org/download.cgi
  6. Unzip the Maven package to the desired folder.
  7. Configure Maven by setting up the “MAVEN_HOME” environment variable to point to the installation directory.
  8. Update the system’s “PATH” variable to include the “%MAVEN_HOME%\bin” directory.
  9. Download and Install IntelliJ IDEA Community Edition.
    https://www.jetbrains.com/idea/download
  10. Download and Install XCode.
    https://apps.apple.com/us/app/xcode/id497799835?mt=12
  11. Download Android SDK. (If you need the emulators in Android Studio, Download and install Android Studio too)
    https://developer.android.com/studio
  12. Configure Android by setting up the “ANDROID_HOME” environment variable to point to the Android SDK directory.
  13. Update the system’s “PATH” variable to include the “%ANDROID_HOME%\platform-tools” directory.
  14. Download and Install Node.JS v7.6.0 or above.
    https://nodejs.org/en/download/
  15. Install Appium 2.0 by executing the below command.
    npm i -g appium@next
  16. Download and Install Appium Inspector.
    https://github.com/appium/appium-inspector/releases
  17. You can use Android Studio Emulators or Real devices. But you must enable “Developer options” and “USB debugging” in your device. Also, you should enable “Unknown sources” from Settings → Security.
  18. Install Appium Drivers
    appium driver install uiautomator2
    appium driver install xcuitest
  19. Navigate to the “Environment Variables” settings on your computer and review the variables and their corresponding values listed in the “System Variable” section.
Environment variables

20. Download the sample Android application.
https://github.com/appium-boneyard/sample-code/blob/master/sample-code/apps/ApiDemos/bin/ApiDemos-debug.apk

How to get appPackage and appActivity?

Unravel the mystery behind obtaining “appPackage” and “appActivity” for your Android application. We’ll provide step-by-step guidance on extracting these crucial elements, enabling you to establish a connection between Appium and your app effortlessly.

  1. Open the command prompt.
  2. Execute adb shell
  3. Now connect your mobile phone to the computer and open the app which you want to find the appPackage and appActivity.
  4. Execute dumpsys window windows | grep -E 'mCurrentFocus'
  5. Identify appPackage and appActivity as below.
Identifying the appPackage and the appActivity

How to Inspect a session to get locators?

Gain the superpower of locating elements within your mobile app! Let’s go through the process of inspecting sessions, understanding the structure of your application, and finding the perfect locators using Appium’s powerful techniques.

  1. Start the Appium server by executing the below command on a new command prompt.
    appium --address 127.0.0.1 --port 4723
  2. Now the Appium server is up and running.
  3. Open the Appium Inspector.
  4. Provide host address as “127.0.0.1” and port as “4723”.
Updating the remote host and the remote port

5. Add the desired capabilities according to your application and mobile device.

Setting up the desired capabilities for the application and device

6. Click on the “Start Session” button.

7. Once Inspect session is loaded, you will see the application on the mobile screen.

Inspector view

8. Click on an element to see the relevant locator.

Checking an element locator from the inspector

Important:

In the context of selecting locators for elements, it is advisable to prioritize using “accessibility id” or “id” over “XPath”. While “XPath” might undergo changes over time, “accessibility id” or “id” typically remain constant, providing more stability and reliability to your test scripts. Therefore, to ensure the longevity and consistency of your locators, it is recommended to opt for “accessibility id” or “id” whenever possible.

How to develop an Appium test?

Now comes the exciting part — creating your first Appium test! We’ll walk you through the development process, explaining how to write clean and robust test scripts using Java, and leveraging the Appium WebDriver API to interact with your app’s elements seamlessly.

  1. Open the IntelliJ IDEA.
  2. Add dependencies to Maven project “pom.xml
pom.xml for the first Appium test

3. Expand the src/test folder.

4. Right-click on the test folder and add a new Java class.

Adding the test class

5. Provide a suitable name for the class. For this tutorial, I will give “LandingPageTest” as the test class name.

6. Add this code.

A sample Appium test

How to run the test?

It’s showtime! Learn how to execute your Appium test suite across different devices, emulators, or real devices. We’ll later cover various test execution approaches, ensuring you’re equipped to run your tests efficiently and make informed decisions based on test outcomes.

For this moment, let’s see how to run the test using IntelliJ IDEA which is a programming IDE.

  1. Start the Appium server by executing the below command on a new command prompt.
    appium --address 127.0.0.1 --port 4723
  2. Now the Appium server is up and running.
  3. Click on the green color play button in front of the class name.
Executing Appium tests

4. You will see tests are running on the mobile device.

After executing the Appium tests

Sample Project: https://github.com/osandadeshan/first_appium_test

Next steps

Embark on an inspiring journey with my next article on “Mobile application automation with Page Object Model”. Discover powerful techniques to elevate your testing skills and achieve excellence in mobile automation. 🚀📱
Don’t miss this game-changing guide! Click here to access the article and unlock new possibilities in your testing projects.

Happy Automation !!!

--

--

Osanda Deshan Nimalarathna
Test Automation Master

Founder of MaxSoft | RPA Solution Architect | Open-source Contributor | Automation Framework Developer | Technical Specialist