Android UiAutomator 1&2
阿新 • • 發佈:2018-11-23
目錄
UiAutomator1 官網 教程1 教程2
1、JDK / SDK / ANT 安裝
2、新建java工程,新增junit、Android庫(sdk\platforms\android-xx下面的android.jar和uiautomator.jar android-17以上)
3、新建java類:
public class Test extends UiAutomatorTestCase { public void testDemo() throws UiObjectNotFoundException{ UiDevice.getInstance().pressHome(); UiObject browserObject = new UiObject(new UiSelector().text("Browser")); browserObject.clickAndWaitForNewWindow(); UiObject editoObject = new UiObject(new UiSelector().className("android.widget.EditText")); editoObject.click(); UiDevice.getInstance().pressDelete(); editoObject.setText("www.baidu.com"); UiDevice.getInstance().pressEnter(); sleep(2000); } }
4、cmd下執行android list
,檢視API 大於15的SDK的ID值
5、cmd下執行android create uitest-project -n <name> -t <android-sdk-ID> -p <path>
其中name為將來生成的jar包的名字,可以自己定義,android-sdk-ID為上一步驟看到的2,path是上面2中的專案路徑。執行完成後會在專案根目錄下生成build.xml檔案
6、cmd進入專案根目錄,執行ant build
,執行完後會在bin目錄下生成jar檔案
7、將jar包push到Android手機中,adb push <jar檔案路徑> data/local/tmp
8、在cmd下執行adb shell uiautomator runtest <jar檔名> -c <包名.類名>
進行測試
9、修改程式碼後,需重新執行ant clean-6-7-8
UiAutomator2 教程
UiAutomator1和2兩者的主要區別如下
(a)2.0基於 Instrumentation, 可以獲取應用Context,可以使用Android服務及介面。 (b)2.0基於 Junit4,測試用例無需繼承於任何父類,方法名不限,使用Annotation進行, 1.0需要繼承UiAutomatorTestCase,測試方法需要以test開頭。 (c)2.0採用Gradle進行構建,1.0使用Maven或Ant。 (d)2.0新增UiObject2、Until、By、BySelector等介面。 (e)2.0輸出到Logcat,1.0可以使用System.out.print輸出流回顯至執行端。 (f)2.0輸出為APK,1.0輸出為JAR。
1、JDK / SDK / GRADLE
2、建立一個簡單的Android工程(有無Activity無所謂),需要注意的是minSdkVersion必須大於等於18。
3、 在對應module的build.gradle檔案中,新增對uiautomator的dependends引用
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:26.1.0'
compile 'com.android.support.constraint:constraint-layout:1.1.3'
testCompile 'junit:junit:4.12'
androidTestCompile('com.android.support.test.espresso:espresso-core:3.0.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
// Set this dependency to use JUnit 4 rules
androidTestCompile 'com.android.support.test:rules:0.4'
// Set this dependency to build and run UI Automator tests
androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.1.2'
}
總的build.gradle
apply plugin: 'com.android.application'
android {
compileSdkVersion 26
buildToolsVersion "28.0.1"
defaultConfig {
applicationId "com.testcrash.qiaoqiao.uiautomator20"
minSdkVersion 18
targetSdkVersion 26
versionCode 1
versionName "1.0"
//使用android的runner執行指令碼
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:26.1.0'
compile 'com.android.support.constraint:constraint-layout:1.1.3'
testCompile 'junit:junit:4.12'
androidTestCompile('com.android.support.test.espresso:espresso-core:3.0.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
// Set this dependency to use JUnit 4 rules
androidTestCompile 'com.android.support.test:rules:0.4'
// Set this dependency to build and run UI Automator tests
androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.1.2'
}
4、在專案的androidTest下會自動生成ExampleInstrumentedTest,同級下新增測試class-SalaryShowAppTest
5、SalaryShowAppTest原始碼如下
package com.testcrash.qiaoqiao.uiautomator20;
import android.content.Context;
import android.content.Intent;
import android.os.RemoteException;
import android.support.test.InstrumentationRegistry;
import android.support.test.runner.AndroidJUnit4;
import android.support.test.uiautomator.UiDevice;
import android.util.Log;
import org.junit.Test;
import org.junit.runner.RunWith;
import java.io.IOException;
/**
* Description:
* Created by quxun on 2018-11-23.
*/
@RunWith(AndroidJUnit4.class)
public class SalaryShowAppTest {
private final String TAG = getClass().getName();
private String mPackageName = "com.tencent.mm";
private String mActivity = "com.tencent.mm.ui.LauncherUI";
@Test
public void case1() {
UiDevice mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
try {
if (!mDevice.isScreenOn()) {
mDevice.wakeUp();
}
} catch (RemoteException e) {
e.printStackTrace();
}
mDevice.pressHome();
startApp(mPackageName);
mDevice.waitForWindowUpdate(mPackageName, 5 * 2000);
closeApp(mDevice, mPackageName);
mDevice.waitForIdle(10000);
startApp(mDevice, mPackageName, mActivity);
mDevice.waitForWindowUpdate(mPackageName, 5 * 2000);
closeApp(mDevice, mPackageName);
}
private void startApp(String packageName) {
Context mContext = InstrumentationRegistry.getContext();
Intent mIntent = mContext.getPackageManager().getLaunchIntentForPackage(packageName);
mContext.startActivity(mIntent);
}
private void closeApp(UiDevice uiDevice, String packageName) {
Log.i(TAG, "closeApp: ");
try {
uiDevice.executeShellCommand("am force-stop " + packageName);
} catch (IOException e) {
e.printStackTrace();
}
}
private void startApp(UiDevice uiDevice, String packageName, String launchActivity) {
try {
uiDevice.executeShellCommand("am start -n " + packageName + "/" + launchActivity);
} catch (IOException e) {
e.printStackTrace();
}
}
}