1. Preparation
Do some preparations.Here is what I use:
Eclipse,Ant,Android.
And install them will take steps.Before we use eclipse,we'll need JDK.
a. Here is some reference about install Eclipse.
b. Here is some reference about install JDK.
c. Here is some reference about install ant.
d. Here is some reference about install Android SDK.
To install these,we need to be patient,especially when it comes to setting path.
It's not very complicated,but we need to follow the instruction step by step.
If the URLs are not available,we may go and search on the Internet.
2. Create a JUnit test case
After the preparation is ready,we can start to create a project.Open Eclipse,click "File-new-Java project",set a name for the project you like,such as "ExoSuperSpeed".
Then "next-Libraries-add external JARs",choose "android.jar"
and "uiautomator.jar".
We need to choose the proper version and keep in mind the version number.Also click "Add Library",choose "JUnit4",click "finish",now we've created a project.
Then create a package,and under which we create a "Junit Test case",set a name.For some version reason,I delete the "import org.junit.Test" and "@Test" in java script.
3. Write code
Now we start to write some codes.But two things we need to watch out:
NO.1 Your main class has to extend "UiAutomatorTestCase".
NO.2 Your main test has to be named starting with "test".
You may wish to use uiautomator.bat to help locate elements.You can find it in
"Android\sdk\tools".
4. Create build.xml file
Find the path for tools in android such as
"C:\USERS\XXX\Android\sdk\tools",and in cmd window,type command
"CD C:\USERS\XXX\Android\sdk\tools".After successfully changing the dictionary,type "Android list",you will get a list.The next order is "android create uitest-project -n XXX -t XXX -p XXX".
After
-n you can name the .jar file you wish;
-t you choose the proper id to match the android version of "android.jar" and "uiautomator.jar";
-p you show the path of the current project.
For example the command may be "android create uitest-project -n ExoPlay01 -t 1 -p C:\Users\XXX\workspace\ExoSuperSpeed".
After building,you will see "Add file build.xml".
5. Build .jar file
We will transfer build.xml file to .jar file by ant.Refresh your project,you will find the build.xml file under JUnit4.Right click on the file,choose "Run as-Ant build",then cancel the choice of "help XXX" and choose "build XXX",run.
Wait a few seconds we will see "BUILD SUCCESSFUL",and something like "[jar] Building jar : C:\User\XXX\workspace\ExoSuperSpeed\bin\ExoPlay01.jar"
6. Push
I push the .jar file to tested device using the command "adb push".
The command may be like
"adb push C:\User\XXX\workspace\ExoSuperSpeed\bin\ExoPlay01.jar /data/".
Wait and see the result.
7. Test
After all we've done,we finally come to the test.In the console type command like "uiautomator runtest /data/ExoPlay01.jar".
So we can just get the test result now.
8. Notice
For all the steps,some things we need to watch out.
1st,make sure your Android and android device supports each other.That is,no version issue.
2nd,do not forget to add "android.jar","uiautomator.jar" and "JUnit4".
3rd,before we start to write some code,make sure we are creating a JUnit test case.
4th,in the main test,we have to name it beginning with "test" to be recognized as a test.
5th,if we use adb push but get "permission denied",that may be caused by permission.We can push the file to another path or do "adb root" then push again.
BTW,I'll show my simple code here.Besides,I'm trying to do some uiautomator 2.0 by Android Studio,that will take time.
I'm new for this,but I'll surely be happy either you can get something from this or you are willing to help me improve this.
public class SuperSpeed extends UiAutomatorTestCase{
public void test() throws InterruptedException, IOException {
String ShellCommand_RunExo = "am start -W "
+ "com.google.android.exoplayer.demo/.SampleChooserActivity";
Runtime.getRuntime().exec(ShellCommand_RunExo).waitFor();
System.out.println("start the app");
UiScrollable ExoPlayerDemo = new UiScrollable(new UiSelector()
.scrollable(true));
ExoPlayerDemo.setAsVerticalList();
UiObject SuperSpeedPlayReady = new UiObject(new UiSelector()
.text("Super speed (PlayReady)"));
try {
ExoPlayerDemo.scrollIntoView(SuperSpeedPlayReady);
System.out.println("scroll until the sight of the exact button");
} catch (UiObjectNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(SuperSpeedPlayReady.exists()){
try {
SuperSpeedPlayReady.click();
} catch (UiObjectNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else{
System.out.println("Cannot locate SuperSpeedPlayReady");
}
}
}