一、引言
- 描述:UI Automator 是一個(gè)界面測(cè)試框架,適用于整個(gè)系統(tǒng)上以及多個(gè)已安裝應(yīng)用間的跨應(yīng)用功能界面測(cè)試。這里我將會(huì)拿QQ軟件的說(shuō)說(shuō)模塊進(jìn)行測(cè)試。
- 知識(shí)點(diǎn):Ui Automator
- 難度:初級(jí)
- 效果
二、了解(Android官方文檔)
1、UiDevice 類
用于在運(yùn)行目標(biāo)應(yīng)用的設(shè)備上訪問(wèn)和執(zhí)行操作,同時(shí)還能進(jìn)行以下操作:
- 改變?cè)O(shè)備的旋轉(zhuǎn)。
- 按硬件鍵,如“音量調(diào)高按鈕”。
- 按返回、主屏幕或菜單按鈕。
- 打開(kāi)通知欄。
- 截取當(dāng)前窗口的屏幕截圖。
2、UI Automator API
- UiCollection:枚舉容器的界面元素,目的是為了計(jì)數(shù),或者按可見(jiàn)文本或內(nèi)容說(shuō)明屬性來(lái)定位子元素。
- UiObject:表示設(shè)備上可見(jiàn)的界面元素。
- UiScrollable:支持搜索可滾動(dòng)界面容器中的項(xiàng)目。
- UiSelector:表示對(duì)設(shè)備上的一個(gè)或多個(gè)目標(biāo)界面元素的查詢。
- Configurator:可讓您設(shè)置用于運(yùn)行 UI Automator 測(cè)試的關(guān)鍵參數(shù)。
3、UI Automator 查看器
????????查看器是非常重要的工具,主要作用:獲取控件ID、Class等信息,用于配合UiDevice 類來(lái)進(jìn)行測(cè)試。
????????Ui Automator Viewer 工具位于 android-sdk/tools/bin 目錄中。文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-517107.html
三、使用
1、依賴
androidTestImplementation 'androidx.test.uiautomator:uiautomator:2.2.0'
2、代碼
如第一章效果圖,給出代碼文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-517107.html
@RunWith(AndroidJUnit4.class)
public class MainActivityTest {
UiDevice device;
@Before
public void init() {
// 初始化UiDevice實(shí)例
device = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
// 從主屏幕開(kāi)始獲取軟件包名
device.pressHome();
//depth(0) 跳轉(zhuǎn)到主屏幕
String launcherPackage = device.getLauncherPackageName();
device.wait(Until.hasObject(By.pkg(launcherPackage).depth(0)),
1000);
//獲取Context 跳轉(zhuǎn)到com.example.xch.android001包,進(jìn)入android001軟件
Context context = ApplicationProvider.getApplicationContext();
Intent intent = context.getPackageManager().getLaunchIntentForPackage("com.tencent.mobileqq");
// Intent intent = context.getPackageManager().getLaunchIntentForPackage("com.sankuai.meituan"); // 美團(tuán)測(cè)試
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
context.startActivity(intent);
device.wait(Until.hasObject(By.pkg("com.tencent.mobileqq").depth(0)),0);;
}
@Test
public void test() throws InterruptedException {
//自動(dòng)化發(fā)表說(shuō)說(shuō)4
device.findObject(By.text("動(dòng)態(tài)")).click(); // 通過(guò)文字點(diǎn)擊控件
device.findObject(By.res("com.tencent.mobileqq","uae")).click(); // 通過(guò)id點(diǎn)擊控件
Thread.sleep(200); // 延遲,給onCreate時(shí)間
device.findObject(By.res("com.tencent.mobileqq","dsj")).click();
Thread.sleep(200);
device.findObject(By.res("com.tencent.mobileqq","hkl")).click();
Thread.sleep(200);
device.findObject(By.res("com.tencent.mobileqq","itv")).setText("云端new守夜人\nAndroid uiautomator 測(cè)試");
device.findObject(By.res("com.tencent.mobileqq","ivTitleBtnRightText")).click();
}
}
到了這里,關(guān)于【Android自動(dòng)化測(cè)試】Ui Automator技術(shù)(以對(duì)QQ軟件自動(dòng)發(fā)說(shuō)說(shuō)為例)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!