Modify the android monkey source code to support automated UI Testing

Source: Internet
Author: User

On the Android platform, If You Want To implement automated testing, there are several methods, such as using the instrumentation-based class library and its testing class, or using monkeyrunner. the test case runs on the PC, use USB or WiFi to send test commands to Android apps. The two solutions have their own advantages and disadvantages:

1. The advantage of the instrumentation technology is that the technology used to write UI automation test cases is the same as the technology used to write Android applications, and the API is also used as a class library of Android itself. However, the disadvantage is that the app to be tested must use a key for Digital Signature debugging. This is because the Android platform requires an app to read or modify the status of another app, must be the same entity, and the evidence of the same entity is to use the same key for digital signature. Therefore, if you use the instrumentation technology to test a third-party application, you must first remove the digital signature of the third-party application and re-use the debug Key signature to perform the test. If a resource file in an application needs to be decrypted using the original digital signature, a problem may occur when the re-signed application is started. For example, this is the case for the Android version of QQ. After a new signature is signed, it will crash if it is restarted.
-It is a good practice to prevent others from maliciously reusing intellectual property rights, but it is another situation for automated testing.

2. The disadvantage of monkeyrunner is that it can only run on a PC. The operating principle of monkeyrunner is that the test case is interpreted on the PC and sent to the Android mobile app through the network. If it is required to be on a mobile phone and completely isolated from the PC, it cannot be tested. For example, to test the network speed and communication quality of a carrier, the tester can take several popular applications on the market, write several automated test cases, and then perform the test on a handheld mobile device.

I have encountered a similar test requirement that I can only run on my mobile phone. I wrote automated test cases for Android apps like QQ that cannot be re-signed ...... Only monkey is the testing technology that can meet this requirement. However, Google only designs monkey as a program for randomly sending key messages-just like monkeys, random clicks on the keyboard of the mobile phone, but no support for the capture control similar to instrumentation for the monkey.

In order to meet the test requirements, we modified the monkey source code to implement the QQ test case on the mobile phone real machine instead of the PC without re-signing. The modification principle is as follows:

1, Android SDK has a tool hierarchyviewer can display Android mobile app UI layout, we also achieved support for iOS hierarchyviewer, interested users can refer to: http://www.cnblogs.com/vowei/archive/2012/08/13/2614468.html

2. The principle of hierarchyviewer is to establish a socket connection with the viewserver running the Android mobile phone (this connection can communicate with viewserver through WIFI or USB port) and send commands to viewserver.

A) For example, when hierarchyviewer sends the dump-1 command, viewserver will return the UI tree layout of the entire android system to hierarchyviewer.

B) hierarchyviewer parses the return string again. In this process, my colleague Liu binhua wrote a very good article: http://www.cnblogs.com/vowei/archive/2012/08/08/2627614.html.

3. Because hierarchyviewer runs on a PC and viewserver runs on a mobile phone, the actual process of capturing the android UI control tree is completed by viewserver, hierarchyviewer is just a simple client.

4. While the Android system is multi-task, several programs can run on the background at the same time, and the process of capturing the UI control tree on the mobile phone becomes very simple:

A) Establish a socket connection to the local viewserver.

B) Like hierarchyviewer, send commands to viewserver and parse the return string, which can be parsed using our IQUERY: https://github.com/vowei/iQuery

C) by default, viewserver is disabled and needs to be started explicitly.

The following is the key code. Open the monkey main function source file, monkey. Java, in its main function:

1. After monkey calls getsysteminterfaces, add the following line to explicitly start viewserver-the listening port number is 4939.

   1:  monkey.mWm.startViewServer(4939);

2. Create a socket in the monkey source code and connect it to the viewserver on the local machine:

   1:  Socket socket = new Socket();
   2:  socket.connect(new InetSocketAddress("127.0.0.1", 4939));

3. Communicate with viewserver:

   1:  BufferedWriter out = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
   2:  BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream(), "utf-8"));
   3:  out.write("DUMP -1");
   4:  out.newLine();
   5:  out.flush();
   6:  String line = null;
   7:  int maxCount = 0;
   8:  while ( (maxCount < 4) && ((line = in.readLine()) != null) ) {
   9:  ... ...
  10:  }

This is because starting viewserver on a mobile phone is an operation that a Super User can do-you need to open a network port and use a root-User device. The running mode is as follows:

1. Find a root device and install a terminator application.

2. Start the terminal simulator program on your mobile phone and execute the following commands in sequence:

   1:  export CLASSPATH=/sdcard/monkey.jar
   2:  cd /sdcard
   3:  su
4: # The following command starts QQ by default and prints the text on the four textview controls on the QQ logon Interface
   5:  app_process /system/bin com.android.commands.monkey.Monkey
6: # Run the following command to run the specified Android Application and print the text on the first four textview controls on the interface. The first parameter is the package name, and the second parameter is the full name of the activity to be started.
   7:  app_process /system/bin com.android.commands.monkey.Monkey com.android.development com.android.development.Development

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.