In front of an article on writing Python to carry out the monkeyrunner of the power test, in fact, Monkeyrunner in the automation of the function is much more than this, wherein, The ability to connect to multiple simulators (or real machines) for automated testing is one of the highlights of Monkeyrunner. Because Monkeyrunner is written based on Python, small partners with a certain understanding of Python will be well-used. OK, here's a quick introduction:
Environment Note: This test is for an app that can automatically simulate the interface (Activity) that enters different resolution simulators (click button and simulate sliding interface) and then perform the various emulator interfaces. Of course, this test application is a communication app (similar to SMS), there will be an analog send to receive text messages, simple automation function code;
Test tool: Eclipse+python (2.7.0) + two different custom resolution simulators (320P, 1080P);
Steps:
Running Eclipse and plug-in installation in another blogger's article has been introduced, here is not cumbersome, directly into the code section:
The specific code is as follows:
#导入相关MonkeyRunner的包from com.android.monkeyrunner import MonkeyRunnerfrom com.android.monkeyrunner.easy import EasyMonkeyDevicefrom com.android.monkeyrunner.easy import byfrom com.android.monkeyrunner import monkeydevice# defines the devices of two connected devices, here is a two simulator device1=monkeyrunner.waitforconnection (1.0, ' emulator-5554 ') device2= Monkeyrunner.waitforconnection (1.0, ' emulator-5556 ') devicelist= (DEVICE1,DEVICE2) # Defines an array of connected devices devicelistk=0 #标识变量 for storing different resolution phones in different custom folders For i in range (0,len ( devicelist)): #遍历数组两个连接设备 # Determine if the device is connected if not devicelist[i]:
print "Please connect a device to start! " else: print "start " #定义一个touchId to respond to touch event touchid= Easymonkeydevice (Devicelist[i]) #安装一个apk包 devicelist [I].installpackage ("********.apk") Monkeyrunner.sleep (+) #由于在模拟器上运行, sometimes the last operation did not complete jump to the next exercise statements, to avoid affecting the next operation and produce an error, need to give the simulator a period of hibernation time, it is recommended to hibernate after each operation (Same as) if (k==0): #进行第一个模拟器的a变量命名, image format is png a= ' *******.png '
else: #进行第二个模拟器的a变量命名 a= ' *******.png ' #启动App中需要测试的Activity devicelist[i].startactivity ( Component= "************activity") Monkeyrunner.sleep (#drag事件是可以理解为在模拟器屏幕上滑动操作), the first two parameters are the simulator's click coordinates devicelist[i].drag ( 700,240), (100,240), 1,10) monkeyrunner.sleep (10) # The TouchId event is the action of clicking the button, each button has an ID number, and the second parameter is equivalent to clicking the button touchid.touch (monkeydevice.down_and _up,by.id (' id/***** '), monkeydevice.down_and_up) monkeyrunner.sleep (Ten) touchid.touch (by.id (' id/***** '), monkeydevice.down_and_up) Monkeyrunner.sleep (Ten)
#press事件是直接操作模拟器的虚拟键盘, here is click "Back" button devicelist[i ].press (' keycode_back ', ' down_and_up ') monkeyrunner.sleep (5) devicelist[i].press (' keycode_back ', ' down_and_up ') Monkeyrunner.sleep (5) touchid.touch (by.id (' id/********* '), monkeydevice.down_and_up) monkeyrunner.sleep (5) compose= Devicelist[i].takesnapshot () //defines a compose variable for the current interface #给当前界面的光标指处模拟输入文字 "1111 " device.type (' 1111 ') #调用python的os库, here is the analog 12345678 send" test "to simulator" 5554 "
#模拟器的命名是从5554开始, followed by 5556, 5558, and so on, each time according to the start of the simulator to name Os.system (' echo ' SMS send 12345678 Test "| telnet localhost 5554 ') #把该进行保存 Compose.writetofile (A, ' png ') #进行完第一个模拟器操作后, k++ the next emulator's loop operation K=k+1
PS: Note the indentation structure of Python writing code above
Conclusion: The above code is sufficient to cover all of the automated functional tests, where the activity of the app or the Click Button Acquisition method continues to be described in subsequent articles. Hope to get everybody's opinion, make the work become more interesting and efficient!
Monkeyrunner for automated testing