Getting Started with Appium - Java

Posted Sep 2nd, 2021

This comprehensive how-to guide was written by Appium project lead Jonathan Lipps. It explores everything you need to get started including issues specific to the Java client and resources to use once your tests are up and running.

Getting started with Appium can seem incredibly easy -- just follow a tutorial online. The online tutorial might explain how to download and install the tool, but it might not be your operating system … or it might be out of date. Once you have Appium installed, most tutorials are trivial; they'll show the testing equivalent for "hello, world." What to do next, what good tests look like, how to structure the tests, get them to run together, connect them to continuous integration, and all the hard work of getting the tests to be part of a software engineering process, all of that is left to the reader to figure out. The online documentation, written by an Open Source project, is likely to be sparse, while the few physical books on the topic are likely out of date before they are published.

Sauce Labs hired Jonathan Lipps, the project lead of the Appium Project and a Sauce employee, and asked him to help solve this problem. The result is our 62-page guide to getting started with Appium using the Java Programming language.

Here's a quick summary of what you'll learn, along with one of the examples.

What is Appium?

Appium is an open-source tool that lives on top of automation tools for Mobile devices, creating a single standard interface for programmers. That interface is extremely familiar to Selenium, creating a logical progression from Selenium to Appium, making the change easy. The interface between iOS and Android is the same, with perhaps a difference in locators. That means the programmers can write essentially the same code, with a lookup table for locators. Like Selenium, Appium has a driver to connect to the device and a client that contains code libraries for all modern programming languages. That means programmers can write code in Java, C#, Ruby, Python, or a host of other languages, likely including the same language they use for writing the production code.

The client for Appium can be a handheld device, an emulator on a local machine, or something in the cloud. The cloud device can be an emulator or Real Device in the Cloud. Switching between the devices is as easy as a config flag; it is common to debug locally and run under Continuous Integration in the cloud.

Getting set up

Appium is free and open, relying on a series of libraries, development toolkits, install tools, all of them free, all under continuous development. The eBook describes not only how to install the tool for Macintosh, Linux and Windows, but also what versions of the libraries are guaranteed to work together. The examples use the IntelliJ development environment, which runs on all major operating systems, and the Gradle Build tool, with a sample application in Github. That means there is nothing to buy and you have everything you need to build a real environment -- not just concepts.

Writing your first Appium test

The tutorial includes Appium Desktop, which provides the tools to both get a server and inspect the application to get locators. The tutorial does more than provide sample code in Java. it explains how to write that code along with a guiding design philosophy. For today, though, here's a simple test program in Appium. Note the core jUnit-like methods: setUp and tearDown with the core test of testLogin().

public class IOSLoginTest {
        IOSDriver driver;
        @Before
        public void setUp() throws MalformedURLException, URISyntaxException {
        URL appiumUrl = new URL("http://localhost:4723/wd/hub");
        URL resource = getClass().getClassLoader().getResource("apps/TheApp-v1.2.1.app.zip");
        File app = Paths.get(resource.toURI()).toFile();
        DesiredCapabilities caps = new DesiredCapabilities();
        caps.setCapability("platformName", "iOS");
        caps.setCapability("platformVersion", "11.2");
        caps.setCapability("deviceName", "iPhone 7");
        caps.setCapability("app", app);
driver = new IOSDriver(appiumUrl, caps);
}
        @After
        public void tearDown() {
                try {
                        driver.quit();
                } catch (Exception ignore) {}
        }
        @Test
        public void testLogin() {
        driver.findElement(MobileBy.AccessibilityId("Login Screen")).click();
        driver.findElement(By.xpath("//XCUIElementTypeTextField[@
        name=\"username\"]")).sendKeys("alice");
        driver.findElement(By.xpath("//XCUIElementTypeSecureTextField[@
        name=\"password\"]")).sendKeys("mypassword");
        driver.findElement(MobileBy.AccessibilityId("loginBtn")).click();
        driver.findElement(MobileBy.AccessibilityId("You are logged in as alice"));
        } 
}

After the hello world example, the book explains page objects as a way to structure tests based on re-usable elements of workflow, instead of hard-coded values and links. This separation from the abilities of the page (where the "page" is an object with methods and data) and the writing of the business logic test to exercise that page, creates easier-to-read, reusable tests that are less brittle. That results in a login test that looks like this, where setup and teardown are pushed into a "BaseTest" class.

public class IOSLoginTest extends BaseTest {
        @Test
        public void testLogin() {
                final String username = “alice”;
                final String password = “mypassword”;
               mainView.navToLogin();
                loginView.login(username, password);
                String loggedInUsername = loggedInView.getLoggedInUsername();
                Assert.assertEquals(loggedInUsername, username);
                } 
}

Gradle, the Cloud, and Continuous Integration

Once the code runs in an IDE, the next step is to push it to the command line, run it on a schedule as part of Continuous Integration (CI) and get the results back into CI. The eBook takes you how to do that step-by-step, along with tips about how to abstract the server into a factory, allowing you to switch between operating systems, devices, local and the cloud with parameters or environmental variables. The paper even includes advice on how to set up the test run under Jenkins, including either local or using the Jenkins plugin for Sauce Labs. The whitepaper ends on additional resources and where to go for support.

Get started!

This guide draws upon Jonathan’s unique insights and knowledge along with that of maintainers and contributors of the Appium Java client. It details out various software, versions used and common issues to help you reproduce code samples used in the guide. It also has links to handy resources you may need once your tests are up and running, like a reference card for Appium setup and commands.

Fill in the form at right and click 'Download' to access this comprehensive guide and get started on your Appium testing journey!

View white paper

Topics

AppiumMobile TestingGetting Started