Click on the top of the blue word code eggs

Write a test case on the UiAutomator and solve it in minutes.

The author blog

http://www.jianshu.com/u/34ece31cd6eb

The source address

https://github.com/alidili/Demos/tree/master/UiAutomatorDemo

The article directories

  • introduce

  • Environment set up

  • The test process

  • practice

0

introduce


Android team launched a new UI automation testing tool UiAutomator in version 4.1 (API 16), which is used to help developers complete App Debug work more efficiently, and is also a great boon for testers. Why?

Test: “I found A bug that your App sometimes flashes when you open page A and then page B.”

Development: “Huh? There are other problems like this. Let me see them again. (Inner monologue: Why does my App have a bug? It must be the wrong posture.)”

Test: After a busy session… “Oh, why not?”

Development: “that you go back first, again tell me.”

Days passed…

Test: full of joy form, “last time that problem I reappear, operation to you see…. Come on, why don’t you show up again?”

Development: “Is there a problem with the equipment, you change the equipment to try again?”

Test: “The baby’s heart is bitter, but the baby doesn’t say!”

With UiAutomator:

Test: “I found A bug that your App sometimes flashes when you open page A and then page B.”

Developer: “This is easy. I’ll write a test case using UiAutomator and solve it in minutes.”

Test: “Awesome Word guy!”

The end of the whole!

UiAutomator provides the following two tools to support UI automation testing:

  • Uiautomatorviewer: A graphical interface tool used to analyze UI controls. It is located in the Tools folder in the SDK directory.

  • Uiautomator: A Java library that provides various apis for performing automated tests.

1

Environment set up

This article uses Android Studio as the IDE, Eclipse, Please Go Home!

Build. Gradle > build. Gradle > build. Gradle > build.

Note: The lowest version of the UIAutomator library supports is API 18, so the minSdkVersion of the development environment for this article is 18.

Configuration AndroidJunitRunner testInstrumentationRunner to:

Take a look at the complete build.gradle file:

AndroidTestCompile androidTestCompile androidTestCompile androidTestCompile androidTestCompile androidTestCompile androidTestCompile androidTestCompile

  • Compile: It is compiled and packaged into debug/release APK.

  • TestCompile: only unit tests are compiled. It is not packaged into the DEBUG /release APK package and does not require device support.

  • AndroidTestCompile: only participate in UI test compilation, will not be packaged into the DEBUG /release APK package, requires device support.

Provided, APK, Debug compile, and Release compile:

  • Provided: only participates in compilation and is not packaged into debug/ Release APK.

  • APK: does not compile, but is packaged into debug/release APK.

  • Debug compile: Only takes part in Debug compilation and is packaged into the Debug APK.

  • Release compile: Only involved in Release compilation, only packaged into Release APK.

2

The test process

1. Install the tested App into the mobile phone.

2. Open the UI analysis tool UIAutomatorViewer. bat to analyze the interface elements of the current UI and ensure that each control of the App can be obtained by the test tool.

3. Write test cases according to App usage process.

4. Run test cases to test, locate and solve bugs.

3

practice


In the above picture, we assume that repeated sliding of the layout will lead to crash. In this case, we write a test case.

Analyze the UI interface elements

Start the App under test, open the uiautomatorviewer. Bat tool, click the Device Screenshot button at the upper left corner to capture a Screenshot, as shown below, Screenshot is displayed on the left side, layout structure and control properties are displayed on the right side, control properties are used in writing test cases.

Write test cases

Select File — New — New Project to create a New Project, which is the same as the process of creating a normal Project, except that there is no need to create an Activity. After creation, the Project structure is as shown below:

AS you can see, in the app-src directory, AS automatically creates an androidTest folder for us to write UI test cases, and a test folder for unit test cases at the same level.

Once the project is successfully created, introduce dependencies as described above, and when the build is complete, start writing test cases. Before we get there, let’s cover up some common UIAutomator apis:

UiDevice:

Device object, through the UiDevice getInstance(instrumentation) method, you can use the UiDevice instance to detect various attributes of the device, such as obtain the screen direction, size, etc., can also use the UiDevice instance to perform device-level operations, For example, click the Home button, the back button, etc. :

UiSelector

Used to obtain certain UI control objects that meet the conditions, you can obtain the resource ID, description and other familiar:

UiObject

Represents a UI control, obtained by uiDevice’s findObject(UiSelector) method. After obtaining the UiObject instance, you can perform related operations on the UI control, such as clicking and holding down:

UiCollection

Represents a set of UI controls, equivalent to ViewGroup. For example, when there are multiple checkboxes in the interface, you can obtain all the checkboxes under the current interface through the class name, and then obtain the specified CheckBox object through the control ID:

UiScrollable

Represents a scrollable control, such as opening the Settings for the phone option:

Take a look at the scrolling effect:

OK, that’s enough about the common API, let’s start writing test cases!

Talk is cheap, Show me the code.

In brief, first get device object and context, which will be used later, then start the test App, open the interface that needs to be tested, slide up and down 5 times and exit the App, because the above API has a certain understanding, look at the code is still very easy.

Note: Test methods must start with test. If multiple test methods exist, they are executed in alphabetical order after test.

Now that the code is finished, it is time to test. Right-click the test class and select the Run button, or click the execute button in the test class. The top button means execute all test methods and the bottom button means execute only the current test method:

After the test is executed, you can see some information printed on the console:

As you can see, the first by the adb shell commands in the equipment installed UiAutomatorDemo and com. Yl. UiAutomatorDemo. Two test apk, then launch the test, the tested App has already begun to execute the test process, after completion of execution, according to test results, Take a look at the execution effect of the App:

OK, so that’s the basic usage of UiAutomator.

Android 4.4.2 introduces super dazzling animation library