您好,登錄后才能下訂單哦!
通過繼承InstrumentationTestRunner,就可以傳遞自定義參數到TestRunner。這里是傳遞port、packageName、activityName,其中port是用于在手機側啟動一個TCP服務的端口,packageName和activityName是用于指定首個啟動的activity的參數配置:
- import android.os.Bundle;
- import android.test.InstrumentationTestRunner;
- public class AthrunInstrumentationTestRunner extends InstrumentationTestRunner {
- private static String packageName = "com.taobao.fario";
- private static String activityName = "com.taobao.fario.MainActivity";
- private static String port = "1234";
- public static String getPort() {
- return port;
- }
- public static String getPackageName() {
- return packageName;
- }
- public static String getActivityName() {
- return activityName;
- }
- @Override
- public void onCreate(Bundle arguments) {
- if (arguments != null) {
- String port = arguments.getString("port");
- String packageName = arguments.getString("packageName");
- String activityName = arguments.getString("activityName");
- if (port != null) {
- AthrunInstrumentationTestRunner.port = port;
- }
- if (packageName != null) {
- AthrunInstrumentationTestRunner.packageName = packageName;
- }
- if (activityName != null) {
- AthrunInstrumentationTestRunner.activityName = activityName;
- }
- }
- super.onCreate(arguments);
- }
執行命令變成:
- adb -s SH14MTJ01900 shell am instrument -w -e port 1234 -e class org.athrun.remoterunner.TravelTest#test org.athrun.remoterunner/org.athrun.instrumentation.AthrunInstrumentationTestRunner
在Testcase中如何使用傳入的packageName和activityName:
- public class TravelTest extends ActivityInstrumentationTestCase2 {
- private static String getPkgName() {
- String packageName = AthrunInstrumentationTestRunner.getPackageName();
- return packageName;
- }
- private static String getActivityClassStr() throws ClassNotFoundException {
- String activityName = AthrunInstrumentationTestRunner.getActivityName();
- return activityName;
- }
- public TravelTest() throws Exception {
- super(getPkgName(), getActivityClassStr());
- }
-e 是屬于擴展參數,使用這種方法是可以不斷擴展的。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。