First of all, the problem caused by a BUG
If there is a BUG in the R&D process: "There is a screen phenomenon when the mobile phone language is constantly switched". How to verify this question? I think the best way is automatic testing.
So, what tasks can automated testing accomplish?
Simply put, those repeated tests can be completed by automation:
1, set the phone language.
2. Add, delete and collect contacts.
Step 3 dial and hang up
4. Even send and receive text messages.
If you need the above functions, start the journey of automation.
Second, the introduction of Android automated testing
Android automated testing is mainly divided into Monkeyrunner, Rubotium, UiAutomator, Monkey (in my opinion, this does not count) and so on. Main features:
1, Monkeyrunner: Advantages: the operation is the simplest, and the test script can be recorded and the operation can be visualized; Disadvantages: it mainly generates automatic calculation of coordinates, which is not portable and has the most limited function;
2.Rubotium: mainly used for automatic testing of APK. APK can have source code or no source code, which is powerful; The disadvantage is that the operation is aimed at APK, which needs to be re-signed (with tools) and the operation is relatively complicated;
3.UiAutomator: Advantages: All operations can be automated and easy to operate; Disadvantages: Android version needs to be higher than 4.0, and it cannot be operated by control id. Relatively speaking, the function is limited, but it is also enough;
4. Monkey: Accurately speaking, this is not an automated test, because it can only generate random events and cannot be carried out according to the established steps;
From the above introduction, we can draw the conclusion that to test an APK, you can choose Rubotium;; The test process may involve multiple apks, so choose UiAutomator;; Some simple tests, choose Monkeyrunner;;
This paper mainly introduces the use of UiAutomator.
Third, environmental construction.
3. 1, prerequisite:
1、JDK
2.SDK(API is higher than 15)
3.Eclipse (installing ADT plug-ins)
4.ANT (for compiling and generating jar)
3.2, brief steps:
1. Install JDK and add environment variables.
After installation, be sure to add environment variables through JAVA_HOME, that is, first create the JAVA_HOME variable, and then add% JAVA _ HOME% \ bin.
2. Add SDK environment variables.
Be sure to establish ANDROID_HOME first, and then add% ANDROID _ HOME% \ tools to the path.
3. Install Eclipse and ADT plugins.
4. Install the ANT tool and add environment variables.
In addition, make sure to create the %ANT_HOME% variable first, and then add %ANT_HOME%\bin to the path.
Fourth, the detailed operation
4. 1, prepare the project
Create a new Java project with Eclipse. Note that it is not an Android project!
4.2, add JUnit library
Next-> Library-> Add library
4.3. Add an Android library
Add external jar:
Find the path Android-SDK \ platforms \ Android-17 \ and add the following android.jar and uiautomator.jar:
After adding all the libraries, it should look like this:
4.4. Add the package in src, and then add the class file.
The file content is:
[java] View Plain Text
Bao com
Import com.android.uiautomator.core.uiobject;
import com . Android . ui automator . core . uiobjectnotfoundexception;
Import com; Android; uiautomator; core; uiscrollable;
Import com; Android; uiautomator; core; uiselector;
Import com; Android; uiautomator; testrunner; uiautomatortestcase;
Public class Runner extension UiAutomatorTestCase {
Public void testDemo () throws UiObjectNotFoundException {
getUiDevice()。 press home();
//Enter the setting menu.
ui object setting app = new ui object(new ui selector()。 Text ("Settings"));
setting app . click();
//Sleep for 3 seconds
Try {
thread . sleep(3000);
} catch(interrupted exception e 1){
// TODO automatically generated catch block
e 1 . printstacktrace();
}
//Input language and input method settings
ui scroll able setting items = new ui scroll able(new ui selector()。 True));;
ui object languaandinputitem = setting items . getchildbytext(
New UiSelector (). Text ("language & input"), "language & input", true);
languageandinputitem . clickandwaitfornewwindow();
}
}
The above project path is in e:\workspace\AutoRunner, and the full name of the class is com. As for the specific function of Runner, we don't care anymore.
4.5. Find the SDK ID
CMD enter the \Android-sdk\tools\ directory and run the command:
Android list
Check the ID value of SDK corresponding to android version, which is currently 60;
4.6. Create a build file.
Still in the \Android-sdk\tools\ directory, run the command:
Android create ui test-project-n & lt; Name & gt-t & lt; < Android-SDK-ID & gt; -p & lt; Path & gt
For example:
Android create ui test-project-n AutoRunner-t60-p e:\ workspace \ AutoRunner
The above name is the name of the jar package generated in the future, which can be defined by yourself. Android-sdk-ID is the 6 seen above; Path is the path of the new project created by Eclipse; After running this command, the build.xml file will be generated in the root directory of the project. If not, check the above steps.
4.7. Compile and generate jar
CMD enters the project directory of the project, and then runs ant build, which will use ant to compile and generate jar. If successful, you will be prompted:
Then the jar file will be generated in the bin directory.
4.8. Push and run the jar.
Adb pushes < Jar file path >; Data/local /tmp
Adb shell uiautomator runs the test < Jar file name >-c <; Class name in the project, including package name >;
For example:
ADB push e:\ workspace \ AutoRunner \ bin \ AutoRunner . jar data/local/tmp
ADB shell ui automator runtest autorunner . jar-c com。 Runner
Then you can see that the phone will automatically follow the steps in Runner. The specific effect is to enter the setting menu and then enter the "Language and Input Method" menu.
Code analysis of verb (abbreviation of verb)
Let's start with a few of the most important objects.
5. 1, UiDevice object
The method of getUiDevice () can obtain an object of UiDevice, through which some actions of the device can be completed:
Click (integer x, integer y)
-Click the pixel indicated by (x, y).
Press Back ()
Press Delete ()
Press enter ()
Press Home ()
Press the menu ()
Press search ()
-Click the corresponding button.
Wake up ()
-When the phone is off-screen, wake up the screen to unlock.
Slide (start, start, end, end, step)
-Slide on the phone, from (startX, startY) to (endX, endY). The number of steps indicates that the sliding distance is completed in several steps. The fewer the numbers, the larger the sliding range.
setOrientationLeft()
setOrientationRight()
-Turn the phone in the corresponding direction.
setOrientationNatural()
-Restore the rotating state of the mobile phone to normal.
5.2.UiSelector object
This object can be understood as a conditional object, describing a condition. Often used with UiObject to get some (some) eligible control objects.
Checked (Boolean value)
-describes a relationship with a check status of val.
Class name (class name)
-describes an object relationship named className.
Clickable (Boolean value)
/g 199209 17/ article/details/16 13 1565