The Monkeyrunner of Android automated testing starts from scratch

Source: Internet
Author: User

Recently, because the company is organizing a free code project, that is, the people choose their own research direction to do some of their own research interests. Since I have learned a little bit about Android before, and Android development is now in full swing, but automated testing is basically a new area of contact, not many people, So I gave our team to study the Android automation testing aspects of the project, sure enough to get the unanimous endorsement of everyone's high spirits, hehe. So apart, we began to look at some of the existing automated testing tools on the Android platform. After online search and study , I understand that the current Android SDK comes with the ready-made test tools have monkey and Monkeyrunner two. People don't look alike to each other, but in fact they are totally different two tools that are used in different areas of testing. In general, Monkey is mainly used in stress and reliability testing, running the command can randomly send a variety of simulation keyboard event flow to the target program, and can define the number of times sent, in order to observe the application of the stability and reliability of the test, the application is relatively simple, remember that a few commands on the line. And Monkeyrunner, in contrast, will be a bit more powerful, it can be applied to functional testing , regression testing, and can customize the test extension, flexibility, and testers can fully control.

Well, don't say much nonsense, let's go straight to the subject. The reason why want to write down this process, is because after I this time in the online search and study, found about Monkeyrunner tutorial very little, almost all of the official Android materials to the translation of their own, for the first contact of the students, may not be too detailed. I want to come out here as a complete beginner, really starting from 0, recording my learning process. This article only for the real rookie, prawns are please float over ha, hey.

To use Monkeyrunner, do we have to do God's horse first? Be prepared to use the environment, of course. This is all that we have to install the Android SDK beforehand, configure the development environment (such as Eclipse), install Python (because Monkeyrunner is actually written by Jython, directly call the Android API to complete). These steps on the Internet have a very detailed tutorial, teach everyone to take the environment, I do not have much nonsense here.

After the environment has been set up, we will begin our journey of use. Many of the online monkeyrunner are directly from the instance code, written very sketchy, also did not take detailed steps to explain down, so I really spent two or three days to touch the clue. So I will write very detailed here, as far as possible to help everyone can easily read. The detailed steps are as follows (running on the simulator, because I do not know why the picture is not displayed properly, so I will write the command directly):

1. Open the Andorid simulator with Elipse or open the emulator with the Andorid command in CMD, as follows:

D:\Software\Android SDK2.3\ANDROID-SDK-WINDOWS\TOOLS>EMULATOR-AVD AVD2.3.3

Comments: Navigate to the Tools directory under the path of the Android SDK and run the command above. "AVD2.3.3" is the name of my simulator, this command is named by each person, the use of the time need to change to the actual name.

2. At this time, if everything is normal, the simulator should be able to start up. (You must run the appropriate emulator before running Monkeyrunner, or Monkeyrunner cannot connect to the device.) Many tutorials do not say, I have tried several times to understand it. Next, we open another CMD window (the previous one does not close), or navigate to the Tools directory, enter the command "Monkeyrunner", carriage return, will enter the Shell Command interactive mode.

OK, then you can import the module that Monkeyrunner will use. Of course, our "From...import ..."

Enter directly in the shell command: from Com.android.monkeyrunner import Monkeyrunner, Monkeydevice enter

OK, after this step is completed, we can start "blending" with the simulator, haha, the command is as follows:

device = Monkeyrunner.waitforconnection ()

Without an error, we can then "harass" it by means of a successful blending with the simulator. Next, we can decorate things in it. Enter the following command and pass in the relative path of your apk. This is the famous Apidemos I have installed. If it succeeds, it returns true. If you do not return true, it is generally a syntax error or you pass in a relative path has a problem, carefully check it is no problem.

Device.installpackage (".. /samples/android-10/apidemos/bin/apidemos.apk ")

Next, we can start any activity in it, just pass in the package and activity name. The command is as follows:

Device.startactivity (component = "Com.example.android.apis/com.example.android.apis.apidemos")

The emulator will automatically open the home page of the Apidemos application.

Next we can also send more operations to the simulator, such as simulating any keystroke time and scrolling, etc., and can. The command format is similar to the above command, and I'm not going to be too tired.

Of course, we can not be so troublesome to enter the command line, you can start from "From...import ..." all the subsequent statements are written into a python file, such as monkeyrunnertest.py, Then we can run it directly from the command line through Monkeyrunner. For example, we use the above example, the syntax is as follows:

D:\software\android2.3.3\android-sdk-windows\tools>monkeyrunner monkeyrunnertest.py

Next Monkeyrunner will automatically call monkeyrunnertest.py and execute the statements in it, which is quite handy.

Today for the first time to write here, today the main key is to understand how to make Monkeyrunner and simulator for the correct connection and related operations, specific Monkeyrunner methods and properties can refer to the official documents, the above very detailed, hehe.

Copyright Notice: This article is from Qingchunjun's 51Testing Software Testing blog: http://www.51testing.com/?22381

The Monkeyrunner of Android automated testing starts from scratch

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.