Run Uiautomator under Android Studio

Source: Internet
Author: User

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

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.