[Go] Android automated test using Java call Monkeyrunner (v)

Source: Internet
Author: User

Android automated testing using Java Invoke Monkeyrunner It is well known that in general we use the Monkeyrunner in Android for automated testing using the Python language to write test scripts. However, it has recently been discovered that you can call the Monkeyrunner Api in Java and write test scripts in the Java language. So, a simple study of a bit. Here to do some summary. Hope there is something useful for lunch in the study. At the beginning, the search for a few fragmented tutorials, said when using Java call 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, built here is the Java project, to the project inside the guide package is through: Right-click Project "Properties" Java Build Path "Libraries" Add External JARs "OK. Such as:

After the import, I simply created a test class, then add a Monkeydevice object, and then introduce the required package, the results found that the error, the reference resources can not be found, according to the example of the show, you need to manually add Adbbackend.java files to your project. So, I was searching, on the official web 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, then, monkeydevice problem solved, but the next problem arises, Adbbackend's parent file cannot be found, that is Monkeyrunnerbackend.java, and then I have to search this file , and then manually add it to your project (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. The parent of the final file is always missing:

Toss a few, I feel should not be like this, but search a lot of fragmentary information, almost all with this method one meaning. Finally, I found an answer in a forum that was barely called a solution. He gave me four packs and said that the four package versions I imported were too new (my update to android4.1). After I add it, I can use it. Here is a brief look at my code, say the call problem:
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 for their own defined wait connection time and device id devices = adb.waitforconnection (8000, "Msm8225qrd5");               }//Add launch permissions String action = "Android.intent.action.MAIN";               collection<string> categories = new arraylist<string> ();    Categories.add ("Android.intent.category.LAUNCHER");               Start the main interface you want to test, primarily configure your own package and startup class device.startactivity (NULL, action, NULL, NULL, categories, New hashmap<string, object> (), "Cn.com.fetion/.android.ui.activities.     StartActivity ", 0);            Click on a coordinate Device.touch (202,258,TOUCHPRESSTYPE.DOWN_AND_UP); }}
This code is still relatively simple, the main note 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 second-to-last parameter of this method is to configure the package name and main interface of the apk you want to test, and run Java application to connect to your test device. Other test methods that need to be used, such as touch,drag,type in Monkeyrunner, etc., are all methods in Monkeyrunner, and you can use them simply by encapsulating them. With Python, there's not much difference. Speaking of why to use the Java language call Monkeyrunner to write scripts, I am accustomed to use Python to write, temporarily do not realize what is too big advantage, perhaps the project volume is large, with Java more direct and easy to manage, perhaps is a personal hobby. This remains to be experienced later. Starting Address: http://www.eoeandroid.com/thread-230577-1-1.html

[Go] Android automated test using Java call Monkeyrunner (v)

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.