Links to automated test history articles

Android Monkey stress test 2.Android Monkey advanced parameter application 3.Android MonkeyScript command simple application 4 The application of UiAutomator2.0

UiAutomator2.0 introduction

UiAutomator2.0 is android’s automated test framework, available across apps. Unlike instrumentation framework, UiAutomator2.0 does not require test object source code, therefore, for the black box test framework. Also, unlike the Monkey, UiAutomator2.0 does not focus on coordinates. Instead, it filters control properties (such as searching for buttons whose text is “submit”) to retrieve the control itself.

UiAutomator2.0 positions elements

UiAutomator2.0 can locate controls by resource ID, text content, property description, select property state, focus state, and then perform some operation validation. Here’s how to locate elements and the corresponding Api:

UiAutomator2.0 Common operations

UiAutomator2.0 needs to perform some operations after locating an element, mainly including click, slide, long press, input, button and drag. The corresponding apis are as follows

Implement a simple chestnut

You need to use the UIAutomatorViewer tool, which is still implemented by MonkeyScript and MonkeyRunner. Click open KotAndord 2) Click Receiver radio 3) Click REGISTER A Receiver 4) Enter Hello in the edit box 5) Click SEND A Receiver 6) click UNREGISTER A Receiver 7) Click CLEAR to CLEAR the edit box 8) Return to exit and return to the home page

With the UIautomatorViewer tool, you can see the text, resource-ID, content-desc, checked, and focused attribute values. With these, the selected control can be located. As shown in the figure below, we can locate the SEND A RECEIVER button

UiAutomator2.0 use

Create an Android Studio project and add uiautomator2.0 dependencies to build.gradle. Note: uses-SDK :minSdkVersion 18

AndroidTestImplementation 'androidx. Test. Uiautomator: uiautomator: 2.2.0'Copy the code

Create the KotAndroidTest test class in androidTest, edit the following code, run the code test, and observe whether the phone runs according to the script to execute the output.

@RunWith(AndroidJUnit4::class) class KotAndroidTest { var instrumentation:Instrumentation ? = null var device:UiDevice ? = null @Before fun setUp(){ instrumentation = InstrumentationRegistry.getInstrumentation() device = UiDevice. GetInstance (instrumentation)} @test fun kotTest(){// Slide the screen to pull the application list device? .swipe(510,1527,510,527,10) // wait 1s to thread.sleep (1000) // click KotAndroid device? .click(600,1000) thread.sleep (1000) FindObject (By. The text (" radio Receiver "))? .click() thread.sleep (2000) // Use the resource ID to locate the element and click device? .findObject(By.res("me.peace.ka:id/register"))? .click() thread.sleep (1500) // Enter hello device? .findObject(By.res("me.peace.ka:id/edit"))? .text = "Hello" Thread.sleep(1500) device? .findObject(By.res("me.peace.ka:id/send"))? .click() Thread.sleep(1500) device? .findObject(By.res("me.peace.ka:id/unregister"))? .click() thread.sleep (1500) // Use the resource ID and text to locate the element device? .findObject(By.res("me.peace.ka:id/clear"))? .findObject(By.text("CLEAR"))? .click() thread.sleep (1500) // Press the Home button .pressHome() } @After fun tearDown(){ device = null instrumentation = null } }Copy the code

If you liked this article or helped you, please give me a Star

https://github.com/peace710/AJLife