
Guide to Appium Sample Test Code

Understanding Appium
Before we dive into the code, let’s briefly understand what Appium is and why it stands out in the mobile testing landscape. Appium is an automation tool that supports both Android and iOS platforms and allows testers to write tests using their preferred programming languages. With Appium, you can test native, hybrid, and mobile web applications, making it a versatile choice for mobile app testing.Setting Up Your Environment
To begin your journey with Appium, you’ll need to set up your testing environment. Ensure you have the following components installed:- Appium Server: The core component that facilitates communication between your test code and the mobile device or emulator.
- Mobile Device Emulator or Physical Device: Appium supports testing on both emulators and real devices.
- Appium Client Libraries: Depending on your programming language of choice (Java, Python, JavaScript, etc.), you’ll need to install the corresponding client library to interact with the Appium server.
Writing Your First Appium Test Code
For the purpose of this example, we’ll use Java as the programming language. Below is a simple Appium test code snippet that opens the Calculator app on an Android emulator and performs a basic addition operationimport io.appium.java_client.AppiumDriver; import io.appium.java_client.MobileElement; import io.appium.java_client.android.AndroidDriver; import org.openqa.selenium.remote.DesiredCapabilities; import java.net.URL; import java.util.concurrent.TimeUnit; public class AppiumSampleTest { public static void main(String[] args) { // Set the desired capabilities for the Android emulator DesiredCapabilities caps = new DesiredCapabilities(); caps.setCapability("platformName", "Android"); caps.setCapability("deviceName", "emulator-5554"); caps.setCapability("appPackage", "com.android.calculator2"); caps.setCapability("appActivity", "com.android.calculator2.Calculator"); // Initialize the Appium driver try { AppiumDriver < MobileElement > driver = new AndroidDriver < > (new URL("http://127.0.0.1:4723/"), caps); // Perform a basic addition operation MobileElement digit2 = driver.findElementById("com.android.calculator2:id/digit_2"); MobileElement plusButton = driver.findElementByAccessibilityId("plus"); MobileElement digit3 = driver.findElementById("com.android.calculator2:id/digit_3"); MobileElement equalsButton = driver.findElementByAccessibilityId("equals"); digit2.click(); plusButton.click(); digit3.click(); equalsButton.click(); // Wait for a few seconds to observe the result TimeUnit.SECONDS.sleep(3); // Close the app driver.closeApp(); } catch (Exception e) { System.out.println("An error occurred: " + e.getMessage()); } } }
Need testing? – Try RobotQA and Start Testing on Real Devices. Start Free Trial
Explanation of the Code:
- The code begins by setting up desired capabilities, which include information about the platform, device, app package, and activity.
- It then initializes the Appium driver using the desired capabilities and establishes a connection to the Appium server.
- The test performs a basic addition operation using the Calculator app on the Android emulator.
- After the test, the app is closed, and any potential errors are caught and displayed.