Appium vs. Espresso: Which Framework to Use for Automated Android Testing
If you are trying to build a well-balanced test portfolio, UI testing has to be on your list. In fact, it represents the highest level of the famous test pyramid. But choosing the right tool for automating UI tests is hard because every solution on the market has its own peculiarities. Let’s take a look at Appium and Espresso, the two most popular automated frameworks for Android testing.
Meet Appium
Appium is an open-source test automation framework that allows creating tests for mobile web, hybrid and native applications. To automate your tests with Appium, you don’t even have to recompile or alter your code. Appium has a client/server architecture with its server written in Node.js with Selenium WebDriver API and client libraries (in Java, Ruby, Python, PHP, JavaScript and C#) with extensions to the WebDriver protocol. Check out our tips and tricks about Appium.
Appium’s Advantages
1. Support for multiple languages and tools
Because Appium uses JSON Wire Protocol for client/server communication, it allows writing clients in many different languages. Appium Web Driver’s clients are more “automation libraries” than simply sending HTTP requests to the server. Remarkably, you can even use Ruby, Python, PHP or C# for writing test scripts. Basically, with Appium, you are not limited in choosing your technology stack.
2. Cross-platform test cases
If you also need to test your iOS app, Appium has you covered: the test automation framework is cross-platform. This means that it allows running tests against multiple mobile platforms. What’s more important, you can reuse code across your Android and iOS test suites.
3. Similarity with Selenium
Appium’s functionality resembles the one of Selenium, a popular test automation framework for web applications. This makes it familiar and easy to use for everyone who has experience with Selenium, so if your team is working with it, adopting Appium for automated Android UI testing will come naturally.
Appium’s Disadvantages
1. Speed
Unfortunately, Appium is rather slow because of its architecture. It takes some time to start the server and then to communicate every action to it. So, don’t be surprised if your automated Android UI tests run with a bit of delay.
2. Flaky tests
Another disadvantage is that Appium lacks precision in its tests. Compared to using Espresso for automated Android testing, Appium’s automated UI testing is much flakier. In other words, Appium’s tests can fail or pass for the same configuration, causing pain to the engineers.
3. Difficult setup
Appium’s setup process for Android testing is somewhat complicated. It requires strong programming skills because you need to know how to configure the server, and it’s more complicated than setting up Espresso. Here’s a guide on how to get started with Appium.
4. Problems with locating elements
Another disadvantage of the tool is that Appium has difficulties locating elements and recognizing images. It just can’t do that automatically. So, your team will have to enter the elements’ positions manually.
5. No support of older Android versions
To make matters worse, Appium doesn’t support versions older than Android 4.2, which means that your tests won’t cover a large number of devices. Luckily, you can solve the issue if you combine Appium with Selendroid.
Appium Sample Test Code
# wait for hello sleep(3) textFields = driver.find_elements_by_tag_name('textField') assertEqual(textFields[0].get_attribute("value"), "Hello") # click sign-in button driver.find_elements_by_name('Sign in')[0].click() # find the text fields again, and enter username and password textFields = driver.find_elements_by_tag_name('textField') textFields[0].send_keys("twitter_username") textFields[1].send_keys("passw0rd") # click the Login button (the first button in the view) driver.find_elements_by_tag_name('button')[0].click() # sleep sleep(3) # click the first button with name "Compose" driver.find_elements_by_name('Compose')[0].click() # type in the tweet message driver.find_elements_by_tag_name('textField')[0].send_keys(”#Bitbar is awesome!") # press the Send button driver.find_elements_by_name('Send')[0].click() # exit driver.quit()
Meet Espresso
Espresso created by Google is a native framework for Android automated testing. The tool is a part of the Android SDK and is easy to use for native mobile development. Thanks to Espresso, you can create tests that are close to the Android app’s logic. Let’s see what it has to offer for automated UI testing of Android apps.
Espresso’s Advantages
1. Stability and speed
Overall, Espresso is much more stable than Appium for automated Android UI testing. In addition to that, the execution of test scripts is much faster. All thanks to Espresso’s automatic synchronization of UI elements and test actions: the framework detects when an element is available for testing and runs test commands accordingly.
2. Simple workflow and fast feedback
Espresso allows compiling automated Android UI tests into a separate APK. This means that the test suite will run next to the app on the device, which is very convenient. On top of that, because Espresso doesn’t require server communication, it provides feedback faster than Appium.
3. Compact API
Another advantage is that Espresso has a simple and lightweight API with three components: viewMatchers, viewActions and viewAssertations. Espresso’s API makes Android UI tests easy to maintain and change. Also, it is customizable.
4. Effortless setup and integration with Android Studio
The setup process for Espresso is much more straightforward than for Appium. Moreover, Espresso is integrated with the native Android development environment – Android Studio. This makes Espresso easy to use if you are already familiar with Android instrumentation.
5. Everyone can use it
Without a doubt, Espresso is the UI automated testing tool of choice for Android developers. It’s integrated with Android Studio IDE and supports platform-specific languages. You may think that your QAs and other team members will find it difficult to use, but Google changed the targeting of its test automation tool just recently and made it possible for everyone to use Espresso. Now, thanks to Espresso Test Recorder, you can create tests for Android apps without having solid programming skills: simply record your interactions with the app instead of writing code.
Espresso’s Disadvantages
1. Limit in languages and technology stack
Espresso only supports Java and JUnit programming languages, which makes it a perfect tool for Android developers who are used to creating native apps. At the same time, it’s a drawback since it means you are limited in your stack.
2. Native to Android UI testing
Frankly, it’s really hard to say that this is a disadvantage since we are comparing Ap[iu for automated Android UI testing. But today, almost every mobile team around the world develops and needs to test both Android and iOS apps. Choosing Espresso certainly means that you need to find a similar tool/framework for automating iOS UI testing. That doubles the work on creating and maintaining the test scripts.
Espresso Sample Test Code
public void testEspresso() { // Check if view with the text 'Hello.' is shown onView(withText("Hello.")).check(matches(isDisplayed())); // R class ID identifier for 'Sign in' - and click it onView(withId(getInstrumentation().getTargetContext().getResources() .getIdentifier("com.twitter.android:id/sign_in", null, null))).perform(click()); // R class ID identifier for entering username onView(withId(getInstrumentation().getTargetContext().getResources() .getIdentifier("com.twitter.android:id/login_username", null, null))).perform((typeText("username"))); // R class ID identifier for entering password onView(withId(getInstrumentation().getTargetContext().getResources() .getIdentifier("com.twitter.android:id/login_password", null, null))).perform((typeText("password"))); // R class ID identifier for clicking log in onView(withId(getInstrumentation().getTargetContext().getResources() .getIdentifier("com.twitter.android:id/login_login", null, null))).perform(click()); // Activate the text field to compose a tweet onView(withId(getInstrumentation().getTargetContext().getResources() .getIdentifier("com.twitter.android:id/menu_compose_tweet", null, null))).perform(click()); // Type the tweet onView(withId(getInstrumentation().getTargetContext().getResources() .getIdentifier("com.twitter.android:id/edit", null, null))).perform((typeText(”#Bitbar"))); // Tweeting! onView(withId(getInstrumentation().getTargetContext().getResources() .getIdentifier("com.twitter.android:id/composer_post", null, null))).perform(click()); }
Appium vs. Espresso
To sum, when it comes to automated Android UI testing, Appium loses to Espresso big time in terms of speed, stability, feedback and ease of configuration. But there’s one quality Appium has that helps it gain some points back in this Appium vs. Espresso battle. Appium allows parallel Android UI test execution, which is a critical feature for any company that wants to make the most out of continuous testing and integration.
If you are looking for a framework that will take care of automated Android UI testing better, go with Espresso. It’s a platform-specific tool that has many strong sides: it’s fast, reliable, stable and easy to work with. But if your app is cross-platform and you also need to test the iOS version, go with Appium. Bitbar’s mobile app testing platform provides premium support for both Appium and Espresso to automate your Android app testing. Sign up for a trial and see how your tests perform on our real Android devices.