This guide will show you how you can record, edit, replay and run an Android test for a sample app.
To get started with Android testing, you will need an Android phone and connect it to the computer's USB port. Barista communicates with the phone over USB. To allow it to do so, enable USB debugging under Developer Options in the Android Settings App.
Enable USB Debugging on your Android phone.
Android phone needed to be in Developer mode
If you don't see Developer options under Settings, follow these instructions to enable it
You can use a Mac or a Windows 10 PC. You only need to install Barista on it. See Installation to find how you can install Barista. The best way to install Barista with all its dependencies is using brew:
brew cask install moquality/brew/barista
To compile the sample app and install it on the device clone our
sample-apps repository and compile the
android-apidemos app. This will install the Debug version of ApiDemos.
git clone firstname.lastname@example.org:moquality/sample-apps.git git submodule update --init --recursive cd android/android-apidemos ./gradlew installDebug
You can also download the pre-compiled APK and install it using
adb install ApiDemos-debug.apk
Login to Barista using its direct link (that you must have received in a welcome email) or using the API Key. Read Login into Barista. Connect the phone to your machine and click on the Android logo. Barista should detect your Android phone.
When you create a test in Barista you don't use the keyboard on the phone as you can just use the keyboard on your machine for setting text. Using keyboards in tests might make your tests flaky. Hence, if your app doesn't use any special keyboards we recommend setting the keyboard type to "Disabled". This will disable the keyboard on your phone temporarily.
If you are a tester in a Team, you can select your Team in the drop down.
Click on the phone on which you want to record the test. Barista will initialize itself by installing UiAutomation libraries with which it can communicate with. If all goes well, you will see Barista is mirroring your phone.
The app list detects all the apps on your phone. Select the
io.appium.android.apis app from the drop-down of the app list. This is the Android package name (application ID) of the sample app.
Click the Create Test button to start recording the test. Barista launches the ApiDemos app. Start interacting with the app as you normally would. As you do so Barista recognizes the actions such as click, input, swipes, and assertions and record them on the right of your screen.
1. Click on Views 2. Swipe until you find WebView2 3. Click on WebView2 4. Click on the text box widget 5. Enter some text like "abc" 6. Add an Assertion - Select the text widget - Select the type as Text Matches - Click Add to add the assertion
After recording the test flow, click Done to save the test. Save the test as "Sample Test".
You can also add sleep as action if you know that your app is going to perform some background operations and the UI might take some time to load. This is not usually needed. However, if your app has animations or uses animations to cover up long-running background tasks, it is a good idea to use sleeps to make your tests less flaky.
- Click: Click anywhere on the mirrored screen in Barista.
- Input: Start typing and a text box will appear where you can enter the entire text.
- Swipe: Click, drag, and release. Swipes are performed after you release the mouse.
- Assertions: Click the Add Assertion button. You can choose a widget from the view hierarchy and choose an assertion.
- Sleep: Click the Add Sleep button to add sleep.
For every click action, Barista finds the expected widget and the best selector for it. Every input action recognizes the correct widget and sets the input text. Swipe action might be converted to scrolls if they are performed on lists. Now you are ready to check if the test recorded is correct or not.
Barista always clears any cache associated with the app when you start recording a test. Any functions of your app that use local storage such as login will get deleted.
To check the recorded test press the play button next to it. This will immediately replay the test back on the device connected. If the test fails at any step, it might need to be edited.
Our "Sample Test" that we created above will convert clicks to proper widgets and selector selections. The swipes will be converted to scrolls. The input text action will recognize the correct widget too.
Barista automates the process of scripting tests by finding the intended widget at every test step and then finding a unique selector for it. Sometimes edits are necessary to tell Barista the expected intent of an action. For example in the ApiDemos app, if the desired test is to select any widget in a list, the selector will be different than the one required to select a unique widget.
To edit the test, click the Edit icon next to the test.
In the Edit Test mode, you will see each test step in detail. Click on the test step to see the screenshot associated with it. You can edit a test step by clicking on Select New Element.
For example, select the test step where you select the text box. The current test step uses the widget properties class name and text to select the widget. Let's say we want to see if we can change the selectors on this test step.
1. Select the test step and click on Select This Element 2. Highlight the text box to see the properties of this widget on the right 3. The text box has a resource-id as 'i_am_a_textbox' 4. Click on Select This Element 5. Barista will now prompt you with new selectors available for this widget 6. Select "Click i have no focus with resource-id" and click Save Changes to save this test.
With these changes, you can replay the test to verify if your change is as intended and the test runs successfully. In this way, you can change the widget and selectors for that widget on each test step and ensure that you have created the test as you intended.
You can also delete a test step, change the description of a test step or add a post-sleep action for any test step in the Edit Mode.
After the test is recorded as intended, running the test is as easy as clicking the Play button. The test immediately runs on the connected device.
The test execution stops only if an assertion fails, otherwise it keeps going. If at a test step a widget is not found, you will see it marked as such under the test. At the end of the execution, you will see if the test run succeeded or not with the steps that might have failed.
To see the test run on the Dashboard go to app.moquality.com. Select the
io.appium.android.apis. Find the Tests tab and the "Sample Test" test in it.
The dashboard shows you the test as a series of screenshots. You will also see the runs done on this test in this view along with a screen-by-screen comparison between the original test recording and the selected test run.