How to Use Appium Java Client for Your Automated Testing

Apr 28, 2022 | Programming

Appium is a powerful tool used for automating mobile applications, and with the Appium Java Client, you can write Java tests that conform to the WebDriver Protocol. Maven Central and Javadocs provide you with essential resources to navigate through the Appium framework effectively. Let’s dive in to understand how to set it up and effectively use it in your projects!

Migration from Older Versions

Before you dive into using the Appium Java Client, it’s crucial to understand any migrations from previous versions:

  • Version 8 to 9: The client now only supports Java 11 and above. Visit the v8 to v9 Migration Guide for further details.
  • Version 7 to 8: Significant changes were introduced in version 8 that may require code updates. Refer to the v7 to v8 Migration Guide.

Adding Appium Java Client to Your Test Framework

To use the Appium Java Client, you need to add it to your test framework. Here’s how to do it for both stable and beta/snapshot versions:

Stable Version

Maven



    io.appium
    java-client
    $version.your.require
    test

Gradle


dependencies {
    testImplementation 'io.appium:java-client:$version.your.require'
}

Beta/Snapshots

Maven



    
        jitpack.io
        https://jitpack.io
    


    com.github.appium
    java-client
    latest commit ID from master branch

Gradle


allprojects {
    repositories {
        ...
        maven { url 'https://jitpack.io' }
    }
}

dependencies {
    implementation 'com.github.appium:java-client:latest commit id from master branch'
}

Driver Support

The Appium Java Client supports various drivers, each catering to different platforms:

Example Usage

Now, let’s see how to execute tests using specific drivers. Think of it like ordering your favorite dish in a restaurant – you tell the server what you’d like and they ensure it’s prepared just how you like it!

UiAutomator2 Example


UiAutomator2Options options = new UiAutomator2Options()
    .setUdid(123456)
    .setApp("homemyapp.apk");
AndroidDriver driver = new AndroidDriver(
    new URL("http://127.0.0.1:4723"), options);
try {
    WebElement el = driver.findElement(AppiumBy.xpath("Button"));
    el.click();
    driver.getPageSource();
} finally {
    driver.quit();
}

XCUITest Example


XCUITestOptions options = new XCUITestOptions()
    .setUdid(123456)
    .setApp("homemyapp.ipa");
IOSDriver driver = new IOSDriver(
    new URL("http://127.0.0.1:4723"), options);
try {
    WebElement el = driver.findElement(AppiumBy.accessibilityId("myId"));
    el.click();
    driver.getPageSource();
} finally {
    driver.quit();
}

Generic Driver Usage


BaseOptions options = new BaseOptions()
    .setPlatformName("myplatform")
    .setAutomationName("mydriver")
    .amend("mycapability1", "capvalue1")
    .amend("mycapability2", "capvalue2");
AppiumDriver driver = new AppiumDriver(
    new URL("http://127.0.0.1:4723"), options);
try {
    WebElement el = driver.findElement(AppiumBy.className("myClass"));
    el.click();
    driver.getPageSource();
} finally {
    driver.quit();
}

Troubleshooting

If you encounter issues during your testing process, here are some common problems and their fixes:

  • InaccessibleObjectException: This can arise if you’re using Java 16 or greater. The Appium Java client uses reflective access to ensure functionality. To resolve, consider following Oracle’s tutorial or visit existing issues for possible solutions. You may need to use `–add-exports` and `–add-opens` command-line arguments to explicitly allow access.
  • Environment Variable Issues: These occur if the Appium server is started directly from your framework code. Check the Appium Environment Troubleshooting document to debug and fix these issues.

For more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.

Conclusion

At fxis.ai, we believe that such advancements are crucial for the future of AI, as they enable more comprehensive and effective solutions. Our team is continually exploring new methodologies to push the envelope in artificial intelligence, ensuring that our clients benefit from the latest technological innovations.

Happy testing!

Stay Informed with the Newest F(x) Insights and Blogs

Tech News and Blog Highlights, Straight to Your Inbox