UiAutomator2.0入門


總是聽說UiAutomator這個框架,但從來沒有使用過。找了篇入門,實踐一下。實踐之后感覺,uiautomator寫測試代碼,還是有點費勁。接口名比較多,比較長。網易的atx里使用的uiautomator相當於原生uiautomator的python wrapper。接口就顯得簡潔很多。

1. 新建項目

使用推薦的android studio創建android空工程。File->New -> New project,在application name處填上工程名,點擊 Next, 在target android devices頁面,選擇 支持的phone and tablet的最小sdk。這個可根據自己設備上的android版本選擇。點出next,先把add no activity,完成了一個空工程的新建。

2. 配置

在Module:app里的build.gradle,新增綠色的內容。

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.0', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })

    compile 'com.android.support:appcompat-v7:24.+'
    testCompile 'junit:junit:4.12'
    androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.1.1'

}

 我使用的gradle版本為2.14.1,相應的android plugin version是2.1.3。之前配置不正確,總是提示gradle sync不成功,然后測試代碼就不能正常運行。另外,如果dependencies里的插件配置有重復,沒有下載到本地的,還需要處理好。將項目設置為Android Tests, 可看到目錄結構為下圖所示。與test相關的文件背景變為綠色。

3. case代碼

case代碼寫在androidTest目錄下,示例內容如下:

@RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest {
     private UiDevice mUIDevice = null;
    private Context mContext = null;
    String APP = "XXX";

    @Before
    public void setUp() throws RemoteException{
        mUIDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());  //獲得device對象
        mContext = InstrumentationRegistry.getContext();

        if(!mUIDevice.isScreenOn()){  //喚醒屏幕
            mUIDevice.wakeUp();
        }
        mUIDevice.pressHome();  //按home鍵
    }

    @Test
    public void test1(){
        Intent myIntent = mContext.getPackageManager().getLaunchIntentForPackage(APP);  //啟動app
        mContext.startActivity(myIntent);
        mUIDevice.waitForWindowUpdate(APP, 5 * 2000);
        UiObject sender = mUIDevice.findObject(new UiSelector().text("Send"));  //定位text內容為Send的控鍵
        try {
            sender.click();  //點擊按鍵
        }catch (Exception e){
            e.printStackTrace();
        }
        assertTrue(true); //斷言,隨便亂寫的,此處未起到斷言作用
    }

}

連接設備,點擊運行測試代碼后,可看到運行的日志如下。

$ adb push D:\AndroidBook\m_adr_atom_hotel\HelloUI2\app\build\outputs\apk\app-debug.apk /data/local/tmp/com.example.mandasun.helloui2
$ adb shell pm install -r "/data/local/tmp/com.example.mandasun.helloui2"
	pkg: /data/local/tmp/com.example.mandasun.helloui2
Success


$ adb push D:\AndroidBook\m_adr_atom_hotel\HelloUI2\app\build\outputs\apk\app-debug-androidTest-unaligned.apk /data/local/tmp/com.example.mandasun.helloui2.test
$ adb shell pm install -r "/data/local/tmp/com.example.mandasun.helloui2.test"
	pkg: /data/local/tmp/com.example.mandasun.helloui2.test
Success


Running tests

$ adb shell am instrument -w -r -e package com.example.mandasun.helloui2 -e debug false com.example.mandasun.helloui2.test/android.support.test.runner.AndroidJUnitRunner
Client not ready yet..
Started running tests
Tests ran to completion.

  從日志中可以看到,uiautomator將測試代碼打成的app-debug.apk包和app-debug-androidTest-unaligned.apk推到adr機上,然后安裝。之后,使用命令運行了這兩個 apk。

本文參考文檔:uiautomator 2.0 demo與使用  uiautomator使用入門官方教程  android測試之UI自動化測試工具Uiautomator介紹


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM