Appium Tutorial for Mobile Automation Testing
Introduction to Appium
Appium is an open-source, cross-platform tool for automating mobile application testing. It supports iOS, Android, and Windows platforms, and it allows you to write tests using a variety of programming languages including Java, JavaScript, Ruby, Python, C#, and others.
Appium is based on the WebDriver protocol, which means it uses the same API as Selenium WebDriver to interact with the mobile application. This makes it easy for developers and testers who are already familiar with Selenium WebDriver to start using Appium without any additional learning curve.
Appium supports different types of mobile applications, including native, hybrid, and mobile web applications. It provides a set of APIs that can be used to automate various mobile app functions such as interacting with UI elements, simulating user gestures, and checking the state of the application.
In this tutorial, we will explore the basics of Appium and how to set up an environment to get started with automated testing using Appium.
Getting Started with Appium
To get started with Appium, you need to have the following software installed on your system:
- Java Development Kit (JDK)
- Android Studio (for Android testing)
- Xcode (for iOS testing)
- Node.js
- Appium Desktop or Appium Server
You can download and install all these tools from their respective websites. Once you have installed these tools, you can follow the steps below to set up your Appium environment.
Step 1: Install Node.js
Node.js is a JavaScript runtime that allows you to run JavaScript code outside the browser. Appium is built using Node.js, so you need to install it on your system before you can start using Appium.
You can download Node.js from the official website: https://nodejs.org/en/download/. Once you have downloaded the installer, run it and follow the prompts to complete the installation.
Step 2: Install Appium
Appium can be installed in two ways: Appium Desktop and Appium Server. Appium Desktop is a graphical user interface (GUI) application that provides a simple way to start and stop the Appium server, inspect the application, and set up the desired capabilities.
To download Appium Desktop, go to the official website: https://github.com/appium/appium-desktop/releases/latest and download the appropriate version for your operating system.
Once you have downloaded the installer, run it and follow the prompts to complete the installation.
Alternatively, you can install Appium Server using npm (Node Package Manager). To do this, open a command prompt or terminal window and run the following command:
npm install -g appium
This command will download and install Appium Server globally on your system.
Step 3: Set up Android and iOS Environments
To automate mobile app testing using Appium, you need to set up the Android and iOS environments.
For Android testing, you need to install Android Studio, which provides the Android SDK and tools required for testing. You can download Android Studio from the official website: https://developer.android.com/studio.
Once you have downloaded the installer, run it and follow the prompts to complete the installation. During the installation process, make sure to select the components that include the Android SDK and tools.
After installing Android Studio, you need to configure the Android SDK path in the environment variables. To do this, follow the steps below:
- Open the System Properties window (Press Win + Pause)
- Click on "Advanced system settings"
- Click on "Environment Variables"
- Under "System Variables", scroll down and click on "New"
- Enter "ANDROID_HOME" as the variable name and the path to your Android SDK folder as the variable value (e.g., C:\Users\username\AppData\Local\Android\Sdk)
- Click "OK
How to Use Appium for Mobile Automation
Now that we have set up the environment for Appium, we can move on to the actual testing process. In this section, we will create a basic Appium test that interacts with a mobile application.
Step 1: Start Appium Server
Before you can start running tests with Appium, you need to start the Appium server. You can start the server using Appium Desktop or the command line.
To start the server using Appium Desktop, open the application and click the "Start Server" button. Appium will start the server and display the server log in the console.
To start the server using the command line, open a command prompt or terminal window and run the following command:
appium
This command will start the Appium server and display the server log in the console.
Step 2: Install Mobile Application
To automate testing for a mobile application, you need to have the application installed on your device or emulator. You can download the application from the app store or get it from the developer.
Step 3: Set Up Desired Capabilities
To run tests with Appium, you need to set up desired capabilities that define the device and application settings. Desired capabilities are a set of key-value pairs that specify the device, application, and other settings that you want to use for testing.
For example, to run tests for an Android application, you can set up desired capabilities as follows:
javaDesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("platformName", "Android");
capabilities.setCapability("deviceName", "Android Emulator");
capabilities.setCapability("appPackage", "com.example.android.myapplication");
capabilities.setCapability("appActivity", "MainActivity");
capabilities.setCapability("automationName", "UiAutomator2");
capabilities.setCapability("udid", "emulator-5554");
In the code snippet above, we have set up desired capabilities for an Android emulator. We have specified the platform name as "Android", device name as "Android Emulator", the package name and activity name of the application, the automation name as "UiAutomator2", and the unique device identifier (UDID) of the emulator.
You can also set up desired capabilities for an iOS application as follows:
javaDesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("platformName", "iOS");
capabilities.setCapability("deviceName", "iPhone 11");
capabilities.setCapability("platformVersion", "14.5");
capabilities.setCapability("app", "/path/to/app.app");
capabilities.setCapability("automationName", "XCUITest");
capabilities.setCapability("udid", "0123456789abcdef0123456789abcdef01234567");
In the code snippet above, we have set up desired capabilities for an iPhone device. We have specified the platform name as "iOS", device name as "iPhone 11", the platform version as "14.5", the path to the application file, the automation name as "XCUITest", and the UDID of the device.
Step 4: Write Test Code
Once you have set up desired capabilities, you can write test code to interact with the mobile application.
In the code snippet below, we will launch the application, enter a username and password, and click on the login button:
java
import io.appium.java_client.MobileElement;import io.appium.java_client.android.AndroidDriver;import io.appium.java_client.remote.MobileCapabilityType;import org.openqa.selenium.remote.DesiredCapabilities;import java.net.URL;import java.util.concurrent.TimeUnit;public class AppiumTest {public static void main(String[] args) throws Exception {// Set up desired capabilitiesDesiredCapabilities capabilities = new DesiredCapabilities();capabilities.setCapability(MobileCapabilityType.PLATFORM_NAME, "Android");capabilities.setCapability(MobileCapabilityType.DEVICE_NAME, "emulator-5554");capabilities.setCapability(MobileCapabilityType.APP, "/path/to/app.apk");capabilities.setCapability(MobileCapabilityType.AUTOMATION_NAME, "UiAutomator2");// Create Android driverAndroidDriver<MobileElement> driver = new AndroidDriver<MobileElement>(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);// Set implicit wait timedriver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);// Interact with mobile applicationMobileElement username = driver.findElementById("com.example.app:id/username");username.sendKeys("testuser");MobileElement password = driver.findElementById("com.example.app:id/password");password.sendKeys("testpass");MobileElement loginBtn = driver.findElementById("com.example.app:id/login");loginBtn.click();// Close applicationdriver.quit();}}
Let's break down this code and see what each part does.
- Import Required Packages
First, we import the required packages for the test:
javaimport io.appium.java_client.MobileElement;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.remote.MobileCapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import java.net.URL;
import java.util.concurrent.TimeUnit;
Here, we import the MobileElement and AndroidDriver classes from the Appium Java client library, the DesiredCapabilities class from the Selenium Java library, and the URL class from the Java standard library. We also import the MobileCapabilityType enum from the Appium Java client library to set up desired capabilities.
2. Create Android Driver
Next, we create an Android driver using the desired capabilities:
java// Create Android driver
AndroidDriver<MobileElement> driver = new AndroidDriver<MobileElement>(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
Here, we create a new AndroidDriver object by passing the URL of the Appium server and the desired capabilities as parameters.
2.1. AndroidDriver<MobileElement>
The first part of this line of code declares a variable named "driver" of type AndroidDriver<MobileElement>. This means that the "driver" object is an instance of the AndroidDriver class that is capable of interacting with mobile elements.
2.2. new AndroidDriver<MobileElement>(new URL("http://127.0.0.1:4723/wd/hub"), capabilities)
The second part of this line of code initializes the "driver" object. It creates a new instance of the AndroidDriver class and passes two arguments:
- A URL object that represents the address of the Appium server. In this case, the address is "http://127.0.0.1:4723/wd/hub". This is the default address used by Appium server.
- A desired capabilities object that defines the properties and behavior of the session that the driver will create with the Appium server. This object is passed as an argument to the AndroidDriver constructor.
The "capabilities" object is a desired capabilities object that is defined earlier in the code using the DesiredCapabilities class. It defines the properties and behavior of the session that the driver will create with the Appium server. In this case, it specifies the capabilities for an Android device, including the platform name, device name, and app package and activity.
Overall, this line of code initializes the "driver" object by creating a new instance of the AndroidDriver class and passing the Appium server URL and the desired capabilities object as arguments. Once the "driver" object is initialized, it can be used to interact with the mobile application.
- Set Implicit Wait Time
Next, we set an implicit wait time of 15 seconds:
java// Set implicit wait time
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
Here, we set an implicit wait time of 15 seconds using the manage() method of the driver object and the timeouts() method. This will make the driver wait for up to 15 seconds for an element to appear before throwing an exception.
- Interact with Mobile Application
Next, we interact with the mobile application by finding elements and performing actions on them:
java// Interact with mobile application
MobileElement username = driver.findElementById("com.example.app:id/username");
username.sendKeys("testuser");
MobileElement password = driver.findElementById("com.example.app:id/password");
password.sendKeys("testpass");
MobileElement loginBtn = driver.findElementById("com.example.app:id/login");
loginBtn.click();
Here, we first find the "username" element by its ID using the findElementById() method of the driver object and assign it to a MobileElement object named "username". We then use the sendKeys() method to enter the text "testuser" into the "username" field.
Next, we find the "password" element by its ID using the findElementById() method of the driver object and assign it to a MobileElement object named "password". We then use the sendKeys() method to enter the text "testpass" into the "password" field.
Finally, we find the "login" button element by its ID using the findElementById() method of the driver object and assign it to a MobileElement object named "loginBtn". We then use the click() method to click the "login" button.
- Close Application
Finally, we close the application by quitting the driver:
java// Close application
driver.quit();
Here, we use the quit() method of the driver object to close the application and release all resources associated with it.
And that's it! This is a simple Appium test code for a mobile application that demonstrates how to set up desired capabilities, create an Android driver, interact with the application, and close it using Appium Java client library. You can use this code as a starting point to write more complex tests for your mobile application.
Different ways to Find Elements of Mobile Applications for Appium
To find elements on a mobile application using Appium, you can use various locators such as ID, name, class name, accessibility id, xpath, and other custom attributes. Here are some examples of how to find elements using these locators:
- Find Element by ID
To find an element by its ID, you can use the findElementById() method of the driver object. For example:
javaMobileElement element = driver.findElementById("com.example.app:id/button");
This will find an element with ID "button" and assign it to the "element" object.
- Find Element by Name
To find an element by its name, you can use the findElementByName() method of the driver object. For example:
javaMobileElement element = driver.findElementByName("Button");
This will find an element with the name "Button" and assign it to the "element" object.
- Find Element by Class Name
To find an element by its class name, you can use the findElementByClassName() method of the driver object. For example:
javaMobileElement element = driver.findElementByClassName("android.widget.Button");
This will find an element with the class name "android.widget.Button" and assign it to the "element" object.
- Find Element by Accessibility ID
To find an element by its accessibility ID, you can use the findElementByAccessibilityId() method of the driver object. For example:
javaMobileElement element = driver.findElementByAccessibilityId("Button");
This will find an element with the accessibility ID "Button" and assign it to the "element" object.
- Find Element by XPath
To find an element by its XPath, you can use the findElementByXPath() method of the driver object. For example:
javaMobileElement element = driver.findElementByXPath("//android.widget.Button[@text='Login']");
This will find an element with the text "Login" and assign it to the "element" object.
- Find Element by Custom Attribute
You can also find elements by their custom attributes using the findElementBy() method of the driver object. This method takes a MobileBy object as an argument, which is created using the MobileBy class. For example:
javaMobileElement element = driver.findElement(MobileBy.custom("attribute", "value"));
This will find an element with a custom attribute "attribute" and a value of "value", and assign it to the "element" object.
Which Tool to use to inspect elements of Mobile Applications for Appium
To inspect elements on a mobile application for Appium, you can use several tools that provide a visual representation of the application's user interface and the elements on it. Here are some popular tools for inspecting elements on mobile applications:
- Appium Desktop
Appium Desktop is a free and open-source tool that provides a graphical user interface (GUI) for Appium. It allows you to start and stop the Appium server, inspect elements on the mobile application, and create and execute test scripts. Appium Desktop provides a visual tree view of the application's user interface, allowing you to select and inspect elements and view their properties.
- UI Automator Viewer
UI Automator Viewer is a tool provided by the Android SDK that allows you to inspect the elements on an Android application. It provides a hierarchical view of the application's user interface and allows you to select and inspect individual elements. UI Automator Viewer can be launched from the command line or from Android Studio.
- Xcode's Accessibility Inspector
Xcode's Accessibility Inspector is a tool provided by Apple's Xcode IDE that allows you to inspect the elements on an iOS application. It provides a hierarchical view of the application's user interface and allows you to select and inspect individual elements. The Accessibility Inspector can be launched from Xcode's "Developer Tools" menu.
- Selendroid Inspector
Selendroid Inspector is a tool provided by the Selendroid project that allows you to inspect the elements on an Android application. It provides a graphical user interface that displays the elements on the application and allows you to select and inspect individual elements. Selendroid Inspector can be launched from the command line or from the Selendroid server.
Overall, these tools provide a visual representation of the mobile application's user interface and allow you to inspect and interact with the elements on it. They are essential for developing and testing mobile applications with Appium.
How to Use Appium Dekstop for inspecting elements?
Appium Desktop is a tool that provides a graphical user interface (GUI) for Appium, making it easier to develop and test mobile applications. Here is a step-by-step guide on how to use Appium Desktop:
- Download and install Appium Desktop
You can download Appium Desktop from the official website: https://github.com/appium/appium-desktop/releases. Once you have downloaded the installation file, run it and follow the installation wizard to install Appium Desktop on your computer.
- Start the Appium server
Launch Appium Desktop and click on the "Start Server" button. This will start the Appium server on your computer and display the logs in the console window.
- Create a new session
Click on the "New Session" button to create a new session. This will open the "Desired Capabilities" window, where you can specify the settings for the session.
- Specify the desired capabilities
In the "Desired Capabilities" window, you can specify the settings for the session. These settings include the platform name, device name, app package and activity (for Android), app path (for iOS), and other options.
For example, to test an Android application, you can specify the following desired capabilities:
json{
"platformName": "Android",
"deviceName": "Android Emulator",
"appPackage": "com.example.myapp",
"appActivity": ".MainActivity"
}
- Connect to the device
To connect to the device, you need to specify the IP address and port number of the Appium server. This can be done by clicking on the "Start Inspector Session" button.
- Inspect the elements
Once you are connected to the device, Appium Desktop will display a visual representation of the application's user interface. You can select individual elements by clicking on them, and Appium Desktop will display their properties.
- Run test scripts
Once you have inspected the elements, you can use Appium Desktop to create and run test scripts. You can write the test scripts in any programming language supported by Appium (such as Java, Python, or JavaScript), and Appium Desktop will execute them on the connected device.
In conclusion, Appium is a powerful tool for mobile automation testing that allows developers to automate tests for both Android and iOS applications. With Appium, you can write tests in your preferred programming language, making it accessible to developers with varying levels of experience.
Comments
Post a Comment