We discussed the Python setup for Appium in the last week and it’s time to move forward and see how to set up Appium things with Java. Furthermore, we’ve been covering the basic setup, installing all required bits and pieces, and preparing the environment for the first test runs in our Appium blog series.
The beauty of Java is in its extensibility and long-existing history. Most developers these days have at least some experience in Java, which makes it easy to approach. You should check the Appium Java-client’s README if you haven’t already!
Before you attempt to launch the Bitbar Java samples, remember to check that all the desired capabilities are correct. It may be a good idea to also set as many capabilities in Environment Variables as possible to reduce the amount of editing when creating additional test scripts. Frankly, this will save a lot of time and also make your test run launching a way more robust (no typos or flaky issues with the script config). Download our free Appium beginner’s guide to learn basic tips and tricks of setting up a proper Appium testing environment.
TIP! Put as many desired capabilities in Environment Variables as possible. It saves time and reduces errors due wrong configuration or typos in desired caps.
Similar to the Python sample, our Java implementation uses the Environment Variables for capabilities such as
testdroid_password, app file location and Appium server:
private static final String TARGET_APP_PATH = "../../../apps/builds/BitbarSampleApp.apk";
private static final String TESTDROID_SERVER = "http://appium.testdroid.com";
String testdroid_username = env.get("TESTDROID_USERNAME");
String testdroid_password = env.get("TESTDROID_PASSWORD");
Notice how we create the test class by extending to another class called BaseTest.java:
public class SampleAppiumTest extends com.testdroid.appium.BaseTest.
Thanks to this extension, we get to use some of our self-created code behind the scenes of the test class.
Let’s take a closer look at the BaseTest.java
Since we have both Android and iOS samples combined in the same Java project, we decided to create a convenient class called BaseTest.java. The class includes functions for both application upload to Bitbar Testing and taking screenshots. The screenshot implementation of the Appium Java-client is based on Selenium Webdriver’s TakesScreenshot interface, which is basically too cumbersome to write multiple times.
While writing your own tests, extending to use such a convenience class can be a real time saver. This is what we do a lot also in our Image Recognition solution, which we will be covering later in this blog series.
Typically, our samples are built for Maven build automation tool. This tool is really handy as it automates downloading all required dependencies before building and executing the project. All the required dependencies and other project settings are described in the
Running Appium Java tests in the cloud
You can run tests from your IDE or directly from the command line using maven:
>mvn clean test -Dtest=SampleAppiumTest
or to be more precise:
>mvn clean test -Dtest=com.testdroid.appium.android.sample.SampleAppiumTest
or run all the tests:
>mvn clean test
Useful Tip: When not to kill Webdriver session
When you have more than one test case to run, you should be aware of the fact that every test session to Bitbar Testing can use only one instance of the Appium Webdriver. If you use the driver.quit(), the current session towards the cloud will also end.
In other words, bundling multiple test cases to one test run session requires you to keep using the same Webdriver instance in each of your test cases. To make sure each of your test cases starts off from a clean slate, take a look at the driver command resetApp.
If you’re unsure of your possibilities with the java-client, you may find some ideas by checking the Appium Java-client API documentation.
See you again on Wednesday! Happy Testing!