Android automated testing (3) & lt; monkeyrunner & gt; search for objects by ID & amp; touch & amp; type (python), android Automated Testing

Source: Internet
Author: User

Android automated testing (3) <monkeyrunner> Search for objects by ID & touch & type (python) and android Automated Testing

I used java in the previous two articles to implement Android automated testing (1) how to install and uninstall an application (java) and Android automated testing (2) search for objects (java) by ID ). However, they are implemented by using the java lib corresponding to monkeyrunner, but there are very few related documents. If you really want to use monkeyrunner for functional automated testing, we strongly recommend that you use the python language.



1. monkey runner

The monkeyrunner tool provides an API for writing programs that controls an Android device or emulator from outside of Android code.
With monkeyrunner, you can write a Python program that installan Android application or test package, runs it,
Sends keystrokes to it, takes screenshots of its user interface, and stores screenshots on the workstation.
The monkeyrunner tool is primarily designed to test applications and devices at the functional/framework level and for running unit test suites,
But you are free to use it for other purposes.
The monkeyrunner tool provides an API for controlling an Android device or simulator by writing programs outside the Android source code.
You can use monkeyrunner to write a Python program to install an Android Application and test package, run it, send key events, screenshots, and store them locally.
The monkeyrunner tool is designed to test applications and devices at the functional/framework level, and to run unit tests.


2. Example implementation

2.1 code implementation function: Go to the mainActivity page of the robot dream, find the button with id center_image and click the event. Then, find the button with id as button1 in the pop-up dialog box for Secondary confirmation.

from com.android.monkeyrunner.easy import EasyMonkeyDevice, Byfrom com.android.monkeyrunner import MonkeyRunner, MonkeyDeviceif __name__ == '__main__':    import codecs    codecs.register(lambda name: codecs.lookup('utf-8') if name == 'cp65001' else None)    print ('test start')    device = MonkeyRunner.waitForConnection()    easyMonkey = EasyMonkeyDevice(device)    print ('start robot dream mainActivity')    device.shell('am start com.robot.dream/com.robot.dream.mainActivity')    MonkeyRunner.sleep(3)    #easyMonkey.touch(MonkeyDevice.DOWN_AND_UP, By.id("id/center_image"));    by = By.id("id/center_image");    print (by)    easyMonkey.touch(by,MonkeyDevice.DOWN_AND_UP)    MonkeyRunner.sleep(3)    hierachy_view = device.getHierarchyViewer()    print(hierachy_view)    view_node = hierachy_view.findViewById('id/center_image')    items_node = view_node.children    print (len(view_node.children))    print ('touch 2')    easyMonkey.touch(by,MonkeyDevice.DOWN_AND_UP)    MonkeyRunner.sleep(3)    by2 = By.id('id/button1')    print (by2)    print (easyMonkey.visible(by2))    vn2 = hierachy_view.findViewById('id/button1')    print (dir(vn2))    print (getattr(vn2, 'name'))     print (vn2.id)    easyMonkey.touch(by2, MonkeyDevice.DOWN_AND_UP)    MonkeyRunner.sleep(3)


2.2 code implementation function: Go to the LoginActivity login page of the robot dream, find the login edit box TextEditor Based on the id of login_account, enter the username abc, and find the password edit box TextEditor Based on the id of login_pwd and enter the password abc123, finally, find the login button with the id of login_button and log on successfully.

from com.android.monkeyrunner.easy import EasyMonkeyDevice, Byfrom com.android.monkeyrunner import MonkeyRunner, MonkeyDeviceif __name__ == '__main__':    import codecs    codecs.register(lambda name: codecs.lookup('utf-8') if name == 'cp65001' else None)    print ('test start')    device = MonkeyRunner.waitForConnection()    easyMonkey = EasyMonkeyDevice(device)    print ('start login')    device.shell('am start com.robot.dream/com.robot.dream.LoginActivity')    MonkeyRunner.sleep(3)    by = By.id("id/login_account")    print (easyMonkey.visible(by))    easyMonkey.type(by,'abc')    MonkeyRunner.sleep(1)    device.press('KEYCODE_ENTER')    MonkeyRunner.sleep(1)    device.press('KEYCODE_BACK')    MonkeyRunner.sleep(1)    by2 = By.id('id/login_pwd')    print (easyMonkey.visible(by2))    easyMonkey.type(by2,"abc123")    MonkeyRunner.sleep(1)    device.press('KEYCODE_ENTER')    MonkeyRunner.sleep(1)    device.press('KEYCODE_BACK')    MonkeyRunner.sleep(1)    by3 = By.id('id/login_button')    print (easyMonkey.visible(by3))    easyMonkey.touch(by3,MonkeyDevice.DOWN_AND_UP)    MonkeyRunner.sleep(1)    device.press('KEYCODE_BACK')


3. Summary:

You can use monkeyrunner to search for controls by id, and use several basic operations such as Touch and Type of EasyMonkeyDevice to complete many basic functional tests.

The advantage of using monkeyrunner is that the environment where the test program runs is real.

The monkeyrunner's limitations cannot be used for functional verification through Assert as in unit testing.


A null pointer error is reported when Android automated testing runs MonkeyRunner. Which of the following gives me some advice?

Device = MonkeyRunner. waitForConnection (1.0, 'xiaomi-mi_2a-208d43a1 ')

This is probably because the device is not connected. If you have only one device connected to your computer, do not add anything in the brackets. If you have to add anything, the connection time is longer. Change 1.0 to 5.0.

Android monkeyrunner tool automation is reflected in

Monkeyrunner test Type
1. Multi-Device Control: the monkeyrunner API can implement a test suite across multiple devices or simulators. You can connect to all devices at the same time or start all simulators (or all together), connect to each device in sequence based on programs, and then run one or more tests. You can also use a program to start a configured simulator, run one or more tests, and then close the simulator.
2. function test: monkeyrunner can automatically perform a function test for an application. You can provide the input values of buttons or touch events, and then observe the screenshots of the output results.
3. regression testing: monkeyrunner can run an application and compare the result screenshots with the results screenshots that are known to be correct to test the stability of the application.
4. scalable automation: Since monkeyrunner is an API toolkit, you can develop a complete set of systems based on Python modules and programs to control Android devices. In addition to the monkeyrunner API, you can also use standard Python OS and subprocess modules to call Android tools such as Android Debug Bridge.

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.