Home » #Technology » Automating Mobile Test Cases: A Beginner’s Guide to Appium

Automating Mobile Test Cases: A Beginner’s Guide to Appium

Leading with 18 years of proven success in the tech corporate industry, Today’s Mobile applications are expected to function flawlessly across both Android and iOS platforms. Automating mobile testing not only ensures consistent quality but also streamlines the testing process. Appium, a powerful open-source framework, makes it possible to automate tests on both Android and iOS using a single codebase. This tech post, let’s understand the process of automating your first mobile test case with Appium, showing how you can efficiently test Android and iOS apps.

What is Appium?

Appium is a cross-platform mobile automation tool that allows developers to automate tests on both Android and iOS apps. It supports native, hybrid, and mobile web applications and does not require any app modifications for automation.

Key Features of Appium:
  • Cross-Platform Support: Test Android and iOS apps with the same codebase.
  • Language Flexibility: Supports several programming languages, including Java, Python, and JavaScript.
  • No App Changes Needed: Appium doesn’t require any changes in your app code for automation.
  • Open Source: It’s free, with an active community providing continuous improvements and support.

Setting Up Appium for Android and iOS

To get started with Appium, you’ll need to set up a few tools and configure the environment for both Android and iOS automation.

Prerequisites:
  1. Install Appium: You can install Appium via npm or download the Appium desktop client.
   npm install -g appium
  1. Java: Ensure the Java Development Kit (JDK) is installed.
  2. Android Studio: Install Android Studio and configure Android SDK for Android app testing.
  3. Xcode: Install Xcode for testing iOS apps on macOS.
Appium Clients and Libraries:

Appium provides client libraries in various programming languages. In this guide, we’ll use Java for writing our test cases.

To include Appium in a Maven project, add the following dependency in your pom.xml:

<dependency>
    <groupId>io.appium</groupId>
    <artifactId>java-client</artifactId>
    <version>7.4.0</version>
</dependency>

Writing Your First Test Case with Appium

In this section, we’ll automate a basic login test case for both Android and iOS platforms using Appium.

Example Scenario: Automating a Login Test Case

We will write a test case that automates the following steps:

  1. User enters a valid username and password.
  2. User clicks the login button.
  3. The app navigates to the dashboard page, and we verify if the dashboard is displayed.
Step 1: Set Up Desired Capabilities

You need to configure the desired capabilities to define the mobile platform, app details, and the device information for Android or iOS.

For Android:

DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("platformName", "Android");
capabilities.setCapability("platformVersion", "10.0");
capabilities.setCapability("deviceName", "Android Emulator");
capabilities.setCapability("appPackage", "com.example.app");
capabilities.setCapability("appActivity", "com.example.app.MainActivity");
capabilities.setCapability("automationName", "UiAutomator2");

For iOS:

DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("platformName", "iOS");
capabilities.setCapability("platformVersion", "14.0");
capabilities.setCapability("deviceName", "iPhone Simulator");
capabilities.setCapability("app", "/path/to/your.app");
capabilities.setCapability("automationName", "XCUITest");
Step 2: Writing the Test Script

Here’s the complete test case to automate the login functionality across both Android and iOS platforms:

import io.appium.java_client.AppiumDriver;
import io.appium.java_client.MobileElement;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.ios.IOSDriver;
import org.openqa.selenium.By;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import java.net.MalformedURLException;
import java.net.URL;

public class LoginTest {
    private AppiumDriver<MobileElement> driver;

    @BeforeClass
    public void setup() throws MalformedURLException {
        DesiredCapabilities capabilities = new DesiredCapabilities();
        // Set this flag based on whether you are testing on Android or iOS
        boolean isAndroid = true;

        if (isAndroid) {
            capabilities.setCapability("platformName", "Android");
            capabilities.setCapability("platformVersion", "10.0");
            capabilities.setCapability("deviceName", "Android Emulator");
            capabilities.setCapability("appPackage", "com.example.app");
            capabilities.setCapability("appActivity", "com.example.app.MainActivity");
            capabilities.setCapability("automationName", "UiAutomator2");
            driver = new AndroidDriver<>(new URL("http://localhost:4723/wd/hub"), capabilities);
        } else {
            capabilities.setCapability("platformName", "iOS");
            capabilities.setCapability("platformVersion", "14.0");
            capabilities.setCapability("deviceName", "iPhone Simulator");
            capabilities.setCapability("app", "/path/to/your.app");
            capabilities.setCapability("automationName", "XCUITest");
            driver = new IOSDriver<>(new URL("http://localhost:4723/wd/hub"), capabilities);
        }
    }

    @Test
    public void testLogin() {
        // Enter username
        driver.findElement(By.id("usernameField")).sendKeys("testUser");
        // Enter password
        driver.findElement(By.id("passwordField")).sendKeys("password123");
        // Click login button
        driver.findElement(By.id("loginButton")).click();
        // Verify dashboard
        MobileElement dashboardElement = driver.findElement(By.id("dashboard"));
        Assert.assertTrue(dashboardElement.isDisplayed(), "Dashboard is not displayed.");
    }

    @AfterClass
    public void tearDown() {
        if (driver != null) {
            driver.quit();
        }
    }
}
Step 3: Run the Test

Ensure Appium is running on your system by using the following command:

appium

Now, run the test in your IDE. Appium will handle the automation process for both Android and iOS based on the configuration you provided.

Best Practices for Mobile Test Automation with Appium

  1. Use Explicit Waits: Mobile environments are slower compared to desktop apps, so always use explicit waits to handle dynamic content.
  2. Organize Tests Using Page Object Model (POM): Structuring your tests using POM improves maintainability and scalability.
  3. Test on Real Devices: While emulators/simulators are useful for initial testing, always test on real devices for accurate results.
  4. Keep Tests Modular: Write modular tests by separating your test logic from test data, making the tests more reusable.

My TechAdvice: Appium simplifies mobile test automation by providing cross-platform support, making it possible to automate tests for both Android and iOS apps using the same codebase. Whether you’re testing native, hybrid, or mobile web apps, Appium provides flexibility and scalability, ensuring consistent quality across platforms. Get started with Appium today, and start automating your mobile app testing to save time and improve overall quality!

#AskDushyant
#TechConcept #TechAdvice #Appium #Testing #SoftwareTesting
Note: The example and pseudo code is for illustration only. You must modify and experiment with the concept to meet your specific needs.

Leave a Reply

Your email address will not be published. Required fields are marked *