MonkeyRunner-errors importing other modules, monkeyrunner

Source: Internet
Author: User

MonkeyRunner-errors importing other modules, monkeyrunner

After learning about it, I just learned what Android can do by providing MonkeyRunner. The official documentation is very clear. To put it simply, you can use a Python program through APIS,

  • Installan Android application or test package,
  • Runs it,
  • Sends keystrokes to it,
  • Takes screenshots of its user interface,
  • Stores screenshots on the workstation.

It also provides easy package, including EasyMonkeyDevice and By, which are used to access control elements through control IDs. However, mass production devices (non-development non-simulators) cannot use Hierarchy Viewer or easy package. The official note is,

To preserve security, Hierarchy Viewer can only connect to devices running a developer version of the Android system.

There is still a way to break through this restriction, which is a little troublesome to operate and can only temporarily discard the convenience brought.

Therefore, the control can only be operated through the control coordinates.

Fortunately, the MonkeyRecorder can reduce the burden.

from com.android.monkeyrunner.recorder import MonkeyRecorder as recorderrecorder.start(dev)

This allows you to easily record the coordinate values of click operations. You can also use the monitor Tool to conveniently obtain the coordinate ranges of controls in each view.

With monitor, you can create two configuration files, one for storing the coordinates of all controls, and the other for storing the arrival path starting from start Activity for each test case.
Does the configuration file use xml or json? After json is selected, nearly lines of control coordinates are written in the masked header, and the control list function is initialized with the resolved coordinate values of the configuration file loaded in Python. Then, monkeyrunner is used to run the function and falls into the trap.

import json

Import error is returned for this row.

After Google knows the cause, MonkeyRunner uses Jython 2.5.3, which may be based on Python 2.5 and Python 2.7 Before the json module is available.

Is it hard to use xml instead? Is there nearly a hundred lines of json configuration files written in white?

Google, more than half of the articles about MonkeyRunner are simple. For example, connect, startActivity, and press. There are no multiple combined touch events. I believe that many people have used MonkeyRunner in depth, but it is hard for those who have used it in depth to write no blog, at the same time, when I write a blog, I only touch and cut a picture after running an App?

Fortunately, stackoverflow finally crawled out of the trap following several similar answers. Detailed steps for crawling.

One solution is to install simplejson for Python 2.5, taking Mac OS X as an example.

You can see the Jython jar file in the Android SDK directory. The version is 2.5.3.

android-sdks/tools/lib/jython-standalone-2.5.3.jar

Download simplejson, copy it to the Python 2.5 directory, decompress it, and install it.

/Library/Python/2.5/site-packages/tar -xzf simplejson-3.6.5.tar.gzpython setup.py install

Then, the import error is returned in MonkeyRunner.

import simplejson

Check Jython path. There is no simplejson path.

import syssys.path

The simplejson path is added and then imported.

sys.path.append('/Library/Python/2.5/site-packages/simplejson-3.6.5')

But it becomes invalid after exiting.
Solution 1 is to use PYTHONPATH, solution 2 is to dynamically add in the Python script, the addition also needs to judge the repetition, it is best to standardize the path, do some processing for the Windows system.
Here we use the dynamic addition method in the Code, regardless of the Windows system.

 

import sysif not ('/Library/Python/2.5/site-packages/simplejson-3.6.5' in sys.path):    sys.path.append('/Library/Python/2.5/site-packages/simplejson-3.6.5')try:    import jsonexcept ImportError:    import simplejson as json

 

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.