Opening Hours : Monday to Sunday - 9AM to 6PM
Appium is an open-source and cross Platform test automation framework primarily used for automating mobile applications. It supports testing for native, hybrid, and mobile web applications on iOS and Android platforms. Appium allows you to write tests in multiple programming languages such as Java, Python, JavaScript, and Ruby using the WebDriver protocol.
Key Features of Appium
1. Cross-Platform Testing
- iOS and Android Support: Appium allows you to write tests that can be executed on both Android and iOS devices without modifying the test logic.
2. Language Flexibility
- Multiple App Types: Appium supports testing native apps (written with SDKs like Android SDK or iOS SDK), hybrid apps (using WebViews), and mobile web apps.
Appium doesn’t enforce a specific programming language. It can be used with WebDriver-compatible languages like:
- Java
- Python
- JavaScript
- Ruby
- C#
3. No Need for App Modifications
You can test your mobile apps without having to recompile or modify them specifically for testing, which is especially useful in production environments.
4. WebDriver Protocol
Appium uses the WebDriver protocol (also known as Selenium WebDriver), which is a standard for web and mobile testing. This allows Appium to integrate with existing Selenium testing frameworks.
Appium follows a client-server
architecture:
Appium Workflow
How to Setup Appium on Local
1
Prerequisites
2
Installing Appium
bash
Copy
code
npm
install -g appium
bash
Copy
code
appium
You’ll see Appium server logs indicating that the server is running.
3
Appium Desktop
Alternatively, you can use Appium Desktop, which provides a GUI for starting the server and inspecting elements on your app. It’s available for download from the Appium website.
Test Automation Example (Java)
Below is an example of a simple
Appium test in Java using the Appium client library and JUnit.
Step 1: Add Dependencies
Include the following dependencies
in your pom.xml (for Maven):
xml
<dependency>
<groupId>io.appium</groupId>
<artifactId>java-client</artifactId>
<version>7.6.0</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.0.0</version>
</dependency>
Step 2: Create the Test Script
java
import
io.appium.java_client.MobileElement;
import
io.appium.java_client.android.AndroidDriver;
import
io.appium.java_client.remote.MobileCapabilityType;
import
org.junit.After;
import
org.junit.Before;
import
org.junit.Test;
import
org.openqa.selenium.remote.DesiredCapabilities;
import java.net.MalformedURLException;
import
java.net.URL;
public
class AppiumTest {
private AndroidDriver<MobileElement>
driver;
@Before
public void setUp() throws
MalformedURLException {
DesiredCapabilities capabilities = new
DesiredCapabilities();
capabilities.setCapability(MobileCapabilityType.PLATFORM_NAME,
"Android");
capabilities.setCapability(MobileCapabilityType.DEVICE_NAME,
"emulator-5554");
capabilities.setCapability(MobileCapabilityType.APP,
"/path/to/your/app.apk");
// Initialize the driver to communicate
with Appium server
driver = new AndroidDriver<>(new
URL("http://localhost:4723/wd/hub"), capabilities);
}
@Test
public void testApp() {
// Locate elements and perform actions
MobileElement element =
driver.findElementById("com.example:id/element_id");
element.click();
MobileElement inputField =
driver.findElementById("com.example:id/input_field");
inputField.sendKeys("Hello,
Appium!");
}
@After
public void tearDown() {
// Close the app and stop the driver
if (driver != null) {
driver.quit();
}
}
}
Running the Test
bash
Copy
code
mvn test
Locating elements on a mobile app is
similar to web automation in Selenium. Some common locators used in Appium are:
Appium Desktop has an Inspector feature that lets you inspect elements in your app and easily find locators.
Advantages of Appium
getElementById()
method in web development.Example (Java):
MobileElement
element
= driver.findElementById(
"com.example:id/submitButton");
android.widget.Button
, android.widget.TextView
, etc.Example (Java):
MobileElement
element
= driver.findElementById(
"com.example:id/submitButton");
Example:
MobileElement
element
= driver.findElementByXPath(
"//android.widget.TextView[@text='Submit']");
driver.findElementByXPath(
"//*[text()='Submit']");
driver.findElementByXPath(
"//*[@content-desc='login_button']");
4.By Accessibility ID
Example:
MobileElement
element
= driver.findElementByAccessibilityId(
"submitButton");
5. By Name (iOS Only)
- Usage: This locator is primarily used in iOS applications, where you can refer to elements by their visible text or labels.
- When to Use: Use this for simple elements where you need to find an element by its display name
Example:
MobileElement
element
= driver.findElementByName(
"Submit");
Example:
MobileElement
element
= driver.findElementByAndroidUIAutomator(
"new UiSelector().text(\"Submit\")");
- Example of UIAutomator
driver.findElementByAndroidUIAutomator(
"new UiSelector().text(\"Submit\")");
driver.findElementByAndroidUIAutomator(
"new UiSelector().resourceId(\"com.example:id/submitButton\")");
7.
By iOS Class Chain (iOS Only)Example:
MobileElement
element
=
driver.findElementByIosClassChain(
"**/XCUIElementTypeButton[`label == 'Submit'`]");