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@Before2public void setUp() throws Exception {3// set up appium against a local application4File appDir = new File(System.getProperty("user.dir"), "../../../apps/TestApp/build/Release-iphonesimulator");56File app = new File(appDir, "TestApp.app");7DesiredCapabilities capabilities = new DesiredCapabilities();8capabilities.setCapability(CapabilityType.BROWSER_NAME, "iOS");9capabilities.setCapability(CapabilityType.VERSION, "6.0");10capabilities.setCapability(CapabilityType.PLATFORM, "Mac");1112//tell Appium where the location of the app is13capabilities.setCapability("app", app.getAbsolutePath());1415//create a RemoteWebDriver, the default port for Appium is 472316driver = new RemoteWebDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);17}181920
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@Before2public void setUp() throws Exception {3String sauceUserName = "YOUR_SAUCE_USERNAME";4String sauceAccessKey = "YOUR_SAUCE_ACCESS_KEY";56DesiredCapabilities capabilities = new DesiredCapabilities();7capabilities.setCapability(CapabilityType.BROWSER_NAME, "iOS 6.0");8capabilities.setCapability("device", "iPhone Simulator");9capabilities.setCapability(CapabilityType.PLATFORM, "Mac 10.8");1011//zip file containing your app to be tested12capabilities.setCapability("app", "http://appium.s3.amazonaws.com/TestApp6.0.app.zip");1314driver = new RemoteWebDriver(new URL(MessageFormat.format("http://{0}:{1}@ondemand.saucelabs.com:80/wd/hub", sauceUserName, sauceAccessKey)),15capabilities);16}
The tests themselves are written just like regular Selenium tests, eg.
1@Test2public void example() throws Exception {34// find an element by tag name5WebElement button = driver.findElement(By.tagName("button"));6button.click();78// get the value of the element9WebElement texts = driver.findElement(By.tagName("staticText"));10assertEquals(texts.getText(), "some expected value");11}
The Java sample tests for Appium include the following classes:
SimpleTest - runs a test against a simple TestApp
SauceTest - runs the same test using Sauce Labs
UICatalogTest - runs a series of tests against the UICatalog sample application
Please let us know if you have any questions or comments about running Appium tests with Java!