Back to Resources

Blog

Posted April 3, 2013

Mobile Test Automation in Java with Appium

quote

Appium is an open source test automation tool which allows you to easily write functional tests that automate iOS and Android mobile apps. One big advantage Appium has over other mobile test automation tools is that Appium tests can be written in any language that has a Selenium client library, including Python, Ruby, Node.js, and, perhaps most interesting to mobile developers, Objective-C and Java.

In this post we'll walk through the steps involved in testing the iOS sample apps using the JUnit Java example tests (we also have created TestNG example tests too).

To start, fork and clone Appium from https://github.com/appium/appium, and follow the installation instructions to set up Appium in your environment.

Download and build the sample projects by running the following from the command line:

grunt getSampleCode grunt buildApp:TestApp grunt buildApp:UICatalog

Once the sample projects have have been built, you can then start Appium by running the following:

grunt appium

Change the working directory to the sample-code/examples/java/junit directory, and run the tests by executing:

mvn test

Or run a single test by executing: mvn -Dtest=com.saucelabs.appium.SimpleTest test

A Java Appium test is much the same as a Selenium test...you create a RemoteWebDriver instance by specifying some DesiredCapabilities, e.g.:

1
@Before
2
public void setUp() throws Exception {
3
// set up appium against a local application
4
File appDir = new File(System.getProperty("user.dir"), "../../../apps/TestApp/build/Release-iphonesimulator");
5
6
File app = new File(appDir, "TestApp.app");
7
DesiredCapabilities capabilities = new DesiredCapabilities();
8
capabilities.setCapability(CapabilityType.BROWSER_NAME, "iOS");
9
capabilities.setCapability(CapabilityType.VERSION, "6.0");
10
capabilities.setCapability(CapabilityType.PLATFORM, "Mac");
11
12
//tell Appium where the location of the app is
13
capabilities.setCapability("app", app.getAbsolutePath());
14
15
//create a RemoteWebDriver, the default port for Appium is 4723
16
driver = new RemoteWebDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
17
}
18
19
20

Tests can also be written to be run against Sauce Labs. To do this, just update your tests to reference a zip file containing your app, and update your RemoteWebDriver instance to point to ondemand.saucelabs.com, eg:

1
@Before
2
public void setUp() throws Exception {
3
String sauceUserName = "YOUR_SAUCE_USERNAME";
4
String sauceAccessKey = "YOUR_SAUCE_ACCESS_KEY";
5
6
DesiredCapabilities capabilities = new DesiredCapabilities();
7
capabilities.setCapability(CapabilityType.BROWSER_NAME, "iOS 6.0");
8
capabilities.setCapability("device", "iPhone Simulator");
9
capabilities.setCapability(CapabilityType.PLATFORM, "Mac 10.8");
10
11
//zip file containing your app to be tested
12
capabilities.setCapability("app", "http://appium.s3.amazonaws.com/TestApp6.0.app.zip");
13
14
driver = new RemoteWebDriver(new URL(MessageFormat.format("http://{0}:{1}@ondemand.saucelabs.com:80/wd/hub", sauceUserName, sauceAccessKey)),
15
capabilities);
16
}

The tests themselves are written just like regular Selenium tests, eg.

1
@Test
2
public void example() throws Exception {
3
4
// find an element by tag name
5
WebElement button = driver.findElement(By.tagName("button"));
6
button.click();
7
8
// get the value of the element
9
WebElement texts = driver.findElement(By.tagName("staticText"));
10
assertEquals(texts.getText(), "some expected value");
11
}

The Java sample tests for Appium include the following classes:

Please let us know if you have any questions or comments about running Appium tests with Java!

Published:
Apr 3, 2013
Share this post
Copy Share Link
© 2023 Sauce Labs Inc., all rights reserved. SAUCE and SAUCE LABS are registered trademarks owned by Sauce Labs Inc. in the United States, EU, and may be registered in other jurisdictions.