Batch script to implement automated test for Android applications _dos/bat

Source: Internet
Author: User
Tags prepare

Test platform: Compatible with all Android platforms (2.3-4.2)

Test background: Because of the need for the Product SDK to do interface testing, and these interfaces need to be called in the app, so developed a simple Android application (such as the diagram) to invoke the interface needs testing, each button in the app is actually a test module.

  

Test requirements:

1, because some interfaces in the first installation of the program to call, you need to implement automated installation turn off uninstall test, you can set the number of repetitions.
2, because of the need to test the stability of the interface, each button every day to run many pieces, so realize a simple click of the UI Automation, loop click.
3, automatically check the collection of Logcat log and resolve log results, automatically send mail.

The following is mainly about how to achieve the above requirements under Windows.

  First, prepare test environment and test documents
1. Install Java JDk and set up environment variables.
2, install the Android SDK, set the environment variable (detailed steps slightly)
3, prepare to be measured apk and test some of the tools needed

Then the main test how to achieve the main program

  Second, start the simulator, and unlock

Batch script:

Copy Code code as follows:

EMULATOR-AVD Galaxy4.2:: Start simulator
Ping-n 127.0.0.1>nul:: Waiting for Emulator to start successfully,-N 90 is a wait time, it is recommended to set a larger
ADB shell input keyevent 82:: The simulator opens and locks the screen, ADB simulates keyboard input, unlocks
Ping-n 2 127.0.0.1>nul

Third, script parameter initialization

Copy Code code as follows:

Set appname=testandroid.apk:: The name of the program under test
Set Pkgname=com.example.testandroid:: Program package name under test
Set openname=com.example.testandroid.mainactivity:: Test program main activity
Set "times=%1":: Test times, script incoming parameters
xcopy Blat.exe C:\Windows\System32\/y:: Copy mail tool to System folder, close 360 guards

Four, loop to perform installation turn off uninstall

Copy Code code as follows:

echo Test start >source.txt:: Create a source file log
FOR/L%%a in (1,1,%times%) do (
ADB install testandroid.apk:: Installing APK
ADB shell am start-w-N%pkgname%/%openname%:: Open apk
Call cmd/c Close.bat%pkgname%:: Closing apk
ADB uninstall%pkgname%:: Uninstalling APK
ADB logcat-d |findstr "^d/k.*}\>":: Filter logcat, get what you need
ADB logcat-c:: Clear Logcat Log
taskkill/f/im Adb.exe) >>source.txt:: End adb process, prevent source files from being occupied
:: Every action log is recorded in source

 V. Process Source.txt to extract key information and send mail

Copy Code code as follows:

Start Javaw-jar Filehandler.jar:: Call the processing log jar, which needs to be adjusted according to the different apk, if you do not need to remove
Ping-n 127.0.0.1>nul

:: The following is the tool for sending mail Blat, see the details http://blog.csdn.net/qiming_zhang/article/details/6065824
Set from=**@163.com
Set user=**
Set pass=**
Set to=**@**
Set subj=apk loading test results
Set Mail=result.txt
Set Attach=source.txt
Set server=smtp.163.com
Blat-install%server%%from% 3 25
Blat%mail%-to%to%-base64-charset gb2312-subject%subj%-attacht%attach%-server%server%-F%
from%-u%user%-pw%pass%

Six, set Windows timed task, the parameter fills in the test number

After the installation uninstall test has been completed, the following steps explain how to use the ADB command for UI Automation testing

First, open the application, Record button or text in the mobile phone screen coordinate point

1, in Android 4.0 above version, can display the mobile phone screen coordinates in real time.
2, click Settings-Application-developer tools-pointer location
3, open the application, get the coordinates of the button position, such as figure coordinates (138.168)

Second, the script simulates the screen clicks the event

Copy Code code as follows:

ADB shell sendevent/dev/input/event0 3 0 138:: x coordinates
ADB shell sendevent/dev/input/event0 3 1:: Y-coordinate
ADB shell sendevent/dev/input/event0 1 330 1:: Press
ADB shell sendevent/dev/input/event0 0 0 0
ADB Shell sendevent/dev/input/event0 1 330 0:: Leave
ADB shell sendevent/dev/input/event0 0 0 0

This simulates the screen-click event, and to test for long press, set the wait time

Third, script simulation keyboard events

As shown in the figure, enter the number 134 in text

Script simulates keyboard events

Copy Code code as follows:

ADB shell input KeyEvent 8
ADB shell Input KeyEvent 10
ADB shell Input KeyEvent 11

This allows the ADB-simulated UI Automation script to be written, and the advantage of this writing is that it is quick and easy to automate for simple logic.

Conclusion: The above is the whole process of requirements implementation, of course, this method can also be used in Ubuntu, Mac and other Unix systems, just the corresponding batch processing language can be changed to shell language.

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.