Prior to learning Uiautomator were in Eclipse, because learning Android development contact as more and more frequent, so want to know how to build a Uiautomator project. Online information is very messy, looked up a lot of information, practice found that as long as the right step, as under the eclipse under the deployment of more simple operation.
The steps are summarized as follows:
First, create a new project, and the difference between the Android project is not creating activity (ADD No activity)
Second, after the completion of the new project, modify the Gradle file
Add the last line:
dependencies { compile filetree (dir: ' Libs '[' *.jar ']) testcompile ' Junit:junit:4.12' compile ' com.android.support:appcompat-v7:23.1.1' //introduced Uiautomator androidtestcompile ' com.android.support.test.uiautomator:uiautomator-v18:2.1.1'}
Gradle Sync, you can see in the external libraries directory generated uiautomator-v18-2.1.1 directory, this time indicates that the Uiautomator library has been successfully imported.
Third, write test cases
This section has encountered two difficulties:
① and eclipse can be performed differently with command-line compilation, as all in-house, and in general, click on the test Method right button, run directly on the line
But what if the right button doesn't have the run option and doesn't know how to run it?
You can click on the as left sidebar build variants view test artifact options, select Andriod Instrumentation Tests, then click on the left sidebar structure, find the test method, right click to see if it can be run.
② perhaps because of the relationship between 1.0 and 2.0 (2.0 was instrumentation Incorporated, some 1.0 of the method was discarded in 2.0), this simple click on the home key use case toss for half a day to run up.
public class Test extends instrumentationtestcase { // public class Test extends Uiautomatortestcase { Public void Testhome () { Uidevice.getinstance (Getinstrumentation ()). Presshome (); // uidevice.getinstance (). Presshome (); (Uidevice.getinstance () has been deprecated) // uidevice device = Getuidevice (); (Getuidevice () could not find the method) // device.presshome (); }}
2.0 no longer inherit uiautomatortestcase, but need to inherit instrumentationtestcase.
The way to get the device has also changed, Uidevice.getinstance (Getinstrumentation ()) This is the correct way to use. Both of the previously used methods are no longer feasible.
The API differences between 1.0 and 2.0 follow a familiar familiarity.
Run Uiautomator under Android Studio