Integration of Appium Tests with TestNG

 

To integrate Appium with TestNG for automating an Android Calculator app, you need to:


Prerequisites:

  • Java (JDK 11+)
  • Appium Desktop or Appium server installed and running
  • Android Emulator or real device connected
  • Appium Java client added to the project
  • TestNG configured in your project (typically via Maven)



Maven Dependencies:


<dependencies>
    <!-- Appium Java Client -->
    <dependency>
        <groupId>io.appium</groupId>
        <artifactId>java-client</artifactId>
        <version>8.5.1</version>
    </dependency>

    <!-- TestNG -->
    <dependency>
        <groupId>org.testng</groupId>
        <artifactId>testng</artifactId>
        <version>7.9.0</version>
        <scope>test</scope>
    </dependency>
</dependencies>



Java Test Script with TestNG Integration:


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 org.testng.Assert;
import org.testng.annotations.*;

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

public class CalculatorTest {

    private AndroidDriver<MobileElement> driver;

    @BeforeClass
    public void setup() throws MalformedURLException {
        DesiredCapabilities caps = new DesiredCapabilities();
        caps.setCapability(MobileCapabilityType.PLATFORM_NAME, "Android");
        caps.setCapability(MobileCapabilityType.DEVICE_NAME, "Android Emulator");
        caps.setCapability(MobileCapabilityType.AUTOMATION_NAME, "UiAutomator2");
        caps.setCapability("appPackage", "com.android.calculator2");
        caps.setCapability("appActivity", "com.android.calculator2.Calculator");
        caps.setCapability(MobileCapabilityType.NO_RESET, true);

        driver = new AndroidDriver<>(new URL("http://localhost:4723/wd/hub"), caps);
    }

    @Test
    public void testAddition() {
        driver.findElementById("digit_2").click();
        driver.findElementById("op_add").click();
        driver.findElementById("digit_3").click();
        driver.findElementById("eq").click();

        String result = driver.findElementById("result").getText();
        Assert.assertEquals(result, "5");
    }

    @AfterClass
    public void tearDown() {
        if (driver != null) {
            driver.quit();
        }
    }
}


How to Run:

  • Ensure the Appium server is running (localhost:4723)

  • Make sure your Android device/emulator is active and unlocked

  • Right-click the test file and choose Run As → TestNG Test