Go Android UI Automation test

Source: Internet
Author: User
Tags gettext

Introduced

The Android Test Support library includes a UI Automation module that automates black-box testing of Android apps. The automation module was introduced in API level 18, which allows developers to mimic user behavior on controls that compose the application UI.

In this tutorial, I'll show you how to use this module to create and execute a basic UI test, choosing the default Calculator module for testing.

Prerequisite

Before use, the following conditions are required:

    1. Latest version of Android Studio
    2. Running Android 4.3 or later devices or virtual machines
    3. Understanding JUnit
1. Installing dependent libraries

To use the UI Automation module in your project, you need to edit the file build.gradleunder your project's app directory and add the following dependencies:

Androidtestcompile ' com.android.support.test:runner:0.2                       ' com.android.support.test:rules:0.2 '                          ' com.android.support.test.uiautomator:uiautomator-v18:2.1.0 '

If you are using library APPCOMPAT-V7 and its version number is 23.1.0, you need to add the following dependencies to ensure that both the app itself and the test app use the same version of the com.android.support:support-annotations :

Androidtestcompile ' com.android.support:support-annotations:23.1.0 '
2. Create a test class

Create a new test class that is CalculatorTester implemented by creating a file named Calculatortester.java in the Androidtest directory. The UI Automation test cases that you create must inherit from InstrumentationTestCase .

Override setUp the method, and add the Testadd method. To this CalculatorTester class is defined as follows:

   Public class extends instrumentationtestcase{       @Override                                                      publicvoidthrows  Exception {                           }                                                                 Public void throws Exception {                         }                                                          }  
3. View Launcher UI

Connect your Android device to the PC and click the Home button to enter the main screen.

Go back to your computer, use file management or terminal to browse the directory where you installed the Android SDK, and go to the Tools directory and click Uiautomatorviewer. This will start the UI Automater Viewer and you will see the following interface:

Click on the phone icon above to get a screenshot of your Android device. Notice that the captured screenshot is interactive at this point. Click on the Apps icon below. In the Node Detail area on the right, you can see different details displayed depending on the selection icon, as shown in:

Interacting with on-screen apps, UI Automation tests need to uniquely identify them. In this tutorial, you can use the app's text , content-desc or field, to make class a unique distinction.

There are no fields from the apps icon that you can see text , but there are content-desc . Make a note of its value, which will be used later.

Pick up your Android device, touch the apps icon, and go to all the app screens installed on your device. Use the UI automater viewe to get another screen. To write a test for a calculator application, click the Calculator icon to see the detailed interface.

This time content-desc is empty, but text the value is calculator, also remember this value.

If your Android device is running a different main interface or different Android versions, the interface and display details will be different. This means that some modifications need to be made in subsequent code to match your operating system.

4. Prepare the test environment

Go back to Android Studio and setUp add code to the method. As its name, the setUp method is used to prepare the test environment. In other words, this method specifies what action needs to be performed before the actual test.

Now you need to write code to simulate a few of the actions you just performed on your Android device:
1. Press the home key to enter the main interface
2. Click the Apps icon to enter the application interface
3. Click the calculator icon to start it

Declare a variable of type in your class UiDevice device . It represents your Android device and later uses it to simulate user behavior.

Private Uidevice device;

in the setUp method, by calling UiDevice.getInstance method the to initialize device , passing Instrumentation instance, as follows:

device = Uidevice.getinstance (Getinstrumentation ());

Analog Click Device Home key, need to call pressHome method.

Device.presshome ();

Next, you need to simulate the action of clicking on the apps icon. This action cannot be done immediately because the Android device needs a response time to load the interface. If this action is performed before the screen is displayed, it will cause a run-time exception.

When waiting for something to happen, you need to invoke UiDevice the method of the instance wait . Wait for the apps icon to display to the screen, using the Until.hasObject method.

The Identify apps icon needs to use By.desc the method and pass the value to the parameters of the apps. You also need to specify the maximum wait time, in milliseconds. Set here to 3000.
The following code snippet is formed:

Device.wait (Until.hasobject (By.desc ("Apps"), 3000);

to get a reference to the apps icon, you need to use findObject method. Once you have a reference to the apps icon, you can invoke the click method to simulate the click action.

UiObject2 Appsbutton = Device.findobject (By.desc ("Apps"); Appsbutton.click ();

as before, we need to wait some time to make sure the calculator icon is displayed on the screen. In the previous steps, we saw text the identification calculator icon that can be unique through the field. We call By.text the method to find the icon, passing the parameter as Calculator .

Device.wait (Until.hasobject (By.text ("Calculator")), 3000);
5. Check the calculator UI

Launch the Calculator app on your Android device and use the UI Automater Viewer to view the display. Once you get to a screenshot, click on a different button to see what values you can use to uniquely differentiate them.

In this test case, use the calculator to calculate the value of the 9+9= and confirm whether the result is 18. This means you need to know how to differentiate between keys 9, + and =.

On my device, here is the information I collected:

    1. Numeric Key matching text values
    2. + and = use content-desc values, corresponding to plus and equals, respectively
    3. The return value is displayed in the EditText control

If you're using a different version of the Calculator app, be aware that these values may not be the same.

6. Create a test class

In the previous steps, you've learned how findObject to use methods By.text By.desc to get references to different objects on the screen. Also learned click to simulate the action of clicking an object by means of a method. The following code uses these methods to simulate 9+9=. Add these to the CalculatorTester method in the class testAdd .

        Device.wait (Until.hasobject (By.text ("9"));         = Device.findobject (By.text ("9"));        Buttonnine.click ();         = Device.findobject (By.desc ("plus"));        Buttonplus.click ();        Buttonnine.click ();         = Device.findobject (By.desc ("equals"));        Buttonequals.click ();

now wait for the result to run. It cannot be used here Until.hasObject , because the result of the calculation is EditText already displayed on the screen. Instead, we use the waitForIdle method to wait for the calculation to complete. Similarly, the maximum wait time is 3000 milliseconds.

Device.waitforidle (3000);

Use findObject and the By.clazz methods method gets EditText the reference to the object. Once you have this reference, you can call the getText method to determine whether the calculation is correct.

UiObject2 Resulttext = Device.findobject (By.clazz ("Android.widget.EditText"= Resulttext.gettext ();

Finally, use assertTrue to verify that the range value is 18.

Asserttrue (Result.equals ("18"));

The test ends here.

6. Perform the test

To perform the test, you need to select it in the toolbar of Android Studio CalculatorTester and click on the Play button to the right of it.

Once the compilation is complete, the test runs successfully. When the test runs, you will see the UI Automation interface on your Android device.

7. Complete code
 Public classCalculatortesterextendsInstrumentationtestcase {PrivateUidevice device; @Overrideprotected voidSetUp ()throwsException {Device=uidevice.getinstance (Getinstrumentation ());        Device.presshome (); Device.wait (Until.hasobject (By.desc ("Apps"), 3000); UiObject2 Appsbutton= Device.findobject (By.desc ("Apps"));        Appsbutton.click (); Device.wait (Until.hasobject (By.text ("Calculator")), 3000); UiObject2 Calbutton= Device.findobject (By.text ("Calculator"));        Calbutton.click ();    Testadd (); }     Public voidTestadd ()throwsException {device.wait (Until.hasobject (By.text ("9")), 3000); UiObject2 Buttonnine= Device.findobject (By.text ("9"));        Buttonnine.click (); UiObject2 Buttonplus= Device.findobject (By.desc ("Plus"));        Buttonplus.click ();        Buttonnine.click (); UiObject2 buttonequals= Device.findobject (By.desc ("equals"));        Buttonequals.click (); Device.waitforidle (3000); UiObject2 Resulttext= Device.findobject (By.clazz ("Android.widget.EditText")); String result=Resulttext.gettext (); Asserttrue (Result.equals ("18")); }}
Summarize

In this tutorial, we learned how to use the UI Automation test module and UI Automater Viewer to create a user interface test. You've also seen how easy it is to perform tests using Android studio. Although we have tested a relatively simple application, we can use the learned concepts in almost all of the testing of Android applications.

You can learn more about the test support library on the Android developer site.

Go Android UI Automation test

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.