Uiautomator running the Android-side UI Testing

Source: Internet
Author: User

The following are the steps for setting up and compiling jar packages and running a case with Uiautomator, the UI testing for Android app-side:

1. New Java Project

2. Right-click Properties,

Add the JUNIT4 library and add Android.jar and Uiautomator to the jar package dependencies

3, the new package under the project, after the new class, the code is as follows:

(Note: Extends classes need to be written clearly uiautomatortestcase, specific methods need to start with test, and throws Uiobjectnotfoundexception)

1  PackageCom.ganji.loginhelper;2 3  4 5 ImportCom.android.uiautomator.core.UiObject;6 7 Importcom.android.uiautomator.core.UiObjectNotFoundException;8 9 Importcom.android.uiautomator.core.UiScrollable;Ten  One ImportCom.android.uiautomator.core.UiSelector; A  - Importcom.android.uiautomator.testrunner.UiAutomatorTestCase; -  the  Public classRunnerextendsUiautomatortestcase { -  -     Public voidTestdemo ()throwsuiobjectnotfoundexception { -  +   //Simulate a short press on the HOME button. -  + Getuidevice (). Presshome (); A  at   //We ' re now in the home screen. Next, we want to simulate -  -   //a user bringing up the all Apps screen. -  -   //If you use the Uiautomatorviewer tool to capture a snapshot -  in   //of the The Home screen, notice that's all Apps button ' s -  to   //Content-description Property has the value "Apps". We can +  -   //Use the "This" to create a uiselector to find the button. the  *UiObject Allappsbutton =NewUiObject (NewUiselector () $ Panax Notoginseng. Description ("Apps")); -  the   //simulate a click to bring up the all Apps screen. +  A Allappsbutton.clickandwaitfornewwindow (); the  +   //in the all apps screens, the Settings app is located in -  $   //The Apps tab. To simulate the user bringing up the Apps tab, $  -   //we create a uiselector to find a tab with the text -  the   //label "Apps". - WuyiUiObject Appstab =NewUiObject (NewUiselector () the  -. Text ("Apps")); Wu  -   //simulate a Click to enter the Apps tab. About  $ Appstab.click (); -  -   //Next, in the apps tabs, we can simulate a user swiping until -  A   //they come to the Settings app icon. Since the container view +  the   //is scrollable, we can use a Uiscrollable object. -  $Uiscrollable appviews =NewUiscrollable (NewUiselector () the  the. Scrollable (true)); the  the   //Set The swiping mode to horizontal (the default is vertical) -  in appviews.setashorizontallist (); the  the   //Create a uiselector to find the Settings app and simulate About  the   //A user Click to launch the app. the  theUiObject Settingsapp = Appviews.getchildbytext (NewUiselector () +  -. ClassName (Android.widget.TextView.class. GetName ()), the Bayi"Settings"); the  the Settingsapp.clickandwaitfornewwindow (); -  -   //Validate The package name is the expected one the  theUiObject settingsvalidation =NewUiObject (NewUiselector () the  the. PackageName ("Com.android.settings")); -  theAsserttrue ("Unable to detect Settings", the  the settingsvalidation.exists ()); 94  theUiObject ReportBug =NewUiObject (NewUiselector (). Text ("Sound")); the  the Reportbug.clickandwaitfornewwindow ();98  AboutUiObject soundvalidation =NewUiObject (NewUiselector () - 101. Text ("Volumes"));102 103Asserttrue ("Unable to detect sound",104  the soundvalidation.exists ()); 106 107 Getuidevice (). Presshome ();108 109   }   the 111}

4. Then compile and publish the Uiautomator test:

Create a compilation file:

C:\users\58>android Create Uitest-project-n autologin-t 15-p E:\Debin2\autoLogin

Added file E:\Debin2\autoLogin\build.xml

The above is created successfully, called Build.xml

This command is:

Android Create uitest-project-n xxx-t xxx-p xxx

Where-n after xxx represents the name to project

The-t 15 is obtained by: through Android list, can get a list, because I use the Android.jar and Uiautomator.jar is 19, so find the corresponding is:

So my-t was written in 15.

Then is the parameter after-p, which is the location of the project, because this build.xml is the project directory

command-line creation prompt after the creation of a successful, that is, added file xxx after the sentence appears, in the project directory can see this file, through the Eclipse directory refresh can also see: (Three more files)

After that, if Eclipse has this package setting for ant directly, you can choose the ant build by right-clicking on the Build.xml, but I don't have a successful configuration in eclipse, but it doesn't matter, as long as the command line can run successfully.

Build.xml file opened, you can see the default is "Help", changed to build

Then I did not install ant in my machine, to download the ant file on the official website, after the successful installation, the environment variable needs to be configured, mainly consists of three places:

Ant_home D:\Program files\apache-ant-1.9.7-bin\apache-ant-1.9.7

Path D:\Program Files\apache-ant-1.9.7-bin\apache-ant-1.9.7\bin

Classpath D:\Program Files\apache-ant-1.9.7-bin\apache-ant-1.9.7\lib

Then run the Ant and ant-version directly under CMD, if the prompt is correct, it is not the case that ant is not an available command, it means that the environment variable is configured successfully.

Here's where the problem is more likely:

Into the project directory,

Command line input: Ant + build.xml

There should be no suffix in the back, direct ant build

You can then see that the build was successful, and in the Bin folder of the project directory, the filename is called: Autologin.jar

5. The next step is to push the jar package to the phone and then run the program.

The directory of the mobile phone that the push enters is/data/local/tmp,

The command to execute the case is: adb shelle uiautomator runtest autologin.jar-c Com.ganji.loginhelper.Runner

Runtest is followed by the previous push in the jar package,-C is followed by the class to be executed (class before the name of the package, that is, xx.xx.package.classname)

Then there will be a run case after the information to play, complete, the back is how to write to achieve their desired test results ~ ~ ~

Uiautomator running the Android-side UI Testing

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.