How to setup UiAutomator 1.0

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");
    }
  }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市互艾,隨后出現(xiàn)的幾起案子试和,更是在濱河造成了極大的恐慌,老刑警劉巖纫普,帶你破解...
    沈念sama閱讀 216,402評論 6 499
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件阅悍,死亡現(xiàn)場離奇詭異,居然都是意外死亡昨稼,警方通過查閱死者的電腦和手機节视,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,377評論 3 392
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來假栓,“玉大人寻行,你說我怎么就攤上這事∝揖#” “怎么了拌蜘?”我有些...
    開封第一講書人閱讀 162,483評論 0 353
  • 文/不壞的土叔 我叫張陵杆烁,是天一觀的道長。 經(jīng)常有香客問我简卧,道長兔魂,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,165評論 1 292
  • 正文 為了忘掉前任举娩,我火速辦了婚禮析校,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘晓铆。我一直安慰自己勺良,他們只是感情好绰播,可當(dāng)我...
    茶點故事閱讀 67,176評論 6 388
  • 文/花漫 我一把揭開白布骄噪。 她就那樣靜靜地躺著,像睡著了一般蠢箩。 火紅的嫁衣襯著肌膚如雪链蕊。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,146評論 1 297
  • 那天谬泌,我揣著相機與錄音滔韵,去河邊找鬼。 笑死掌实,一個胖子當(dāng)著我的面吹牛陪蜻,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播贱鼻,決...
    沈念sama閱讀 40,032評論 3 417
  • 文/蒼蘭香墨 我猛地睜開眼宴卖,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了邻悬?” 一聲冷哼從身側(cè)響起症昏,我...
    開封第一講書人閱讀 38,896評論 0 274
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎父丰,沒想到半個月后肝谭,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,311評論 1 310
  • 正文 獨居荒郊野嶺守林人離奇死亡蛾扇,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,536評論 2 332
  • 正文 我和宋清朗相戀三年攘烛,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片镀首。...
    茶點故事閱讀 39,696評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡医寿,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出蘑斧,到底是詐尸還是另有隱情靖秩,我是刑警寧澤须眷,帶...
    沈念sama閱讀 35,413評論 5 343
  • 正文 年R本政府宣布,位于F島的核電站沟突,受9級特大地震影響花颗,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜惠拭,卻給世界環(huán)境...
    茶點故事閱讀 41,008評論 3 325
  • 文/蒙蒙 一扩劝、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧职辅,春花似錦棒呛、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,659評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至秀鞭,卻和暖如春趋观,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背锋边。 一陣腳步聲響...
    開封第一講書人閱讀 32,815評論 1 269
  • 我被黑心中介騙來泰國打工皱坛, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人豆巨。 一個月前我還...
    沈念sama閱讀 47,698評論 2 368
  • 正文 我出身青樓剩辟,卻偏偏與公主長得像,于是被迫代替她去往敵國和親往扔。 傳聞我的和親對象是個殘疾皇子贩猎,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,592評論 2 353

推薦閱讀更多精彩內(nèi)容

  • 作為一個大二的糙妹子,真正重視護膚應(yīng)該是大學(xué)開始瓤球,雖然時間很短融欧,但是成效非常顯著,所以來給大家送bonus啦~這是...
    IsabellaDU閱讀 505評論 0 4