Android Automation Test (3) <monkeyrunner> find objects by ID &touch&type (python)

Source: Internet
Author: User

I used Java to implement Android automation tests in two previous articles (1) How to install and uninstall an application (Java), Android Automation test (2) Find objects by ID (Java). But in essence, it is implemented with Monkeyrunner corresponding Java lib, but the related documents are very few, if you really want to use Monkeyrunner to do functional automated testing, it is strongly recommended to use the Python language



1, Monkey Runner

The Monkeyrunner tool provides an API for writing programs, control an Android device or emulator from outside of Android OID code.
With the Monkeyrunner, you can write a Python program, the installs an Android application or the test package, runs it,
Sends keystrokes to it, takes screenshots of their 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 run Ning Unit test Suites,
But is free to use it for other purposes.
The Monkeyrunner tool provides an API to program an Android device or emulator from outside the Android source.
You can use Monkeyrunner to write a Python program to install an Android app and test package, run it, send key event, screenshot, exist locally.
The design of the Monkeyrunner tool originates from a test suite that tests applications and equipment at the functional/framework level, and runs unit tests


2, sample implementation

2.1 Code implementation function: Go to Robot Dream's page mainactivity, find the button with ID center_image Click event, and then find the button with ID button1 again in the popup dialog, make two confirmations.

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.mainactivit    Y ') 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.gethierarch Yviewer () print (hierachy_view) View_node = Hierachy_view.findviewbyid (' id/center_image ') Items_node = view_node.c Hildren print (len (view_node.children)) print (' Touch 2 ') Easymonkey.touch (by,monkeydevice.down_and_up) Monkey Runner.sleep (3)    By2 = by.id (' Id/button1 ') print (by2) print (easymonkey.visible (by2)) vn2 = Hierachy_view.findviewbyid (' Id/bu Tton1 ') 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: Enter Robot Dream landing page Loginactivity, according to the ID login_account find the Login edit box texteditor Enter the user name ABC, according to the ID of Login_ PWD Find password edit box texteditor Enter password abc123, finally find the login button ID Login_button, successfully login

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 ') Monkeyrunne R.sleep (3) by = By.id ("Id/login_account") Print (Easymonkey.visible (by)) Easymonkey.type (by, ' abc ') Monkeyrunne R.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.sle    EP (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.pr ESS (' Keycode_back ')


3. Summary:

Many basic functional tests can be done with Monkeyrunner using the ID to find the control, using a few basic operations like touch, type, and so on Easymonkeydevice.

Using the benefits of Monkeyrunner, the test program runs in an environment that is real in the context of the scenario.

With the limitations of Monkeyrunner, it is not possible to use assert for functional verification as unit tests do.

Android Automation Test (3) <monkeyrunner> find objects by ID &touch&type (python)

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.