Android Automated test-uiautomator Environment setup

Source: Internet
Author: User

Android Automated test-uiautomator Environment setup

First, the Environment preparation

  1. Install the Android SDK and configure the environment variables

2. Install Android Studio, domestic access to the official website is limited, if not downloaded, you can go to my Baidu cloud disk download:

Https://pan.baidu.com/s/1bpq5wK3

This cloud disk has Uiautomator2-dependent jar packages that can be downloaded at the same time

   

Second, the new Android Studio project

  

  Create a new project, enter application name, Next,

  

Default selection, Next,

  

Select empty activity:

  

  After the final finish, switch to Project view;

  

  Right-click the project, create a new libs, and put the Uiautomator-dependent jar package in the network tray, copy it in, and add the dependency,

  

After the ADD as library, a small box will pop up, select the app, click OK

  

  So our project will be built, the upper left corner, to switch our project mode to Android mode,

  

 Now the Android view mode, the interface is relatively simple and intuitive, as shown in: labeled Android test is where we want to write the test case package,

  

  New Home a Java class, input class name, now we can happily write test code

Three, test example

  Below we write an example, launch simulator, emulator home has a Chrome browser, operation steps: click chrome-input Www.baidu.com-enter;

Click on the AVD Manager on Android studio to launch the emulator, the emulator interface is as follows:

  

  

  Test Case:

1. Click Chrome

2. Enter www.baidu.com

3. Enter

The code is as follows:

    

  After writing the test case, we can run it, and before we run it, we'll look at the running configuration:

  

  In the configuration file, be sure to have the following line of code, if not, you can add:

Testinstrumentationrunner "Android.support.test.runner.AndroidJUnitRunner"

Now it's ready to run, open your simulator and see what the interface looks like:

  

  The complete code is as follows:

1 Importandroid.app.Instrumentation;2 ImportAndroid.support.test.InstrumentationRegistry;3 ImportANDROID.SUPPORT.TEST.RUNNER.ANDROIDJUNIT4;4 ImportAndroid.support.test.uiautomator.UiDevice;5 ImportAndroid.support.test.uiautomator.UiObject;6 Importandroid.support.test.uiautomator.UiObjectNotFoundException;7 ImportAndroid.support.test.uiautomator.UiSelector;8 Importandroid.view.KeyEvent;9 Ten ImportOrg.junit.Before; One Importorg.junit.Test; A ImportOrg.junit.runner.RunWith; -  - /** the * Created by tianxing on 2017/8/15. -  */ -  -@RunWith (ANDROIDJUNIT4.class) +  Public classHelloWorld { -  + Uidevice Uidevice; A instrumentation instrumentation; at  - @Before -      Public voidsetUp () { -instrumentation =instrumentationregistry.getinstrumentation (); -Uidevice =uidevice.getinstance (instrumentation); -     } in  - @Test to      Public voidLaunchchrome () { +UiObject Chrome = Uidevice.findobject (NewUiselector (). Text ("Chrome")); -UiObject searchcontent = Uidevice.findobject (NewUiselector (). Text ("Search or type URL")); the  *         Try { $ Chrome.click ();Panax NotoginsengSleep (2000); -Searchcontent.settext ("www.baidu.com"); the Uidevice.presskeycode (keyevent.keycode_enter); +}Catch(uiobjectnotfoundexception e) { A e.printstacktrace (); the         } +  -     } $  $      Public voidSleepintmint) { -         Try{ - thread.sleep (mint); the}Catch(interruptedexception e) { - e.printstacktrace ();Wuyi         } the     } -  Wu}
View Code

Android Automated test-uiautomator Environment setup

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.