Summary of Android automation using Monkeyrunner

Source: Internet
Author: User
Tags x2 y2 appium

Summary of Android automation using Monkeyrunner

Android automation can be used not only for automated testing of Android apps, but also for some other very interesting automated tasks. common automatic chemicals include Monkeyrunner, Robotium, and Appium. monkeyrunner is an automated testing tool provided by Android. It allows you to extract elements from the UI of Android devices and perform operations such as touch and drag. With HierarchyViewer and other modules, you can easily perform automated operations.

First, you must install the Android development environment and run the Monkeyrunner script to install the Jython environment. jython allows you to use the syntax format of Python to write automated test code. Therefore, it is very advantageous for Python developers. some modules in Python cannot be directly used in Jython. You need to install them for Jython. For details, refer

 

Http://stackoverflow.com/questions/3256135/importing-python-modules-in-jython. For example, install the bottle module, jython ez_setup.py bottle, and then import the module during use.

 

 

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
Using Monkeyrunner for Android automation can be divided into the following types of operations: Device and UI interface operations, UI Interface Element extraction, comparison, etc.

 

1. device and UI operations

In fact, it is enough to use the adb that comes with the development environment for operations on Android devices, and Monkeyrunner encapsulates the following adb operations. The common adb operations are as follows:

 

Adb install xxx.apk install apk file adb shell am start-an com. xxx. xxx /. mainActivity starts APPadb shell am force-stop com. xxx. xxx stops the APPadb shell input keyevent KEYCODE_HOME to simulate the Android HOME button adb-s emulator-5554 shell input text test_to_input. The adb shell input tap x y simulates the screen touch operation on a specific simulator. adb shell input swipe x1 y1 x2 y2 simulates the screen sliding operation adb devices to view all online Android devices.

 

The detailed adb command can be queried through adb-h. The operations on the device in Monkeyrunner are 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 (250,450, 'down _ AND_UP ') device. drag (1080/2, 1700), (1080/2, 400), 0.5, 1) device. type ("text to type") device. shell ("input text" + "text to input") device. press ("KEYCODE_HOME") # In addition, you can also use the id to perform the touch operation. In this case, you can introduce the By module to find the corresponding elements through id. from com. android. monkeyrunner. easy import EasyMonkeyDevice, Byeasy_device = EasyMonkeyDevice (device) easy_device.touch (. id ('Id/button1 '), easy_device.DOWN_AND_UP)
The above method is actually the same as the adb shell operation, but it is convenient for you to call it in the Jython script file.

 

2. UI element Extraction

Monkeyrunner can use HierarchyViewer to parse the elements of the UI interface. The parsing result is consistent with that of DDMS and Android Device Monitor in Android Studio.

First, you need to parse the UI interface, and then you can extract the element through the element id and other attributes, and parse all its attributes.

 

From com. android. monkeyrunner import MonkeyRunner, MonkeyDevicefrom com. android. chimpchat. hierarchyviewer import HierarchyViewerdevice = MonkeyRunner. waitForConnection (5, "emulator-5554") hViewer = device. getHierarchyViewer () # parse the current UI View content = hViewer. findViewById ('Id/content') # Use the id to find the corresponding element memberView = content. children [0] text = memberView. namedProperties. get ('text: mtext '). value. encode ('utf8') desc = memberView. namedProperties. get ('accesssibility: getContentDescription ()'). value. encode ('utf8') mleft = memberView. namedProperties. get ('layout: mleft '). value. encode ('utf8') height = memberView. height

 

It is a common practice to use HierarchyViewer to parse hierarchical relationships on the interface and find specific elements by id. however, there are many elements in the Android APP that do not have the corresponding id (this can be seen through the parsing results in DDMS or AVD, if we want to precisely find a specific element, we can only achieve it by further parsing the children of an element, which is troublesome, but often very accurate.

It should be noted that using HierarchyViewer and searching for elements by id may occasionally cause errors, prompting that the corresponding elements cannot be found. if you encounter an element that is difficult to parse, you can use another module AndroidViewClient for parsing. the principle is similar. sometimes it is much simpler to write than 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 of AndroidViewClient is browse.

 

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, the above two methods may fail to parse the UI element, probably because of the imperfection of the corresponding Android tool. because DDMS and AVD often fail to parse a UI.

You can consider the specific application scenarios. In short, you can perfectly obtain the required elements.

3. Comparison

This is a unique Monkeyrunner method. It is often used to obtain the execution result by comparing the front and back of the device screen.

 

Path = "/tmp/images" image = device. takeSnapshot () # image. writeToFile (path + "home page" .decode('00008'00000000now0000'.png ', 'png') # Compare the previous one. # Find the image to be compared in the file, compare with the image1 result = MonkeyRunner. loadImageFromFile ('/tmp/images/result.png') # determine whether the image acquaintance is 90% if (image. sameAs (result, 0.9): log. write ("image comparison successful ...... \ N ") else: log. write (" Comparison of homepage images failed ...... \ N ")
The above is some basic content for Android automation through Monkeyrunner.

 

 

Below, we will record some pitfalls that people may encounter to benefit the masses.

Monkeyrunner may encounter the following pitfalls:

1. Question about Chinese Input

By default, Monkeyrunner only supports Ascii encoding. Therefore, Chinese characters cannot be entered through adb input or type. copy to PC clipboard and paste it to the Android simulator.

However, it should be noted that the clipboard content in the Android simulator is the clipboard content when the current PC focus is switched from the PC desktop environment to the Simulator Interface instantly. in common cases, when the clipboard content in the PC environment is pasted to the Android simulator through the Monkeyrunner script file, it is often unable to paste what we want. for debugging purpose, we will check whether the clipboard in the current PC environment is what we need. then move the mouse focus to the simulator and often find that the required content can be pasted ., however, this is actually a reason for the time difference, that is, the clipboard content in the PC is correct, and then switched to the simulator interface. The clipboard content is brought from the PC environment, of course, correct. on the contrary, after the Monkeyrunner script is executed, the focus of the current PC is switched to the simulator before the clipboard operation, and the clipboard content is incorrect. it's a bit messy. You can think about it and practice it on your own.

A student on github wrote a small tool to conveniently execute clipboard operations in the Android simulator.

2. Time consumed for each operation in the Monkeyrunner script

When you use HierarchyViewer and AndroidViewClient to parse interface elements during Monkeyrunner script execution, you will find that the findViewById operation consumes a lot of time. especially when there are many elements on the UI interface, the time consumption is very obvious. however, when interface switching is involved, we often determine whether the interface is successfully switched based on whether the current interface contains an id element. in most cases, we do not need to determine the current interface based on the id. getHierarchyViewer (). focusedWindowName () is enough for us to judge the vast majority of UI interfaces, and the efficiency is definitely not an order of magnitude improvement.

3. Algorithms for switching between UI Interfaces

We often encounter switching between several specific UI interfaces. However, due to the instability of the Android automation environment and tool itself, the screen switching latency is often caused, and the clicking or switching fails, the APP gets stuck or even returns. this is the time to test the coding capability and algorithm skills. in this regard, I am very grateful to the professor for your help.

We can pre-define the focusdWindowName of all common interfaces and the structure of the Operation behavior required for screen switching, as shown in figure

 

SCREEN_SWITCH_ACTION = {'activity1': {'activity1': None, 'activity2': ('left _ low', 'activity1'), # For example, LEFT_DOWN is required to switch from Activity2 to Activity1. 'activity3': ('left _ low', 'activity1'),}, 'activity2': {'activity1': ('mid _ low', 'activity2 '), 'activity2': None, 'activity3': ('mid _ low', 'activity2'),}, 'activity3': {'activity1': ('Right _ low ', 'activity3'), 'activity2': ('Right _ low', 'activity3'), 'activity3': None ,},}
As shown above, the key in the dictionary is the target screen, and its value represents the operation action required to switch from the current screen to the target screen. here, LEFT_DOWN can be a simple touch button, or a set of operations that determine the Operation Behavior Based on the interface and elements. with the above structure, we only need to write a corresponding algorithm to parse and execute the operation from the structure during screen switching. fault tolerance mechanisms such as screen waiting and failure retry can be added at will.

 

In fact, there is too much space for research and improvement on this algorithm level. If you are interested, you can further discuss it. Thank you for your advice.

Of course, except for Monkeyrunner, tools such as Robotium and Appium are also very frequently used.
The above is a summary of the Monkeyrunner automation, which is relatively simple. You are welcome to criticize and correct it.

 

 

 



 


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.