A summary of Android automation using Monkeyrunner

Source: Internet
Author: User
Tags appium

Using Android automation, not only can you automate testing your Android app, it can also be used for other very interesting automation tasks. Common automation tools are Monkeyrunner, Robotium, Appium and so on. Monkeyrunner is an automated test tool with Android, allowing users to extract elements from the UI interface of Android devices, perform touch and drag operations, With modules such as Hierarchyviewer, it is very easy to automate the operation.

First, the user needs to install the Android development environment and run the Monkeyrunner script to install the Jython environment. Jython allows you to write automated test code using the Python syntax format. So there are advantages for Python developers. Some of the individual modules in Python cannot be used directly in Jython, and you need to install them for the Jython version, depending on how you

Http://stackoverflow.com/questions/3256135/importing-python-modules-in-jython. If you install the bottle module, Jython ez_setup.py bottle, and then import the module when you are using it.

Import syssys.path.append ('/home/jython2.5.3/lib/site-packages/bottle-0.12.7-py2.5.egg ') from bottle import bottle, Run, request, response, GET, post
Android Automation using Monkeyrunner can be divided into the following types of operations: Device and UI interface operation, UI interface element extraction, contrast, etc.

1, device and UI interface operation

In fact, when it comes to the operation of Android devices, it is enough to use the ADB with the development environment, and Monkeyrunner also encapsulates the ADB operations below. Common ADB operations are as follows:

ADB install xxx.apk apk file for adb shell am start-an com.xxx.xxx/. Mainactivity start appadb shell am force-stop com.xxx.xxx Stop the appadb shell input keyevent keycode_home simulate Android's HOME button Adb-s E mulator-5554 Shell input text test_to_input operations for a specific emulator adb shell input tap x y analog screen touch operation adb shell input swipe x1 y1 x 2 y2 analog screen swipe operation ADB devices view all online Android devices.

Detailed ADB commands can be queried by adb-h. The operation of the device in Monkeyrunner is as follows:

From com.android.monkeyrunner Import Monkeyrunner,monkeydevicedevice = monkeyrunner.waitforconnection (5, " emulator-5554 ") Device.shell (" Am start-an com.xxx.xxx/. Mainactivity ") Device.touch ((1080/2, 1700), 1080/2) 0.5,1 (" Down_and_up ") Device.drag (the" Text to type ') Device.shell ("Input text" + "text to input") device.press ("Keycode_home") # In addition, you can use the ID to do touch, The by module can be introduced at this time, so it is very convenient to find the corresponding element by ID. From com.android.monkeyrunner.easy import easymonkeydevice, Byeasy_device = Easymonkeydevice (device) Easy_device.touch (by.id (' Id/button1 '), Easy_device. DOWN_AND_UP)
The above approach is in fact consistent with the operation of the ADB shell, just to make it easier for users to invoke the Jython script file.

2, UI interface element extraction

Monkeyrunner can parse the elements of the UI interface through Hierarchyviewer, and the results are consistent with DDMS and Android Device Monitor in Android Studio.

You first need to parse the UI interface, and then you can extract the element by element ID and other attributes, and parse all of its properties.

From Com.android.monkeyrunner import Monkeyrunner, monkeydevicefrom com.android.chimpchat.hierarchyviewer Import Hierarchyviewerdevice = Monkeyrunner.waitforconnection (5, "emulator-5554") Hviewer = Device.gethierarchyviewer () # Parsing the current UI view content = Hviewer.findviewbyid (' id/content ')  # Find the corresponding element by id Memberview = Content.children[0]text = MemberView.namedProperties.get (' Text:mtext '). Value.encode (' UTF8 ') desc = MemberView.namedProperties.get (' Accessibility:getcontentdescription () '). Value.encode (' utf8 ') mleft = MemberView.namedProperties.get (' layout: Mleft '). Value.encode (' utf8 ') height = memberview.height

It is common practice to use Hierarchyviewer to parse the hierarchical relationships of interfaces and to find specific elements based on their IDs. However, there are many elements in the Android app that do not have a corresponding ID (this can be seen through the parsing results in DDMS or AVD), At this point, if we want to pinpoint a specific element, it can only be done by further parsing the children of an element, which can be cumbersome, but often very precise.

It is important to note that using hierarchyviewer and finding elements by ID occasionally makes an error, suggesting that the corresponding element cannot be found. If you encounter elements that are really hard to parse, Consider using another module, androidviewclient, for parsing. The principle is very similar. Even sometimes, the wording is much simpler than the hierarchyviewer.

VC = Viewclient (Device=device, serialno= "emulator-5554") content = Vc.findviewbyid (' id/content ') Memberview = Content.children[0]text = Memberview.gettext () x = Memberview.getx () y = memberview.gety () height = memberview.getheight ( )

The project address for Androidviewclient is https://github.com/dtmilano/AndroidViewClient. When using a note, we first write the androidviewclient into the environment variable, Then import the Androidviewclient module, then import the Monkeyrunner and the corresponding other modules, or you will not find the androidviewclient error. As for why, you can try it on your own to understand.

Import sysreload (SYS) sys.setdefaultencoding ("Utf-8") Android_view_client_home = os.environ[' Android_view_client_ HOME ']sys.path.append (android_view_client_home + '/src ') from com.dtmilano.android.viewclient import Viewclient, Viewfrom com.android.monkeyrunner import Monkeyrunner, monkeydevicefrom com.android.monkeyrunner.easy Import Easymonkeydevice, byfrom com.android.chimpchat.hierarchyviewer import hierarchyviewer

However, in both of these ways, it is possible for UI element parsing to fail, possibly due to imperfections in the Android tool itself. Because DDMS and AVD often occur when a UI interface cannot be resolved.

Specific application scenarios Let's think for yourself, in short, to get the elements you need perfectly.

3, contrast

This is a very distinctive way of monkeyrunner, which is often used to obtain a judgment of the execution result by comparing the screen to the device.

Path = "/tmp/images" image = Device.takesnapshot () # Image.writetofile (path+ "Main Page". Decode (' utf-8 ') +now+ '. png ', ' PNG ') # The following is the beginning of the comparison of the previous # go to the file to find the picture we want to compare with the image1 to compare result = Monkeyrunner.loadimagefromfile ('/tmp/images/result.png ') # Determine if the image acquaintance is for 90%if (Image.sameas (result,0.9)):    log.write ("Picture comparison succeeded ... \ n") Else:    log.write ("Main Page image comparison failed ... \ n")
Above, is the basic content of Android automation through Monkeyrunner.


Below, will be easy to meet some of the pits recorded, for the benefit of the masses of the people.

Monkeyrunner easy to encounter some pits:

1, Chinese input problem

Monkeyrunner only supports ASCII encoding by default, so in Chinese, it is not possible to enter it through the input and type of ADB. Then you can copy to the PC Clipboard and then paste it into the Android emulator.

However, it is important to note that the contents of the Clipboard inside the Android emulator are the contents of the Clipboard when the current PC's focus is switched from the PC desktop environment to the emulator interface. A common scenario is when you paste the Clipboard contents from the PC environment to the Android emulator by monkeyrunner a script file. It often appears that we do not paste the content we want. At this point, for debugging purposes, we will check the Clipboard in the current PC environment, whether it is what we need. And then move the mouse focus into the emulator, and often find the right content to paste on. And yet, this is actually a time lag reason, That is, the clipboard content of the PC is correct, then switch to the emulator interface, the Clipboard content is brought from the PC environment, of course, is correct. Instead, after the Monkeyrunner script executes, the focus of the current PC is switched to the emulator before the clipboard operation. You will find that the Clipboard contents are incorrect. It's a little messy, we can think about it and practice it.

A classmate on GitHub wrote a small tool that makes it very easy to perform clipboard operations in the Android emulator, https://github.com/bingwei/inputchineseviaadb. Very good, everyone can try. Of course, Encountered some special characters, still need to do some simple escape and other operations.

2, Monkeyrunner time-consuming issues for each operation in the script

During Monkeyrunner script execution, when interface element parsing is performed using Hierarchyviewer and androidviewclient, it is found that the time consuming of Findviewbyid operation is very high. Especially when there are so many elements in the UI interface, Time-consuming is obvious. However, when it comes to interface switching, we often determine whether the interface is successful depending on whether an element in the current interface contains an ID. Then, in most cases, we do not need to judge the current interface by ID, by Windowname = Device.gethierarchyviewer (). Focusedwindowname () This approach is enough for us to make most of the UI-interface judgments, and is definitely not an order of magnitude improvement in efficiency.

3. Algorithmic issues related to switching between UI interfaces

We often encounter, obviously in a few specific UI interface between the switch, but due to the Android Automation environment and the tool itself instability, often cause screen switching delay, click or switch is not successful, Apps get stuck and even flash away with some very vexed questions. So this is the time to test your coding and algorithmic skills. At this point, thank you very much for the help of the students, praise a.

We can pre-define a structure of all common interface focusdwindowname and the operation behavior required for screen switching, such as

Screen_switch_action = {    ' Activity1 ': {        ' Activity1 ': None,        ' Activity2 ': (' Left_down ', ' Activity1 '), # like, Switching from Activity2 to Activity1 requires a left_down operation.        ' Activity3 ': (' Left_down ', ' Activity1 '),    },    ' Activity2 ': {        ' Activity1 ': (' Mid_down ', ' Activity2 '),        ' Activity2 ': None,        ' Activity3 ': (' Mid_down ', ' Activity2 '),    },    ' Activity3 ': {' Activity1 ':        (' Right_ Down ', ' Activity3 '),        ' Activity2 ': (' Right_down ', ' Activity3 '),                ' Activity3 ': None,    },}
As above, the key in this dictionary is the target screen, and its value represents the action required to switch from the current screen to the target screen. Where left_down can be simply touch a button, You can also write a collection of responsible actions that determine the behavior of the action based on the interface and elements. With this structure, we only need to write a corresponding algorithm to parse the operation and execute it from the structure when the screen is switched. such as screen waits, failure retries and other fault-tolerant mechanisms, can be arbitrarily added.

In fact, the problem involved in this algorithm level, the space for research and improvement is really too big. Interested students can be more in-depth discussion, welcome advice.

Of course, in addition to Monkeyrunner, Robotium and Appium and other tools are also very high usage, each has its pros and cons.
These, is the Monkeyrunner automation of some summary, write a relatively brief, welcome criticism correct.








A summary of Android automation using Monkeyrunner

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.