MonkeyRunner learning for android automated testing (2)

Source: Internet
Author: User

The MonkeyRunner tool has three classes: MonkeyRunner, MonkeyDevice, and MonkeyImage.
1. MonkeyRunnner class:
This class is mainly used to connect MonkeyRunner to a real machine or simulator. In addition, there is an alert method that displays the prompt information and a selection method that is commonly used.
Ii. MonkeyImage class:
This class is used to save the test, save the image to various formats, and compare two MonkeyImage objects.
There are two common methods: writeToFile (string path, string format), which is the storage location and name. You can save the image to sdcard or your computer; sameAs (MonkeyImage other, float percent), which is used to compare whether the two are the same and the result is a boolean value.
Iii. MonkeyDevice class:
This class has a few more methods. It mainly provides methods for installing and uninstalling packages, starting an activity, and operating the keyboard. StartActivity (string uri, string action, string data, string mimetype, iterable categories dictionary extras, component, flags) in the image to start an application, press (string name, dictionary type) click controls, touch (integer x, integer y, integer type) Click controls (positioning by coordinates), drag (tuple start, tuple end, float duration, integer steps) drag controls and other methods are commonly used.
The MonekeyRunner test tool is not difficult to use. The following code briefly explains:
FetionTest. py
# Coding = the first sentence of the UTF-8 Python program, add this sentence to make the comment support Chinese
# Import the required MonkeyRunner package, which can also be a self-written class. If there are many files
From com. android. monkeyrunner import MonkeyRunner, MonkeyDevice, MonkeyImage
# To connect to a real machine, enter the correct name of your real machine. Run the cmd command to view the name using adb devices.
Devices1 = MonkeyRunner. waitForConnection (5, "1234567890 ABCDEF ")
# Connection Simulator
Devices2 = MonkeyRunner. waitForConnection (5, "emulator-5554 ")
# Start the main interface of Apsara stack. This is my test of Apsara stack program. It mainly includes the package name of Apsara stack and the startup Activity.
(Component = 'cn. com. fetion/. android. ui. activities. startactive ')
(Component = 'cn. com. fetion/. android. ui. activities. startactive ')
# Sleep for 8 seconds to facilitate Account Logon
MonkeyRunner. sleep (8.0)
# Click my friends. The coordinate method is used here.
Devices1.touch (202,258, "DOWN_AND_UP ")
# Selecting friends
Devices1.touch (222,326, "DOWN_AND_UP ")
Devices1.touch (442,325, "DOWN_AND_UP ")
# Click "add to blacklist"
Devices1.touch (223,563, "DOWN_AND_UP ")
# Perform the following operations and save the image.
Result1 = device1.takeSnapshot ()
Result1.writeToFile ('e: \ slack01.png ', 'png ')
# Click the return key to find any key value created on the simulator, which can be found in the official documentation. ('keycode _ back', MonkeyDevice. DOWN_AND_UP)
Result2 = device1.takeSnapshot ()
Result2.writeToFile ('e: \ slack02.png ', 'png ')
This program is just like this. In addition, you can optimize your own program, encapsulate different test cases into a method, or set some configurations to global variables, then it is called in different classes. This requires a brief understanding of Python programming knowledge.
The following describes a useful tool for obtaining coordinates. It is a tool used to obtain the coordinates of a real machine. When we click the space on the real machine, the coordinates of the click points on the real machine are displayed.
This is a python file that can be defined by yourself. There are only a few words:
From com. android. monkeyrunner import MonkeyRunner as mr
From com. android. monkeyrunner. recorder import MonkeyRecorder as recorder
# Add your real machine name
Device = mr. waitForConnection (5, "MSM8225QRD5 ")
Recorder. start (device)
For example, if I define it as and put it on the G disk and run monkeyrunner in cmd, the following interface will appear:

When we click on this tool, the real machine will also be executed, and the coordinates of the positions we click in each step will be displayed. For example, when I click to open the music player, the record will be as follows:

This tool is good only when you have used it several times, the advantages of this tool are shown. In addition, you must note that this cannot be used to obtain the coordinates of the simulator. The coordinates on the simulator can only be obtained by the next tool. This is indeed a pain point.
Software tools are like this and there is no absolute convenience. MonkeyRunner is no exception. Well, this time I wrote so much about it. This tool is easy to use and more needs to be explored by myself. I hope you can share and exchange more information.


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: 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.