Android Monkey automation test to Java invoke Monkeyrunner api_android

Source: Internet
Author: User
Tags touch in python
As we all know, in general we use the Monkeyrunner in Android for automated testing, using the Python language to write test scripts. However, it was recently discovered that Java can invoke the Monkeyrunner Api to write test scripts in the Java language.
So, it was a simple study. Here are some summaries. I hope it will be useful for the lunch in study.
In the beginning, the search for a few fragmented tutorials, said that the use of Java invoke Monkeyrunner, you need to import the Android SDK tools path in the lib inside the 4 packages:Ddmlib.jar , Guavalib.jar,monkeyrunner.jar,sdklib.jar. Of course, here is the Java project, to the project inside the guide package is through: Right Key engineeringproperties,Java Build Path "Libraries"add External JARs "OK. The following figure:

After the import, I simply created a test class, then added a monkeydevice object, and then introduced the required package, and found that the report was wrong, the referenced resources are not found, according to the example of the hint that need to manually add Adbbackend.java file to your own project. So, I searched a bit, in the official online down to this class (official website address:http://www.java2s.com/Open-Source/Android/android-core/platform-sdk/com/android /monkeyrunner/adb/catalogadb.htm) added, after themonkeydevice problem was solved, but, the next problem appeared,adbbackend parent files can not find, is Monkeyrunnerbackend.java, then I had to search the file and add it to my project manually (address:Http://www.java2s.com/Open-Source/Android/ android-core/platform-sdk/com/android/monkeyrunner/monkeyrunnerbackend.java.htm). As a result, it's like a bottomless pit, always unable to find the parent class of the final file: Figure:

After a while, I feel that it should not be like this, but the search for a lot of fragmented data, almost all with this method is a meaning. Finally, I found an answer in a forum that was barely known as a solution. He gave me four more packages and said that the four packages I had imported were too new (my update to android4.1). I was able to use it when I added it.
Here's a quick way to stick to my code and say the calling question:
Copy Code code as follows:

Import java.util.ArrayList;
Import java.util.Collection;
Import Java.util.HashMap;
Import Com.android.monkeyrunner.adb.AdbBackend;
Import Com.android.monkeyrunner.core.IMonkeyDevice;
Import Com.android.monkeyrunner.core.TouchPressType;
public class MyTest2 {
private static Imonkeydevice device;
private static adbbackend adb;
public static void Main (string[] args) {
TODO auto-generated Method Stub
if (adb==null) {
adb = new Adbbackend ();
Parameters are defined for their own waiting connection time and device ID, respectively
device = adb.waitforconnection (8000, "Msm8225qrd5");
}
Add Start permission
String action = "Android.intent.action.MAIN";
collection<string> categories = new arraylist<string> ();
Categories.add ("Android.intent.category.LAUNCHER");
Start the main interface to test, primarily by configuring your own packages and startup classes
Device.startactivity (NULL, action, NULL, NULL, categories,
New hashmap<string, object> (), "Cn.com.fetion/.android.ui.activities.startactivity", 0);
Click on a certain coordinate
Device.touch (202,258,TOUCHPRESSTYPE.DOWN_AND_UP);

}
}
This code is relatively simple, the main focus is two points: 1. adb. Waitforconnection (a,b); This method adds the time to wait for startup and the name of your device. 2. StartActivity () The penultimate parameter of this method is to configure the package name and main interface of the apk you want to test.
Run the Java application to connect to your test device. Other test methods that need to be used, such as Monkeyrunner touch ,drag,type, etc., are all methods in Monkeyrunner, and you can use them simply by encapsulating them. There's not much difference when it comes to using Python.
Speaking of why to use the Java language call Monkeyrunner to write script, I am accustomed to write in Python, temporarily did not realize what is too much advantage, perhaps the project volume is large, with Java more direct and easy to manage, Maybe it's just a personal hobby. This is to be experienced later.
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.