MonkeyRunner tool for Android Automated Testing

Source: Internet
Author: User

MonkeyRunner tool for Android Automated Testing 1. What is the monkeyrunnermonkeyrunner tool provides an API that allows programs written using this API to control Android devices and Simulators out of Android code. With monkeyrunner, you can write a Python program to install an Android Application or test package, run it, send a simulated press key to it, and capture its user interface image, and stored on the workstation. The primary purpose of the monkeyrunner tool is to test applications and devices at the functional/framework level, or to run the unit test suite, but you can also use it for other purposes. Ii. Differences between the monkeyrunner tool and the Monkey tool Monkey: The Monkey tool runs directly in the adb shell of the device or simulator to generate a pseudo-random event stream of the user or system. Monkeyrunner: The monkeyrunner tool is a specific command and event control device or simulator defined on the workstation through APIS. Iii. monkeyrunner test Type 1. Multi-Device Control: the monkeyrunner API can implement Test suites across multiple devices or simulators. You can connect to all devices at a time or start all simulators at a time, connect to each device in sequence based on programs, and then run one or more tests. You can also use a program to start a configured simulator, run one or more tests, and then close the simulator. 2. function test: monkeyrunner can automatically perform a function test for an application. You can provide the input values of buttons or touch events, and then observe the screenshots of the output results. 3. regression testing: monkeyrunner can run an application and compare the result screenshots with the results screenshots that are known to be correct to test the stability of the application. 4. scalable automation: Since monkeyrunner is an API toolkit, you can develop a complete set of systems based on Python modules and programs to control Android devices. In addition to the monkeyrunner API, you can also use standard Python OS and subprocess modules to call Android tools such as Android Debug Bridge. 4. Run monkeyrunner. You can directly run monkeyrunner using a code file, or enter the monkeyrunner statement in the Interactive dialog box. Either way, you need to call the monkeyrunner command under the tools subdirectory of the SDK directory. If you provide a file name as a running parameter, monkeyrunner regards the file content as a Python program and runs it. Otherwise, it provides an Interactive dialog environment. The command syntax of monkeyrunner is: monkeyrunner-plugin <plugin_jar> <program_filename> <program_options> example. Prerequisites: a device is connected (this can be a real device or a simulator). 1. Place apidemos.apk under $ Android_Root \ tools. 2. Create a monkeyrunnerprogram under $ Android_Root \ tools. py file, according to the official help documentation, the content of this file is: 1 # Imports the monkeyrunner modules used by this program 2 3 from com. android. monkeyrunner import MonkeyRunner, MonkeyDevice, MonkeyImage 4 5 # Connects to the current device, returning a MonkeyDevice object 6 7 device = MonkeyRunner. waitForConnection () 8 9 # INSTALLThe Android package. notice that this method returns a boo Lean, so you can test10 11 # to see if the installation worked.12 13 device. installPackage ('. /ApiDemos.apk ') 14 15 16 # Runs the component17 18 device. startActivity (component = 'com. example. android. apis /. apiDemos ') 19 20 21 # Presses the Menu button22 23 device. press ('keycode _ MENU ', 'down _ AND_UP') 24 25 26 # Takes a screenshot27 28 result = device. takeSnapshot () 29 30 31 # Writes the screenshot to a file 32 33 result. writeToFile ('. /shot1.png ', 'png') 3. Open the command line and go to the Android_Root \ tools directory to run the command: monkeyrunner monkeyrunnerprogram. py130215 23:35:19. 711: I [main] [com. android. monkeyrunner. monkeyManager] Monkey Command: wake.130215 23:35:20. 711: I [main] [com. android. monkeyrunner. monkeyManager] Monkey Command: wake.130215 23:35:21. 711: I [main] [com. android. monkeyrunner. monkeyManager] Monkey Command: wake.130215 2 3:35:22. 835: S [pool-1-thread-1] [com. android. monkeyrunner. adb. adbMonkeyDevice] Error starting command: monkey -- port 12345130215 23:35:22. 835: S [pool-1-thread-1] [com. android. monkeyrunner. adb. adbMonkeyDevice] com. android. ddmlib. shellCommandUnresponsiveException130215 23:35:22. 835: S [pool-1-thread-1] [com. android. monkeyrunner. adb. adbMonkeyDevice] at com.android.ddmlib.AdbHelper.exe cuteRemoteCommand (AdbHelper. java: 408) 130215 23:35:22. 835: S [pool-1-thread-1] [com. android. monkeyrunner. adb. adbMonkeyDevice] at com.android.ddmlib.Device.exe cuteShellCommand (Device. java: 276) 130215 23:35:22. 835: S [pool-1-thread-1] [com. android. monkeyrunner. adb. adbMonkeyDevice] at com. android. monkeyrunner. adb. adbMonkeyDevice $ 1.run( AdbMonkeyDevice. java: 89) 130215 23:35:22. 835: S [pool-1-thread-1] [com. android. monkeyrunne R. adb. adbMonkeyDevice] at java. util. concurrent. executors $ RunnableAdapter. call (UnknownSource) 130215 23:35:22. 835: S [pool-1-thread-1] [com. android. monkeyrunner. adb. adbMonkeyDevice] at java. util. concurrent. futureTask $ Sync. innerRun (Unknown Source) 130215 23:35:22. 835: S [pool-1-thread-1] [com. android. monkeyrunner. adb. adbMonkeyDevice] at java. util. concurrent. futureTask. run (Unknown Source) 130215 23:35:22. 835: S [pool-1-thread-1] [com. android. monkeyrunner. adb. adbMonkeyDevice] at java. util. concurrent. threadPoolExecutor $ Worker. runTask (Unknown Source) 130215 23:35:22. 835: S [pool-1-thread-1] [com. android. monkeyrunner. adb. adbMonkeyDevice] at java. util. concurrent. threadPoolExecutor $ Worker. run (UnknownSource) 130215 23:35:22. 835: S [pool-1-thread-1] [com. android. monkeyrunner. adb. adbMonkeyDevice] at java. lang. T Hread. run (UnknownSource) 130215 23:35:55. 632: I [main] [com. android. monkeyrunner. monkeyManager] Monkey Command: press KEYCODE_MENU.130215 23:35:58. 311: I [main] [com. android. monkeyrunner. monkeyManager] Monkey Command: quit.4. shot1.png can be generated under Android_Root \ tools. 6. instance improvement because no MENU appears when you press the MENU key on the ApiDemos homepage. To make it more visualized, continue the experiment on the basis of the previous experiment: 1. Create a monkeyrunnerprogram1.py file under $ Android_Root \ tools with the following content: 1 # Imports the monkeyrunner modules used by this program 2 3 from com. android. monkeyrunner import MonkeyRunner, MonkeyDevice, MonkeyImage 4 5 # Connects to the current device, returning a MonkeyDevice object 6 7 device = MonkeyRunner. waitForConnection () 8 9 # Takes a screenshot10 11 result = device. takeSnapshot () 12 13 # Writes the screenshot to a file14 15 result. writeToFile ('. /shotbegin.png ', 'png') 16 17 # Presses the Down button18 # Here is the main content we add 19 device. press ('keycode _ DPAD_DOWN ', 'down _ AND_UP') 20 21 device. press ('keycode _ DPAD_DOWN ', 'down _ AND_UP') 22 23 device. press ('keycode _ DPAD_DOWN ', 'down _ AND_UP') 24 25 device. press ('keycode _ DPAD_DOWN ', 'down _ AND_UP') 26 27 device. press ('ke YCODE_DPAD_DOWN ', 'down _ AND_UP') 28 29 # Takes a screenshot30 31 result = device. takeSnapshot () 32 33 # Writes the screenshot to a file34 35 result. writeToFile ('. /shotend.png ', 'png') www.2cto. com2: locate the screen on the homepage of Apidemos, and then the cursor is positioned on the first line. 3. Run the command in the $ Android_Root \ tools Directory: monkeyrunner monkeyrunnerprogram1.py4. During the running process, we can see that the cursor keeps moving down and can be customized in the current directory: before running: shotbegin.pngafter running, shotend.png (five KEYCODE_DPAD_DOWN operations are performed in the Code)

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.