Current location - Recipe Complete Network - Catering franchise - How to test android automatically with python?
How to test android automatically with python?
Start the first simple Android UI automation test.

1. Connect the real machine or simulator with adb command.

2. Open the uiautomatorviewer tool.

3. Use the uiautomatorviewer tool to get the applied elements and locate them.

4. Briefly introduce the usage of unittest framework.

5. Write the automatic test of Mao Ning attendance application registration module in Python.

1. Connect the real machine or simulator with adb command:

The mobile phone USB is connected to the computer and enters the developer mode;

Cmd command: adb device, check whether the mobile phone is connected.

Write a picture here.

Display error

This is because the port of Yade is occupied. We need to see what application occupies this port (5037 is the default port of adb).

Cmd command: netstat-aon | findstr "5037 "

Write a picture here.

It can be seen that the PID number of the program corresponding to port 5037 is 8388;

Cmd command: tasklist|findstr "8388 "

Write a picture here.

It can be seen that the program corresponding to 8388 is kadb.exe, indicating that the program is using port 5037;

At this time, we need to end the kadb. exe process in the task manager, press the shortcut key "Ctrl+Shift+Esc" to bring up the Windows task manager, find "kadb.exe", and click the end process below!

Write a picture here.

Let's run the cmd command again: adb device.

Write a picture here.

After this step is successful, we can run uiautomatorviewer; This comes with the sdk.

We need to use the uiautomatorviewer tool to get the elements for positioning.

Cmd command: uiautomatorviewer, open uiautomatorviewer interface.

Write a picture here.

Or find the sdk directory: find the uiautomatorviewer.bat file in sdk\tools, and double-click to run.

Write a picture here.

2. Open the uiautomatorviewer tool.

Write a picture here.

We can locate by text, resource id, class and other elements.

3. Use the uiautomatorviewer tool to get the applied elements and locate them.

Here I write a test script with python's own IDLE, open a python file, find the IDLE(python GUI) and double-click to open it, as shown in the following figure:

Write a picture here.

4. Briefly introduce the usage of unittest framework.

#-*-encoding: utf-8 -*-

Import devices as d from uiautomator.

Import unit test

Class Mytest(unittest. Test case):

# Initialize work

Define settings (itself):

Print "-Initialization Work"

# Quit cleaning work

Define disassembly (itself):

Print "-

# Test Click Cat Ning Attendance

Define Test _ Login (Self):

D(text= "Mao Ning attended"). Click ()

Print "-test 1"

# Test 2

Define test _z (itself):

Print "-Test 2" # Here you can write your second test case.

# Test 3

Define test _w (itself):

Print "-Test 3" # Here you can write your third test case. . . . . . . . . . . . .

if __name__ == '__main__ ':

unittest.main()

The results are as follows:

The test started on 2 1: 14 …

——————— Initialization work

———— Test 1

—————————— Quit cleaning up.

——————— Initialization work

———— Test 3

—————————— Quit cleaning up.

——————— Initialization work

—————— Test 2

——————————— Quit cleaning up.

Process ends with exit code 0.

From the results, we can see that the running mode of unittest framework is:

Installation test 1 disassembly

Installation Test 2 Disassembly

Installation Test 3 Disassembly

5. Write the automatic test of Mao Ning attendance application registration module in Python.

#-*-encoding: utf-8 -*-

Import devices as d from uiautomator.

Import time

Import unit test

Class MyTestSuite(unittest. Test case):

# Initialize work

Define settings (itself):

Print "-Initialization Work"

# Quit cleaning work

Define disassembly (itself):

Print "-

# * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

# Determine whether the control exists &; text

def check_controls_exists(self,controls_text):

if d(text=controls_text)。 Existence:

Return 1

Otherwise:

Return 0

# Judge whether the button is grayed out&; Text & clickable

def check _ controls _ click _ text(self,controls_text):

If d (text = controls _ text). info.get ("clickable") is true:

Return 1

Otherwise:

Return 0

#assertIn(a, b) a in b

def check_ainb(self,resourceid,b):

If d (resourceid = resourceid). info.get ("text") in b:

Return 1

Otherwise:

Return 0

#***********************************************************

# Registration module

def test_Aregister(self):

Try:

Time. Sleep (2)

# Cat Ning attends to open a new era

Self. Assert equality (ego. Check _ controls _ click _ text ("registration"), 1, u "Mao Ning opens a new era of attendance").

# Cat Ning attendance opens a new era-"Click the registration button to enter the registration cat ning interface.

D(text= "registration ")。 Click ()

Time. Sleep (3)

# Register Cat Ning Interface

Self. Assert equality (ego. Check _ text ("com.isentech.attendance t: id/regis _ phone", "Please enter your mobile phone number"),

1, u "Registration page-"Please enter your mobile phone number ")

Self. Assert equality (ego. Check _ text ("com.isentech.attendance t: id/regis _ verifiycode", "Please enter the verification code"),

1, u "Registration page-"Please enter verification code ")

Self. Assert equality (ego. Check _ controls _ click _ text ("get verification code"), 0, u "Registration page-"get verification code ").

Self. Assert equality (ego. Check _ controls _ click _ text (Zhongke Aixun Service Agreement), 1, u "Registration Page-Zhongke Aixun Service Agreement)

self . assert equal(self . check _ controls _ click _ text(" registration "),0,u " registration page-" registration ")。

Time. Sleep (2)

# Zhongke Aixun Service Agreement

D(text= "Zhongke Aixun Service Agreement"). Click ()

Time. Sleep (2)

Self. Assert equality (ego. Check _ ainb ("com.isentech.attendance t: id/title", "service agreement"), 1, u "registration page-"service agreement ").

Time. Sleep (1)

d(resourceId = " com . isentech . attendance et:id/title _ back ")。 Click ()

Time. Sleep (1)

# Can I register without entering my mobile phone number?

D(text= "registration ")。 Click ()

Time. Sleep (3)

# Can I register my mobile phone number with only 1 digit? Can I get the verification code only by entering the number 1?

d(resourceId = " com . isentech . attendance et:id/regis _ phone ")。 set_text(" 1 ")

Self.assert equal (self.check _ controls _ click _ text ("get verification code"), 0)

Time. Sleep (1)

D(text= "registration ")。 Click ()

Time. Sleep (1)

d(resourceId = " com . isentech . attendance et:id/regis _ phone ")。 Clear text ()

Time. Sleep (1)

# Can I get the verification code only by entering 5 numbers?

d(resourceId = " com . isentech . attendance et:id/regis _ phone ")。 set _ text(" 1 1 1 1 1 ")

Self.assert equal (self.check _ controls _ click _ text ("get verification code"), 0)

Time. Sleep (1)

d(resourceId = " com . isentech . attendance et:id/regis _ phone ")。 Clear text ()

Time. Sleep (1)

# Can I only enter my mobile phone number to register?

d(resourceId = " com . isentech . attendance et:id/regis _ phone ")。 Set_text (phone number)

self . assert equal(self . check _ controls _ click _ text(" registration "),0)

Time. Sleep (1)

D(text= "registration ")。 Click ()

Time. Sleep (1)

# Enter the correct verification code &; Gets whether the verification code is highlighted.

d(resourceId = " com . isentech . attendance et:id/regis _ verify code ")。 set_text("5648 ")

Time. Sleep (1)

Self. assert equal (self. check _ controls _ click _ text ("get verification code"), 1).

Time. Sleep (2)

# Can I register my password only with 1 digit? Is the Register button highlighted?

d(resourceId = " com . isentech . attendance et:id/regis _ pass ")。 set_text(" 1 ")

d(resourceId = " com . isentech . attendance et:id/regis _ passa gain ")。 set_text(" 1 ")

Time. Sleep (1)

Self. Assert equality (ego. Check _ controls _ click _ text ("register"), 0, u "Can I register my password with only 1 digit?" )

Time. Sleep (1)

d(resourceId = " com . isentech . attendance et:id/regis _ pass ")。 Clear text ()

d(resourceId = " com . isentech . attendance et:id/regis _ passa gain ")。 Clear text ()

Time. Sleep (1)

# Can I register with a different password?

d(resourceId = " com . isentech . attendance et:id/regis _ pass ")。 set_text(" 123456 ")

d(resourceId = " com . isentech . attendance et:id/regis _ passa gain ")。 set_text(" 12345 ")

Time. Sleep (1)

Self. Assert equality (ego. Check _ controls _ click _ text ("register"), 0, u "Can I register with a different password?" )

Time. Sleep (1)

d(resourceId = " com . isentech . attendance et:id/regis _ pass ")。 Clear text ()

d(resourceId = " com . isentech . attendance et:id/regis _ passa gain ")。 Clear text ()

Time. Sleep (1)

# Can I register with the correct password &; I have agreed whether to tick.

d(resourceId = " com . isentech . attendance et:id/regis _ pass ")。 set_text(" 123456 ")

d(resourceId = " com . isentech . attendance et:id/regis _ passa gain ")。 set_text(" 123456 ")

Time. Sleep (1)

self . assert equal(self . check _ controls _ click _ resourceId(" com . isentech . attendance et:id/regis _ agree "), 1)

self . assert equal(self . check _ controls _ click _ text(" registration "), 1)。

Time. Sleep (2)

D(text= "registration ")。 Click ()

Time. Sleep (8)

With exceptions, e:

Print u "Error: There is a problem with the registration module \n", e

Define a test application ():

Test unit = unit test. Test suite ()

test _ unit . add test(mytest suite(" test _ a register "))

if __name__ == "__main__ ":

# Test application

unittest.main()