Appium Testing Tutorial For Mobile Applications
Tags: mobile, android, webdev
Published: 2023-01-03 16:58:16



The count of mobile users is on a steep rise. According to the research, by 2025, it is expected to reach 7.49 billion users worldwide. 70% of all US digital media time comes from mobile apps, and to your surprise, the average smartphone owner uses ten apps per day and 30 apps each month.

![](https://cdn-images-1.medium.com/max/2000/0*6mOpX286XDDZ1bH-.png)

In the present world, there are apps for every mobile device and purpose, and feedback is an integral part of their improvement process. With the emerging technology and demand for faster releases, it is quite important to make sure that the mobile apps need to be tested thoroughly before it is released to the customers.

There are many tools available to perform [mobile app testing](https://www.lambdatest.com/mobile-app-testing?utm_source=devto&utm_medium=organic&utm_campaign=jan03_sd&utm_term=sd&utm_content=webpage) in the market. Still, there are more [challenges in mobile app testing](https://www.lambdatest.com/blog/mobile-app-testing-challenges/?utm_source=devto&utm_medium=organic&utm_campaign=jan03_sd&utm_term=sd&utm_content=blog) than web application testing, as there is a huge fragmentation when it comes to mobile devices & mobile operating systems. You can learn more about Android and iOS fragmentation from our earlier blog on how to deal with [iOS version fragmentation](https://www.lambdatest.com/blog/ios-version-fragmentation/?utm_source=devto&utm_medium=organic&utm_campaign=jan03_sd&utm_term=sd&utm_content=blog).

Amongst all the [mobile automation tools](https://www.lambdatest.com/blog/mobile-app-testing-tools/?utm_source=devto&utm_medium=organic&utm_campaign=jan03_sd&utm_term=sd&utm_content=blog) (or frameworks), Appium is one of the most popular open-source test automation frameworks for mobile app testing. It is cross-platform and compatible with a number of popular programming languages (e.g., Java, Python, JavaScript, etc.) for [app test automation](https://www.lambdatest.com/app-test-automation?utm_source=devto&utm_medium=organic&utm_campaign=jan03_sd&utm_term=sd&utm_content=webpage). The best part is that you don’t have to recompile your app or modify it due to the usage of standard automation APIs on the supported platforms.

While Selenium is still the king of the web, Appium rules the mobile domain, with 35% of the organization preferring it over other free/open-source functional automation tools.

![](https://cdn-images-1.medium.com/max/2000/0*-5NLtmH_V3www51G.png)

In this Appium testing tutorial, we will deep dive into the essentials of the Appium framework for performing [mobile automation tests](https://www.lambdatest.com/mobile-automation-test?utm_source=devto&utm_medium=organic&utm_campaign=jan03_sd&utm_term=sd&utm_content=webpage).

## Introduction to Appium testing

Appium is an open-source framework for automating native, mobile web, and hybrid applications on Android mobile, iOS mobile, and Windows desktop platforms. For starters, Appium was developed by Dan Cuellar and Jason Huggins and today has about 15.2K stars and 5.7K Fork on GitHub. It has the highest market share compared to all the other mobile automation frameworks.

![](https://cdn-images-1.medium.com/max/3488/0*26Ks8kEszyyOwAW5.jpg)

Here are the major categories of mobile applications that can be tested using the Appium framework:

* **Native apps** — These are written using the iOS, Android, or Windows SDKs. Instagram, Spotify, and Facebook are some of the most popular native mobile apps.

* **Mobile web apps** — These are applications accessed using a mobile browser. Appium supports Safari on iOS and Chrome or the built-in ‘Browser’ app on Android. Uber and Pinterest are some of the most popular mobile web applications.

* **Hybrid apps** — These applications have a wrapper around a “webview” — a native control that enables interaction with the web content. Projects like Apache Cordova make it easy to build new apps using web technologies bundled into a native wrapper, creating a hybrid app. LinkedIn and Facebook are the most popular hybrid mobile applications.

* **Progressive Web Apps (PWAs)** — [Progressive Web Apps](https://www.lambdatest.com/blog/progressive-web-apps-and-the-future-of-mobile-web/?utm_source=devto&utm_medium=organic&utm_campaign=jan03_sd&utm_term=sd&utm_content=blog) are almost the same as regular web pages. In fact, they also function when the device is in offline mode (i.e., when the device is not connected to the Internet). The best thing about these “PWAs” is that they can be accessed via the app icon on the device. By clicking on it, the icon launches the app website.

Intrigued to know more about Web, Hybrid, and Native apps? You can deep-dive into it by going through the [Web vs Hybrid vs Native Apps](https://www.lambdatest.com/blog/web-vs-hybrid-vs-native-apps/?utm_source=devto&utm_medium=organic&utm_campaign=jan03_sd&utm_term=sd&utm_content=blog) comparison.

Appium is “cross-platform”; hence, tests written for one platform (i.e., Android, iOS, etc.) work seamlessly across other platforms supported by the Appium framework. This increases the code reusability factor and minimizes the code maintainability factor of the test code.

Watch this video to learn about Appium, one of the most popular open-source test automation frameworks for mobile app testing.

{% youtube Z_J8RmQf7Nw %}

## Platforms supported by Appium

Before we start discussing [Appium Mobile Testing](https://www.lambdatest.com/appium-mobile-testing?utm_source=devto&utm_medium=organic&utm_campaign=jan03_sd&utm_term=sd&utm_content=webpage) in greater detail, we should know about the platforms supported by Appium. In my personal experience, if we aren’t aware of the support provided by Appium, you may waste your time trying out some implementation that might not be supported by Appium.

Appium supports multiple platforms and testing modalities mentioned below:

* Native Apps

* Hybrid Apps

* Web Apps

* Testing on Real devices

* Testing on Simulators and Emulators

Appium supports iOS, Android, macOS, and Windows. Here is the list of drivers required for each platform:

 1. **iOS **Support for XCUITest Driver and SafariDriver for Apple’s Safaridriver.

 2. **Android **supports UiAutomator2 Driver, Espresso Driver, and Geckodriver for Firefox and GeckoView. However, UIAutomation Driver **support has been deprecated for both iOS and Android**.

 3. **macOS **support for Mac2 Driver is for macOS 10.15 (or later). Windows Desktop supports WinAppDriver Geckodriver for Firefox and GeckoView.

Apart from these, some vendors/community-based drivers can be found on the Appium official website.

In case you are a beginner to Appium you can learn to perform mobile app testing easily through this tutorial.

{% youtube pI5zrUhydyo %}

## How to install Appium?

Appium can be installed in two ways:

* Via NPM (Node Package Manager).

* By downloading Appium Desktop — a graphical, desktop-based way to launch the Appium server.

## Installation of Appium via NPM

If you want to run Appium via an npm install, you will need Node.js and NPM (use nvm, n, or brew install node to install Node.js).

Ensure that Node or Appium are not installed with sudo (or root); otherwise, you’ll run into problems. We recommend the latest stable version, though Appium supports Node.js 12+.

Run the following command on the terminal to install Appium via npm:

  

```
  npm install -g appium
```

Once installation is done, we can type “npm — -version,” which will return the installed version as shown below:

![](https://cdn-images-1.medium.com/max/2000/0*XTR822kfzwC2CxED.png)

## Installation of Appium via Desktop App

Simply download the latest version of Appium Desktop from the releases page of Appium official website.

![](https://cdn-images-1.medium.com/max/2000/0*9XOFUO4Grb_h13z8.png)

## How to start the Appium Server?

Appium server is basically written in Node.js and follows the HTTP JSON Wire Protocol. It acts as a virtual server that sends the commands to the framework to handle testing actions and scripts autonomously.

The Appium server processes the request and then responds with the test results. Now we can kick up an Appium server, either by running it from the command line or by using Appium Desktop (assuming the npm install was successful).

 1. We can use the below command to start the appium server:

  

```
  ⇒ appium
```

2- You will get to see **Welcome to Appium** like the below screenshot:

![](https://cdn-images-1.medium.com/max/2292/0*Hucw514Uj7Y0ynJ0.png)

3- If you have installed Appium Desktop, then we can start by clicking on the ‘**Start Server**’ button inside of Appium Desktop as shown below.

![](https://cdn-images-1.medium.com/max/2000/0*7vnkit1pJ96N9jli.png)

4- Appium will now show you a little welcome message showing the version of Appium you’re running and what port it’s listening on (the default is 4723: You can use “netstat” to check whether a port is available or not).

5- You can use port number 4726 instead of 4723. However, the server needs to be configured to run on the said port. This port information is vital since you will have to direct your test client to make sure to connect to Appium on this port. If you want to change the port, you can do so by using the -p flag when starting Appium (be sure to check out the full list of server parameters).

**Bonus Tip**

There is an easy way to start Appium server programmatically:

 

```
   AppiumDriverLocalService service = AppiumDriverLocalService.buildDefaultService();
    service.start(); // start the Appium Server
    service.stop(); // stop the Appium Server

```
## Android Emulators for Appium testing

In my ten years of experience, I have observed that when we want to [test mobile applications](https://www.lambdatest.com/blog/how-to-test-mobile-application-manually/?utm_source=devto&utm_medium=organic&utm_campaign=jan03_sd&utm_term=sd&utm_content=blog), we always prefer to do the dry run on emulators. In fact, most organizations prefer emulators for running a regression suite for Appium testing. So it is quite vital to know about emulators and how to create one for Appium testing.

Android emulator is a tool that creates virtual Android devices (with both software and hardware) that can be used for running [Android device tests](https://www.lambdatest.com/android-device-test?utm_source=devto&utm_medium=organic&utm_campaign=jan03_sd&utm_term=sd&utm_content=webpage) that are implemented using the Appium framework. You can go through the blog on [emulator vs simulator](https://www.lambdatest.com/blog/difference-between-emulator-vs-simulator-for-mobile-testing/?utm_source=devto&utm_medium=organic&utm_campaign=jan03_sd&utm_term=sd&utm_content=blog) differences to learn about emulators and simulators.

[Android emulators](https://www.lambdatest.com/android-emulator?utm_source=devto&utm_medium=organic&utm_campaign=jan03_sd&utm_term=sd&utm_content=webpage) are very important for performing [Android app testing](https://www.lambdatest.com/android-app-testing?utm_source=devto&utm_medium=organic&utm_campaign=jan03_sd&utm_term=sd&utm_content=webpage). Below are the steps to create an Android Mobile Emulator, which we can use for Mobile testing on any Android version.

 1. Download and install Android Studio in case it is not present on your machine.

 2. Open Android Studio and click on “Configure” “AVD Manager” from the list as shown in the below screenshot.

![](https://cdn-images-1.medium.com/max/2000/0*HhlnG21VP4sAkBOw.png)

3- Now click on the “Create Virtual Device” button, and you will be redirected to the “Select Hardware” page, where we need to select the mobile type, for example, “Pixel XL,” as shown below and then click on “Next”.

![](https://cdn-images-1.medium.com/max/2000/0*IkqShaoC2QGFTn_q.png)

4- After the “Select Hardware” page you will be able to see the “System Image” page. Here we need to select which Android version we want in our mobile device for testing, for example, Pie, Oreo, R, etc. In the below image, you can see that I have selected Android 11, then click “Next.”

![](https://cdn-images-1.medium.com/max/2000/0*d9GmeHH5UpcCuwOq.png)

Here, we can change the Memory and Storage by clicking on “Show Advanced Settings.”

![](https://cdn-images-1.medium.com/max/2000/0*DzXauWFkULg27LEU.png)

5- Now you will arrive at the “Verify Configuration” page. Here, you can check the selection we have made in steps 3 and 4. Provide the name for your device as I have given the name “AndroidMobile” in the below screenshot:

![](https://cdn-images-1.medium.com/max/2000/0*e9yVwKlVDeCDof2c.png)

6- Now the device is created, and in the dashboard, we can see it. Search with the device name that we have provided in Step 5, as shown below. Now click on the ‘Play’ button, and the device is ready for testing.

![](https://cdn-images-1.medium.com/max/2000/0*3lTRa7GgcQBBd_QX.png)

In the above screenshot, we have an edit option, which can be used to edit some fields for the emulator, and the dropdown button can be used for cold boot.

But sometimes, it’s really difficult to go through all the six steps mentioned above to create an emulator. So, instead of that, you can also use [online Android emulators](https://www.lambdatest.com/android-emulator-online?utm_source=devto&utm_medium=organic&utm_campaign=jan03_sd&utm_term=sd&utm_content=webpage) provided by LambdaTest.

Cloud-based [cross browser testing](https://www.lambdatest.com/?utm_source=devto&utm_medium=organic&utm_campaign=jan03_sd&utm_term=sd&utm_content=webpage) platforms like LambdaTest provides an [online device farm](https://www.lambdatest.com/online-device-farm?utm_source=devto&utm_medium=organic&utm_campaign=jan03_sd&utm_term=sd&utm_content=webpage) of over 3000+ Android emulators to [test on Android devices](https://www.lambdatest.com/test-on-android-devices?utm_source=devto&utm_medium=organic&utm_campaign=jan03_sd&utm_term=sd&utm_content=webpage?utm_source=devto&utm_medium=organic&utm_campaign=jan03_sd&utm_term=sd&utm_content=webpage).

We need to run just three steps to start an emulator on LambdaTest:

 1. [Login to LambdaTest](https://accounts.lambdatest.com/login?utm_source=devto&utm_medium=organic&utm_campaign=jan03_sd&utm_term=sd&utm_content=login) and go to the [LambdaTest Dashboard](https://accounts.lambdatest.com/login?utm_source=devto&utm_medium=organic&utm_campaign=jan03_sd&utm_term=sd&utm_content=login).

 2. Navigate to Real Time Testing > App Testing as shown below:

![](https://cdn-images-1.medium.com/max/2400/0*S8djCzJZ7WfrzVmh.png)

3- Now select your **BRAND **and **DEVICE/OS** and then click on the **START** button. Here, the sample apk is mentioned, but you can use your apk for Appium testing.

![](https://cdn-images-1.medium.com/max/2400/0*Tr_gd5X8e_VEtcpz.png)

Isn’t it easy :), Don’t forget to stop the emulator after use by clicking on the last button on the left panel.

![](https://cdn-images-1.medium.com/max/2000/0*lWnoOuGSSEs5s7md.png)

***Check this out: [Regression testing](https://www.lambdatest.com/blog/regression-testing-what-is-and-how-to-do-it/?utm_source=devto&utm_medium=organic&utm_campaign=jan03_sd&utm_term=sd&utm_content=blog)- Complete Guide With Best Practices***

## Installing App on Emulators

Once the emulator is ready for use and the Android path is set up correctly, the next step would be to install an app on the emulator on which we would be performing the app testing.

 1. The first step is to run the emulator on which you need to install the app.

 

```
   emulator -avd
```

2- The emulator will be launched, and the command will run in the terminal.

3- There are two ways to install the app:

* Install via ADB command:

 1. Download the apk and put it in folder

 2. Run the following command

adb install path/to/your/app.apk
When multiple emulators are open, run the following command
adb -s serial-number-of-device install path/to/your/app.apk

* Drag and Drop

We can also drag the apk file and drop on to the emulator, which will install the application.

Now perform testing by dragging your .apk to the newly created virtual device. There are many websites that provide the sample apk, which can be used for testing. If you do not have any .apk or .ipa files, you can run your sample tests on LambdaTest using our sample 🔗 Android app or sample 🔗 [iOS app](https://prod-mobile-artefacts.lambdatest.com/assets/docs/proverbial_ios.ipa).

## How to identify elements for Appium testing?

Before we can start writing test scripts, we need to understand identifying elements using attributes such as ID, Name, Class, etc., to perform actions. You can go through this [Selenium Locators tutorial](https://www.lambdatest.com/learning-hub/selenium-locators?utm_source=devto&utm_medium=organic&utm_campaign=jan03_sd&utm_term=sd&utm_content=learning_hub) to learn more about the locators.

To identify (or locate) elements, we can use **UIAutomatorViewer** or [Appium inspector](https://www.lambdatest.com/blog/appium-inspector-for-apps/?utm_source=devto&utm_medium=organic&utm_campaign=jan03_sd&utm_term=sd&utm_content=blog) to capture a snapshot of the current device screen, which shows the hierarchy of the UI components properly arranged on the screen.

UI Automator Viewer is a GUI tool that comes with Android SDK and is primarily used to inspect UI components of the Android Application. It also lets you view the properties associated with it. We can find it in the Android SDK “tools” folder with the file name ‘uiautomatorviewer.bat’ on Windows OS.

## How to access elements using UIAutomatorViewer?

When designing automation scripts, Appium uses “UIAutomatorViewer” to identify different properties of the object and use the properties to identify the required object.

There are several ways by which the UIAutomatorViewer can be launched:

* **For Windows**: Go to the path where UIAutomatorViewer exists (Android SDK folder) and run the batch file (for Windows) — uiautomatorviewer.bat.

* **For Mac**: Go to the location via terminal and run the uiautomatorviewer sh file. Execution permission must be given to the file, or errors will be thrown.

* It can be added as an alias to run directly via the terminal. In the Mac machine, add it to the bash_profile file.

**Example**: alias inspector='$ANDROID_HOME/tools/bin/uiautomatorviewer'

**For Mac:**

![](https://cdn-images-1.medium.com/max/2000/0*tgr6SnoZS8v5bRfP.png)

Follow the below mentioned steps for using UIAutomatorViewer:

 1. Connect the device to the PC via USB cable.

 2. Enable USB Debugging mode for the device connected.

 3. Run the ADB devices command in the command prompt/terminal to check if the device is detected and online/available.

 4. Open an application and click the **device screenshot** icon from the right up corner.

![](https://cdn-images-1.medium.com/max/NaN/1*b31hiO4ynbDLRrXWEFF4aQ.png)

5- After clicking on ‘**Device Screenshot**‘, an image of the current screen will be loaded in the UIAutomatorViewer, and we can use the mouse to click on any element and see the details of that element. You can also use the hierarchical tree in the top right to browse a particular element.

***Note***: The UIAutomatorViewer works only with Android native app elements.

Want to make your life of testing easy and fuss-free while debugging? [Try LT Debug Chrome extension!](https://www.lambdatest.com/lt-debug?utm_source=devto&utm_medium=organic&utm_campaign=jan03_sd&utm_term=sd&utm_content=webpage)

***Check this out: Mobile [Emulator Online](https://www.lambdatest.com/mobile-emulator-online?utm_source=devto&utm_medium=organic&utm_campaign=jan03_sd&utm_term=sd&utm_content=webpage)- Test your mobile websites and smartphone apps using our scalable on cloud mobile emulator.***

## How to work with Appium Inspector?

A default inspector available in standalone Appium (not in the npm version) is called **Appium Inspector**, which can be used to inspect Android and iOS elements. It simply mirrors the screen of the device, and we can interact with it and get the details of the elements. Let’s see how we can launch and work with it.

 1. Launch Appium from your application/programs menu by clicking on Start Server. You can refer to the screenshot in the “[How to start the Appium Server](https://www.lambdatest.com/blog/appium-tutorial/#D?utm_source=devto&utm_medium=organic&utm_campaign=jan03_sd&utm_term=sd&utm_content=blog)” section of this blog.

2- Click the Search icon from the right up corner.

![](https://cdn-images-1.medium.com/max/2000/0*psycYn1gJ3v9i2cP.png)

 1. Add correct desired capabilities as per your device/emulator and application. Then click on the **Start Session** button.

![](https://cdn-images-1.medium.com/max/2400/0*QQ01k_CWXrJIIwG3.png)

4- It will open the App screen, App Source (the hierarchy of elements), and Selected Element Pane (The attributes and associated values of the element). You can use this to create locators which will uniquely identify the element.

5- You can provide a .apk or .app file in desired capabilities inside Appium Inspector, which will install and launch the app. Refer to the screenshots below, which we have used to test .apk and passed in the value against “app.”

![](https://cdn-images-1.medium.com/max/2308/0*q7DXkBpfIePfJn9x.png)

6- Best thing about the Appium Inspector is that you can verify your written locator on screen. Check the below screenshots.

![](https://cdn-images-1.medium.com/max/2000/0*6h2M1PU7fCFgo3Gm.png)

If your locator is correct, then it will highlight the associated element.

![](https://cdn-images-1.medium.com/max/2000/0*cRBTnwOxqw5Md7bS.png)

Sometimes the screen on the left and your emulator won’t be in sync, so we need to click on the refresh button to bring the screen and emulator in sync:

![](https://cdn-images-1.medium.com/max/2000/0*Bo4R8JA5Pvng60rJ.png)

## Locators used for Mobile Automation

Shown below are some of the major locators used for mobile app automation:

 <table>
    <tr>
        <td>STRATEGY</td>
        <td>DESCRIPTION</td>
    </tr>
    <tr>
        <td>Accessibility ID</td>
        <td>	Reads a unique identifier for a UI element. For XCUITest, it is the element’s accessibility-id attribute. For Android, it is the element’s content-desc attribute.</td>
    </tr>
    <tr>
        <td>Class name</td>
        <td>	For iOS, it is the full name of the XCUI element and begins with XCUIElementType. For Android it is the full name of the UIAutomator2 class (e.g., android.widget.TextView)</td>
    </tr>
    <tr>
        <td>ID</td>
        <td>	Native element identifier. resource-id for android; name for iOS.</td>
    </tr>
    <tr>
        <td>Name</td>
        <td>	Name of element.</td>
    </tr>
    <tr>
        <td>XPath</td>
        <td>	Searches the app XML source using xpath (not recommended, has performance issues)</td>
    </tr>
    <tr>
        <td>Image</td>
        <td>	Locates an element by matching it with a base 64 encoded image file</td>
    </tr>
    <tr>
        <td>Android UiAutomator (UiAutomator2 only)</td>
        <td>Uses the UIAutomator API, in particular, the UiSelector class to locate elements. In Appium, you send the Java code, as a string, to the server, which executes it in the application’s environment, returning the element or elements. </td>
    </tr>
    <tr>
        <td>Android View Tag (Espresso only)</td>
        <td>Locates an element by its view tag. </td>
    </tr>
    <tr>
        <td>Android Data Matcher (Espresso only)</td>
        <td>Locates an element using Espresso DataMatcher. </td>
    </tr>
    <tr>
        <td>iOS UIAutomation</td>
        <td>When automating an iOS application, Apple’s Instruments framework can be used to find elements.</td>
    </tr>
</table> 

***Check this out: [WebDriver](https://www.lambdatest.com/learning-hub/webdriver?utm_source=devto&utm_medium=organic&utm_campaign=jan03_sd&utm_term=sd&utm_content=learning_hub) Tutorial: A Comprehensive Guide on Selenium WebDriver Automation***

## How to run Appium tests on iOS and Android?

In this section of this Appium testing tutorial, I will walk you through the steps for running Appium tests with iOS and Android. First, make sure the Appium server is up and running. You can refer to the section “[How to start the Appium Server](https://www.lambdatest.com/blog/appium-tutorial/#D?utm_source=devto&utm_medium=organic&utm_campaign=jan03_sd&utm_term=sd&utm_content=blog)” in this blog to kick start your first step for Appium testing.

Then, we have to check that the Android emulator we have created in the section “[Android Emulators for Appium testing](https://www.lambdatest.com/blog/appium-tutorial/#E?utm_source=devto&utm_medium=organic&utm_campaign=jan03_sd&utm_term=sd&utm_content=blog)” is up and running. For that, we need to run adb devices. For example, we should see one device connected, as shown in the below screenshot. This is the device Appium will use for testing purposes. (Remember that if the Android SDK tools are on your path, simply run: emulator -avd )

![](https://cdn-images-1.medium.com/max/3520/0*3V9t4yYbhgZO3pHS.png)

Now we need to wait for the Android emulator to finish launching. Sometimes, for various reasons, adb gets stuck. If it’s not showing any connected devices or otherwise failing, we can restart it by running: adb kill-server && adb devices.

Once we are done with starting the Appium server and checking the emulator, then we should be aware of the desired capabilities as per the OS(Android/iOS) we are using for Appium testing.

**Android**: Set the following desired capabilities in your WebDriver script:

```
    DesiredCapabilities capabilities = new DesiredCapabilities();
    capabilities.setCapability(MobileCapabilityType.PLATFORM_NAME, "iOS");
    capabilities.setCapability(MobileCapabilityType.PLATFORM_VERSION, "7.1");
    capabilities.setCapability(MobileCapabilityType.DEVICE_NAME, "iPhone Simulator");
    capabilities.setCapability(MobileCapabilityType.APP, myApp);
```

Using your WebDriver library of choice, set the remote session to use these capabilities and connect to the server running at port 4723 of localhost (or whatever host and port you specified when you started Appium). You are all set now!

There are many ways to start an Appium application (it will work exactly the same way as when the application is started via ADB command):

* apk or zip only, default activity will be launched (‘app’ capability)

* apk + activity (‘app’ + ‘appActivity’ capabilities)

* apk + activity + intent (‘app’ + ‘appActivity’ + ‘appIntent’ capabilities)

If you are not sure what activity is configured in your apk, you can proceed in one of the following ways:

* Mac/Linux: ‘adb shell dumpsys window windows | grep mFocusedApp’

* In the Ruby console: ‘adb shell dumpsys window windows`.each_line.grep(/mFocusedApp/).first.strip’

* In Windows terminal, run ‘adb shell dumpsys window windows’ and manually look for the mFocusedApp line.

**iOS**: Set the following desired capabilities in your WebDriver script::

```
    DesiredCapabilities capabilities = new DesiredCapabilities();
    capabilities.setCapability(MobileCapabilityType.PLATFORM_NAME, "Android");
    capabilities.setCapability(MobileCapabilityType.PLATFORM_VERSION, "4.4");
    capabilities.setCapability(MobileCapabilityType.DEVICE_NAME, "Android Emulator");
    capabilities.setCapability(MobileCapabilityType.APP, myApp);
```

In the set of capabilities for Android and iOS, myApp must be any one of the below:

* A local absolute path to your .apk or a .zip

* An URL of a zip file containing your .apk

* path to one of the sample app relative to the appium install root

Using the WebDriver library of choice, we can set the remote session to use these capabilities and connect to the server running at port 4723 of localhost . You should be all set now!

![](https://cdn-images-1.medium.com/max/2000/0*MItoJMnXFuCY9eV3.jpg)

***Check this out: Test On [iPhone tester](https://www.lambdatest.com/test-on-iphone-simulator?utm_source=devto&utm_medium=organic&utm_campaign=jan03_sd&utm_term=sd&utm_content=webpage) Simulator On Cloud***

## Appium testing with Demo Code

Here, we are using simple code, which can be used by anyone for their first test on Appium. We are using an emulator that we created in this post to run our apk, which can be downloaded from the ”[Installing App on Emulators](https://www.lambdatest.com/blog/appium-tutorial/#F?utm_source=devto&utm_medium=organic&utm_campaign=jan03_sd&utm_term=sd&utm_content=blog)” section of this post.

We will try to verify two important scenarios in the test below, the first one is “**open the geolocation page**” and the second one is “**speed test page**”. Apart from the code, I will share the screenshot on the emulator while running the test script for both the scenarios mentioned above.

Sometimes it’s difficult to start from scratch, so to make life easier, you can clone the LambdaTest’s 🔗LT-appium-java repository and then replace the vanilla_android.java class with the code mentioned below.

Now we are halfway done. We just need to hit the below two commands to run our sample test for Appium testing on [Android APK emulator online](https://www.lambdatest.com/apk-emulator-online?utm_source=devto&utm_medium=organic&utm_campaign=jan03_sd&utm_term=sd&utm_content=webpage). The first command is to install dependencies and second one to run our suite.

To install the required dependencies:

```

    mvn clean install
```

To run your test just type below command in your command prompt:

  

```
  mvn compile exec:java -Dexec.mainClass=vanilla_android -Dexec.classpathScope=”test”
```

**Sample Code:**

 

```
   import io.appium.java_client.AppiumDriver;
    import io.appium.java_client.MobileBy;
    import io.appium.java_client.MobileElement;
    import org.openqa.selenium.JavascriptExecutor;
    import org.openqa.selenium.remote.DesiredCapabilities;
    import org.openqa.selenium.remote.RemoteWebDriver;
    import java.net.MalformedURLException;
    import java.net.URL;
    
    public class vanilla_android {
       private static AppiumDriver driver;
    
       public static void main(String args[]) throws MalformedURLException, InterruptedException {
    
           try {
               DesiredCapabilities capabilities = new DesiredCapabilities();
               capabilities.setCapability("deviceName", "AndroidMobile");
               capabilities.setCapability("platformVersion", "11");
                   capabilities.setCapability("platformName", "Android");
               capabilities.setCapability("isRealMobile", false);
               capabilities.setCapability("app", "/Users/{user}/Downloads/proverbial_android.apk"); //Enter your app url
               capabilities.setCapability("name", "Sample Test Java");
               capabilities.setCapability("console", true);
               capabilities.setCapability("network", false);
               capabilities.setCapability("visual", true);
               capabilities.setCapability("devicelog", true);
    
    
               driver = new AppiumDriver(new URL("http://localhost:4723/wd/hub"), capabilities);
    
               //Open the geolocation page
               MobileElement geo = (MobileElement) driver.findElement(MobileBy.id("com.lambdatest.proverbial:id/geoLocation"));
               geo.click();
               Thread.sleep(5000);
    
               //takes back to home page
               MobileElement el3 = (MobileElement) driver.findElementByAccessibilityId("Home");
    
               driver.navigate().back();
               Thread.sleep(2000);
    
               //Takes to speed test page
               MobileElement speedtest = (MobileElement) driver.findElement(MobileBy.id("com.lambdatest.proverbial:id/speedTest"));
               speedtest.click();
               Thread.sleep(15000);
    
               driver.navigate().back();
    
           } catch (AssertionError a) {
               ((JavascriptExecutor) driver).executeScript("lambda-status=failed");
               a.printStackTrace();
           }
    // The driver.quit statement is required, otherwise the test continues to execute, leading to a timeout.
           driver.quit();
       }
    
    }
```

I am going to share two screenshots from live running to give you guys a look and feel of scenarios running on an emulator. The left section of the screenshot shows the execution of commands on the command prompt and the right panel is the emulator on which the script is executing.

**Scenario 1:** Open the geolocation page (check the geolocation page on the emulator screen)

![](https://cdn-images-1.medium.com/max/3200/0*H2UunRyaofnL0m3d.png)

**Scenario 2:** Takes the speed of the test page. (check the speed of the test page on the emulator).

![](https://cdn-images-1.medium.com/max/3200/0*mOEGSUA6b-mb52pb.png)

## Debugging Steps for Common Errors in Appium

 1. Make sure that before running the suite, the Android emulator is up and running. It’s useful to run adb kill-server && adb devices. This can reset the connection to the Android device, and the issue can be resolved.

 2. If you’re running your Android emulator using Fastboot and the emulator isn’t being detected by ADB properly, try doing a cold boot: Android Studio > Tools > AVD Manager > Actions > Cold Boot Now. This will mostly solve all emulator issues (refer to Step 6) from the “[Android Emulators for Appium testing](https://www.lambdatest.com/blog/appium-tutorial/#E?utm_source=devto&utm_medium=organic&utm_campaign=jan03_sd&utm_term=sd&utm_content=blog)” section to know how to cold boot.

 3. For iOS, if the Instrument crashes when running against a physical device (“exited with code 253”), ensure Xcode has downloaded device symbols. Go to Window -> Devices, and then it should start automatically. It is needed after iOS version upgrades.

 4. We need to ensure that the macOS keychain that holds the certificate required for building the app and the WebDriverAgent is unlocked. Especially when you are using ssh.

**General symptoms to look for are codesign failures.**

* Appium may sometimes have difficulties finding the node executable if you’ve installed Node and npm via MacPorts. We need to make sure that the MacPorts bin folder (default path is : /opt/local/bin ) is added to PATH somewhere in ~/.profile, ~/.bash_profile or ~/.bashrc.

* If you get an error “[INST STDERR] posix spawn failure; aborting launch” while running iOS tests, your app is not compiled correctly for the simulator or device.

## Appium testing on Real Device Cloud

When we talk about Appium testing or try to figure out the real use of Appium then, in my past experiences, two things come to my mind: The first one is that it supports both [Android automation testing ](https://www.lambdatest.com/android-automation-testing?utm_source=devto&utm_medium=organic&utm_campaign=jan03_sd&utm_term=sd&utm_content=webpage)and [iOS automation testing](https://www.lambdatest.com/ios-automation-testing?utm_source=devto&utm_medium=organic&utm_campaign=jan03_sd&utm_term=sd&utm_content=webpage), and the second one is that Appium is free and open source.

Remember that the learning curve for Appium testing is also quite easy, and you must have realized it by now if you are at the end of this post.

One important point to remember is that until now, we have run our test scripts using an emulator, but you must be wondering if I want to run on a [real device cloud](https://www.lambdatest.com/blog/real-device-cloud/?utm_source=devto&utm_medium=organic&utm_campaign=jan03_sd&utm_term=sd&utm_content=blog).

Do I need to buy an iPhone or Android phone? So the answer is NO. Now you must think, if we won’t buy a device, how can we test it? The answer is the buzzword “Cloud Devices.”

The best part is running our existing scripts on a real device is a cakewalk. Below, I will explain what changes to make if we opt for running our test on LambdaTest real device cloud. Here’s the walkthrough of the automated app testing on real devices using LambdaTest.

{% youtube _gC5igQyJf8 %}

You can also subscribe to the [LambdaTest YouTube Channel](https://www.youtube.com/channel/UCCymWVaTozpEng_ep0mdUyw?sub_confirmation=1) and stay updated with the latest tutorials around [Selenium testing,](https://www.lambdatest.com/selenium-automation?utm_source=devto&utm_medium=organic&utm_campaign=jan03_sd&utm_term=sd&utm_content=webpage) [Cypress E2E testing](https://www.lambdatest.com/cypress-e2e-testing?utm_source=devto&utm_medium=organic&utm_campaign=jan03_sd&utm_term=sd&utm_content=webpage), CI/CD, and more.

***Check this out: Run Automated [Puppeteer Testing](https://www.lambdatest.com/puppeteer-testing?utm_source=devto&utm_medium=organic&utm_campaign=jan03_sd&utm_term=sd&utm_content=webpage) Online***

You can use the same code that I have shared before but with the below changes:

 1. Firstly let’s change the value of desired capabilities for parameters:

```
    DesiredCapabilities capabilities = new DesiredCapabilities();
                capabilities.setCapability("deviceName", "Galaxy S20");
                capabilities.setCapability("platformVersion", "11");
                capabilities.setCapability("platformName", "Android");
                capabilities.setCapability("isRealMobile", true);
                capabilities.setCapability("app", "lt://APP10016551653068008326557"); //Enter app URL
```

2- Second change is to use Username and Password from LambdaTest and change Appium driver instance:


```
    public static String userName = System.getenv("LT_USERNAME") == null ? "LT_USERNAME"  //Add username here
                : System.getenv("LT_USERNAME");
        public static String accessKey = System.getenv("LT_ACCESS_KEY") == null ? "LT_ACCESS_KEY" //Add accessKey here
                : System.getenv("LT_ACCESS_KEY");
    
    driver = new AppiumDriver(new URL("https://" + userName + ":" + accessKey + "@beta-hub.lambdatest.com/wd/hub"), capabilities);

```
3- If you want help for creating authentication or uploading your app to the real device provided by LambdaTest, you can refer to this [Java with Appium](https://www.lambdatest.com/support/docs/appium-java/?utm_source=devto&utm_medium=organic&utm_campaign=jan03_sd&utm_term=sd&utm_content=webpage) documentation.

One thing we need to remember is that the scenarios are the same as used in the previous script using the emulator (open the geolocation page & take to speed test page). After all the changes are made on the vanilla_android class, the final piece of code will look like below:

```
    import io.appium.java_client.AppiumDriver;
    import io.appium.java_client.MobileBy;
    import io.appium.java_client.MobileElement;
    import io.appium.java_client.ios.IOSDriver;
    
    import org.openqa.selenium.JavascriptExecutor;
    import org.openqa.selenium.remote.DesiredCapabilities;
    import org.openqa.selenium.remote.RemoteWebDriver;
    import java.net.MalformedURLException;
    import java.net.URL;
    
    public class vanilla_android {
        public static String userName = System.getenv("LT_USERNAME") == null ? "LT_USERNAME"  //Add username here
                : System.getenv("LT_USERNAME");
        public static String accessKey = System.getenv("LT_ACCESS_KEY") == null ? "LT_ACCESS_KEY" //Add accessKey here
                : System.getenv("LT_ACCESS_KEY");
        private static AppiumDriver driver;
    
        public static void main(String args[]) throws MalformedURLException, InterruptedException {
    
            try {
               
                DesiredCapabilities capabilities = new      DesiredCapabilities();
                capabilities.setCapability("deviceName", "Galaxy S20");
                capabilities.setCapability("platformVersion", "11");
                capabilities.setCapability("platformName", "Android");
                capabilities.setCapability("isRealMobile", true);
                capabilities.setCapability("app", "lt://APP10016551653068008326557"); //Enter your app url
                capabilities.setCapability("deviceOrientation", "PORTRAIT");
                capabilities.setCapability("build", "Java Vanilla - Android");
                capabilities.setCapability("name", "Sample Test Java");
                capabilities.setCapability("console", true);
                capabilities.setCapability("network", false);
                capabilities.setCapability("visual", true);
                capabilities.setCapability("devicelog", true);
                driver = new AppiumDriver(new URL("https://" + userName + ":" + accessKey + "@beta-hub.lambdatest.com/wd/hub"), capabilities);
              //  driver = new AppiumDriver(new URL("http://localhost:4723/wd/hub"), capabilities);
                
                //Open the geolocation page
                MobileElement geo = (MobileElement) driver.findElement(MobileBy.id("com.lambdatest.proverbial:id/geoLocation"));
                geo.click();
                Thread.sleep(5000);
    
                //takes back to home page
                MobileElement el3 = (MobileElement) driver.findElementByAccessibilityId("Home");
    
                  driver.navigate().back();
                Thread.sleep(2000);
    
                //Takes to speed test page
                MobileElement speedtest = (MobileElement) driver.findElement(MobileBy.id("com.lambdatest.proverbial:id/speedTest"));
                speedtest.click();
                Thread.sleep(15000);
    
                driver.navigate().back();
    
            } catch (AssertionError a) {
                ((JavascriptExecutor) driver).executeScript("lambda-status=failed");
                a.printStackTrace();
            }
    // The driver.quit statement is required, otherwise the test continues to execute, leading to a timeout.
            driver.quit();
        }
        
    }
```

4- Once you run it from the command line using the command shared in the “[Appium testing with Demo Code](https://www.lambdatest.com/blog/appium-tutorial/#i?utm_source=devto&utm_medium=organic&utm_campaign=jan03_sd&utm_term=sd&utm_content=blog)” section:(mvn compile exec:java -Dexec.mainClass=vanilla_android -Dexec.classpathScope="test"), you can go to LambdaTest and check the execution report under Real Device section which will look like below:

![](https://cdn-images-1.medium.com/max/3200/0*Jcav0tj-AWn6B2Vt.png)

 
The dashboard shown above has many features like screenshots, artifacts, logs, and execution videos. Now with the execution visible on the dashboard, we are done with running our tests on real devices!

***Check this out: [Agile Testing](https://www.lambdatest.com/learning-hub/agile-testing?utm_source=devto&utm_medium=organic&utm_campaign=jan03_sd&utm_term=sd&utm_content=learning_hub) Tutorial: A Comprehensive Guide With Examples and Best Practices***

### **Conclusion:**


In this [mobile app testing tutorial](https://www.lambdatest.com/learning-hub/mobile-app-testing?utm_source=devto&utm_medium=organic&utm_campaign=jan03_sd&utm_term=sd&utm_content=learning_hub), we deep-dived into the basics of the Appium framework for performing Android and iOS app testing with code demonstration. We also looked at running Appium tests on cloud grids like LambdaTest. Finally, we learned how to debug errors while running Appium tests and how to run Appium tests on real devices.

Remember, “Try and Fail but do not fail to try.” So try to run the tests with the demo code provided in this Appium tutorial, and we’ll be happy to hear your thoughts. You can use LambdaTest to [test on Oppo](https://www.lambdatest.com/test-on-oppo?utm_source=devto&utm_medium=organic&utm_campaign=jan03_sd&utm_term=sd&utm_content=webpage) & other mobile devices right from android to iPhone.

Happy Testing!


](url)