Appium with Java: Getting Started to Run Automated Tests
By Neha Vaidya, Community Contributor - April 25, 2022
Introduction to Appium
Appium is an open-source automation testing framework for applications supported by various platforms like iOS, Android, macOS, and Windows. It enables the QA to write test scripts in different programming languages such as JavaScript, Java, Ruby, Python, PHP, and C#. It also offers cross-platform testing wherein a single API can be used for both Android and iOS platforms.
Appium makes automated mobile app testing a lot more efficient by offering scalability and flexibility. Mobile testing using Appium has gained traction in the mobile application industry with its extensive features in a short period.
Introduction to Java
Java is one of the best programming languages. It is extremely popular in the market because of its impressive features. Java is a robust, high-level, object-oriented, and secure programming language. It is used to develop mobile apps, web apps, desktop apps, games, and much more.
Java is used for:
- Designing web applications and creating Android applications.
- Developing software tools too
- Game development
- Writing various automation test scripts
Appium Automation with Java: What makes it perfect?
The key motto of Appium is to provide flexibility for mobile testers and QA teams to build the tests in a framework that fits into their development processes.
Teams adopting Agile methodology for mobile testing can easily integrate Appium into the testing process. It benefits both QAs and mobile developers because Appium tests are easy to consider in the development source code repository.
When test cases are available, developers will try to leverage Appium as part of their development process. Therefore, the whole team can utilize Appium in different instances to develop and test more efficiently. Moreover, DevOps initiatives benefit by improving efficiency.
During Agile development, one of the main reasons that Appium is so popular with enterprise mobility teams is that Appium’s different client libraries support multiple programming languages. This robust support offers flexibility to choose the most suitable programming language for teams while building automated tests.
Considering the support for multiple languages by Appium choosing Java becomes obvious since Java is the most common language used by mobile testers and quality assurance professionals for automation. The Appium Java client currently has the most robust Appium functionality.
Other benefits of automating with Java are as follows:
- Supports Cross-platform operating systems (Mac, Windows, Linux)
- Test cases can be run on Eclipse or IntelliJ, which developers were already using to build Java applications.
- Tools used when writing tests are free and have mature functionality
Running Appium Test cases
To run Appium test cases on your desktop, it is essential to install and set up Appium. Learn how to install, set up, and run the first Appium Test Script with the help of the following article.
Read More: How to Run Your First Appium Test Script
Automating Appium tests using Java with Browserstack App Automate
BrowserStack App Automate lets you test native and hybrid mobile applications using the Appium automation framework on thousands of real Android and iOS devices. It’s often very easy to run Appium tests written in Java on real Android and iOS devices on BrowserStack.
Note: If you are new to BrowserStack, refer to this document on executing your first Appium test case on the BrowserStack cloud. Run Appium tests with Java
Setup
You will need to have a BrowserStack username and access key. Get your access credentials by signing up for a free trial or purchasing a plan. After that, launch Browserstack App Automate.
It is necessary to have Appium’s Java client library installed. If you do not have one, please add the Maven dependency below to your project in IDE. In this case, the IDE being used is Eclipse.
// Maven users can add this dependency to project's POM <dependency> <groupId>io.appium</groupId> <artifactId>java-client</artifactId> <version>7.0.0</version> </dependency>
Also, ensure that you have access to an Android app (.apk or .aab file) or an iOS app (.ipa file)
/* Note the "app_url" value for the sample app. This value uniquely identifies the app on BrowserStack. */ {"app_url":"bs://c700ce60cf13ae8ed97705a55b8e022f13c5827c"} /* In your test script, use this "app_url" value to specify the application under test using the "app" capability. During test execution, the sample app will automatically be installed and launched on the device being tested. */ caps.setCapability("app", "bs://c700ce60cf13ae8ed97705a55b8e022f13c5827c")
If you do not have an application, please use the sample android or iOS app. In this case, let’s use an Android application.
As a next step, configure the code, as shown in the screenshot below. In this case, let’s select a real Google Nexus 6 device with Android from the drop-down as shown below.
You can also copy the code snippet from the App Automate dashboard to execute the test case.
Execution
package android; import java.net.URL; import java.util.List; import java.util.function.Function; import java.net.MalformedURLException; import io.appium.java_client.MobileBy; import io.appium.java_client.android.AndroidDriver; import io.appium.java_client.android.AndroidElement; import org.openqa.selenium.support.ui.ExpectedConditions; import org.openqa.selenium.support.ui.WebDriverWait; import org.openqa.selenium.WebDriver; import org.openqa.selenium.remote.DesiredCapabilities; public class BrowserStackSample { public static void main(String[] args) throws MalformedURLException, InterruptedException { DesiredCapabilities caps = new DesiredCapabilities(); // Set your access credentials caps.setCapability("browserstack.user", "your_username"); caps.setCapability("browserstack.key", "your_key"); // Set URL of the application under test caps.setCapability("app", "bs://c700ce60cf13ae8ed97705a55b8e022f13c5827c"); // Specify device and os_version for testing caps.setCapability("device", "Google Nexus 6"); caps.setCapability("os_version", "5.0"); // Set other BrowserStack capabilities caps.setCapability("project", "First Java Project"); caps.setCapability("build", "browserstack-build-1"); caps.setCapability("name", "first_test"); // Initialise the remote Webdriver using BrowserStack remote URL // and desired capabilities defined above AndroidDriver<AndroidElement> driver = new AndroidDriver<AndroidElement>( new URL("http://hub.browserstack.com/wd/hub"), caps); // Test case for the BrowserStack sample Android app. // If you have uploaded your app, update the test case here. AndroidElement searchElement = (AndroidElement) new WebDriverWait(driver, 30).until( ExpectedConditions.elementToBeClickable( MobileBy.AccessibilityId("Search Wikipedia"))); searchElement.click(); AndroidElement insertTextElement = (AndroidElement) new WebDriverWait(driver, 30).until( ExpectedConditions.elementToBeClickable( MobileBy.id("Google"))); insertTextElement.sendKeys("BrowserStack"); Thread.sleep(5000); List<AndroidElement> allProductsName = driver.findElementsByClassName( "android.widget.TextView"); assert(allProductsName.size() > 0); // Invoke driver.quit() after the test is done to indicate that the test is completed. driver.quit(); } }
Now, execute the first Appium test on BrowserStack. Open the terminal/command prompt and navigate to the folder containing your test script on your local machine. Build and run the test script like any other Java program using your project’s preferred build tools such as Maven and Gradle.
# Using Maven mvn test -P <android-first-test> or mvn compile exec:java -Dexec.mainClass="android.BrowserStackSample"
After executing the test case, view the results on the BrowserStack dashboard. That’s how it works.
BrowserStack App Automate offers cloud-based access to both the latest and legacy devices (Android, iOS, and Windows) installed with real mobile devices. App Automate also requires no additional setup, helping testers save precious time and meet their deadlines that much faster. Run Appium tests on thousands of real devices so that the app becomes truly cross-platform compatible and user-friendly.
Since users demand high-functioning and engaging apps, Appium testing is an absolute requirement before releasing any apps. By running Appium tests on real Android devices, testers can ensure that apps are working as expected in real user conditions. Run as many tests as possible on as many real Android devices as possible to offer a consistently optimal user experience.