一、打開build.gradle(app)添加依賴,後同步:
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
// 模塊添加依賴
androidTestImplementation 'com.android.support.test.uiautomator:uiautomator-v18:2.1.2'
}
二、通過sdk/tooles/bin/uiautomatorviewer.bat打開工具,獲取元素
三、核心定位元素
resource-id 資源id
text 文字屬性,一般是顯示在組件上的文字
content-desc 描述
checked 選擇屬性,常見開關、單元、複選的開與關的狀態
focused 焦點屬性,如輸入框光標在閃動,說明焦點在輸入框
四、搜索對象 BySelector
資源id:res(sourceName)
通過text:text(text)
通過描述定位:desc(desc)
focused(boolean isFocused)
查找資源id:
mUidevice.findObject(By.res('com.android.calculator2:id/digit_7')).click()
mUidevice.findObject(By.text('7')).click()
mUidevice.findObject(By.desc('減')).click()
輸入文本:.setText('1234');
五、基本模擬動作
1.1、常見操作 類 UiObject2
點擊:public boolean click()
滑動:public boolean longClick()
拖動:public void drag(Point dest)
mUidevice.findObject(By.res("com.xxxx:id/xxx")).drag(new Point(224,1213))
輸入: public boolean setTest(String text)
1.2、常見設備操作 類 UiDevice
點擊 click(int x,int y)
按鍵 pressKeyCode(int keyCode)
滑動 swipe(int startX,int startY,int endX,int endY,int steps) -----一個步長5毫秒
(1)public boolean pressSearch() ,點擊查找功能鍵
(2)public boolean pressBack() ,點擊後退鍵
(3)public boolean pressDelete() ,點擊刪除鍵
(4)public boolean pressEnter() ,點擊回車鍵
(5)public boolean pressHome(),點擊Home鍵
(6)public boolean pressMenu(),點擊菜單鍵
(7)public boolean pressRecentApps(),點擊在運行的APP鍵
(1)public boolean openNotification(),展開通知欄
(2)public boolean openQuickSettings(),展開快速設置欄
六、示例代碼
package mk.com;
import android.app.Instrumentation;
import android.os.RemoteException;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import androidx.test.platform.app.InstrumentationRegistry;
import androidx.test.uiautomator.By;
import androidx.test.uiautomator.UiDevice;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
@RunWith(AndroidJUnit4.class)
public class MKTest {
public Instrumentation mlnstrumentation = InstrumentationRegistry.getInstrumentation();;
public UiDevice ud = UiDevice.getInstance(mlnstrumentation);
int h = ud.getDisplayHeight();
int w = ud.getDisplayWidth();
@Before
public void setUp() {
ud.pressBack();
ud.pressBack();
ud.pressBack();
ud.pressBack();
}
@Test
public void DemoTest() throws RemoteException, InterruptedException {
// 以上爲套路代碼
ud.findObject(By.text("微信")).click();
Thread.sleep(2000);
System.out.println("========屏幕寬:"+w+",屏幕高:"+h);
// 滑動
// Boolean s = ud.swipe(w/2,(new Double(h*0.25)).intValue(),w/2,(new Double(h*0.75)).intValue(),1);
// 點擊查找圖標
ud.click((new Double(w*0.82)).intValue(),(new Double(h*0.06)).intValue());
Thread.sleep(2000);
// 搜索二卡聯通
ud.findObject(By.text("搜索")).setText("二卡");
Thread.sleep(2000);
// 點擊出現的聯繫人第一個
ud.click((new Double(w*0.5)).intValue(),(new Double(h*0.19)).intValue());
Thread.sleep(2000);
// 點擊輸入
ud.findObject(By.focused(true)).setText("晚安!");
Thread.sleep(2000);
// 點擊發送
ud.findObject(By.text("發送")).click();
}
}