通过继承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进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。