Monkeyrunner_android automated test: using Java to call monkeyrunner

Source: Internet
Author: User

As we all know, we generally use the Python language to write test scripts when using monkeyrunner in Android for automated testing. However, we recently found that you can use Java to call the monkeyrunner API and write test scripts in Java.

So I simply studied it. Here is a summary. I hope it will be useful for the study lunch. At the beginning, I searched for some fragmented tutorials. When using Java to call monkeyrunner, I needed to import four packages in lib under the android SDK tools path: ddmlib. jar, guavalib. jar, monkeyrunner. jar, sdklib. jar. of course, the Java project is built here. to export the package to the project, right-click the project, and choose Properties, Java build path, libraries, add external jars, and OK. For example:
After the import, I created a test class, added a monkeydevice object, and introduced the required package. The result showed that an error was reported and the referenced resource could not be found, according to the prompts In the example, You need to manually add adbbackend. java file to your own project. So, I searched, on the official website up and 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 in, after monkeydevice problem solved, but, the next problem appeared, the parent class file of adbbackend cannot be found, that is, monkeyrunnerbackend. java, then I had to search this file and manually add it to my project (Address: http://www.java2s.com/Open-Source/Android/android-core/platform-sdk/com/android/monkeyrunner/MonkeyRunnerBackend.java.htm
). The result is like a bottomless pit, and the parent class of the final file is always not found:
After a long time, I felt that this was not the case. However, I searched for a lot of fragmented information, which is similar to this method. Finally, I found a solution in a forum. He gave me four packages again, saying that the four packages I imported were too new (I updated them to android4.1 ). I can use it after I add it. Here, I will briefly post my code and discuss the call problems:
 
 
  1. Import java. util. arraylist;
  2. Import java. util. collection;
  3. Import java. util. hashmap;
  4. Import com. Android. monkeyrunner. ADB. adbbackend;
  5. Import com. Android. monkeyrunner. Core. imonkeydevice;
  6. Import com. Android. monkeyrunner. Core. touchpresstype;
  7. Public class mytest2 {
  8. Private Static imonkeydevice device;
  9. Private Static adbbackend ADB;
  10. Public static void main (string [] ARGs ){
  11. // Todo auto-generated method stub
  12. If (ADB = NULL ){
  13. ADB = new adbbackend ();
  14. // The parameters are the custom connection wait time and device ID.
  15. Device = aDb. waitforconnection (8000, "msm8225qrd5 ");
  16. }
  17. // Add the start permission
  18. String action = "android. Intent. Action. Main ";
  19. Collection <string> categories = new arraylist <string> ();
  20. Categories. Add ("android. Intent. Category. launcher ");
  21. // Start the main interface for testing, mainly to configure your own package and startup class
  22. Device. startactivity (null, action, null, null, categories,
  23. New hashmap <string, Object> (), "cn.com. fetion/. Android. UI. Activities. startactivity", 0 );
  24. // Click a Coordinate
  25. Device. Touch (202,258, touchpresstype. down_and_up );
  26. }
  27. }
These codes are still relatively simple, with two points of attention: 1 ). ADB. Waitforconnection (a, B); this method adds the waiting time and the name of the device. 2). The second-to-last parameter of the startactivity () method is to configure the package name and main interface of the APK you want to test. You can connect to your test device by running Java application. Other testing methods, such as touch, drag, and type in monkeyrunner, are all monkeyrunner methods. You can simply encapsulate them and use them. It is no different from Python. For more information, see http://fengbohaishang.blog.51cto.com/5379297/962705. Speaking of why I want to use the Java language to call monkeyrunner to write scripts, I am used to writing in Python, and I have not yet realized the great advantages, maybe when the amount of work is large, java is more direct and easy to manage. It may be my hobby. This is to be experienced in the future. There is a problem left over, that is, how to call monkeyrunner when using the four packages of the higher version. I have not found a solution yet. I am very grateful to anyone who knows this.

This article is from the "Wind Sea" blog, please be sure to keep this source http://fengbohaishang.blog.51cto.com/5106297/1065647

Note: This article is for learning reference only.

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.