简体   繁体   English

从命令行启动android测试时的自定义参数

[英]custom parameter when launching android test from command line

I can launch my Android applications Junit/Robotium tests from the command line like this: 我可以从命令行启动我的Android应用程序Junit / Robotium测试,如下所示:

adb shell am instrument -w com.myapp.client.test/android.test.InstrumentationTestRunner

However, I want to somehow include a custom parameter that allows me specify whether the test is to be run in "Portrait" mode or "landscape" mode. 但是,我想以某种方式包含一个自定义参数,允许我指定测试是在“纵向”模式还是“横向”模式下运行。

How can I: 我怎样才能:

  1. specify that custom parameter in the command-line command? 在命令行命令中指定该自定义参数?

  2. How can I access that custom parameter's value in the Java code? 如何在Java代码中访问该自定义参数的值?

Thanks 谢谢

You can extend Android Instrumentation runner and override oncreate() method to get custom parameter from command line. 您可以扩展Android Instrumentation runner并覆盖 oncreate()方法以从命令行获取自定义参数。 Use your customized instrumentation runner while executing test cases. 在执行测试用例时使用您的自定义仪器运行器。

public class CustomInstrumentationRunner extends android.test.InstrumentationTestRunner{

@Override
public void onCreate(Bundle arguments) {
    //process you parameters here.     
super.onCreate(arguments);
}

@Override
public void onStart() {
    try {
       logger = CustomLogger.GetLogger();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    super.onStart();
}

You can specify a custom parameter using 您可以使用指定自定义参数

adb shell am insrument -e <NAME> <VALUE> <package/runner>

You can reach it's value using the bundle that is available if you override the onCreate method of the InstrumentationTestRunner . 如果覆盖InstrumentationTestRunneronCreate方法,则可以使用可用的包来达到它的值。

Eg: 例如:

public void onCreate(Bundle savedInstanceState) {

 super.onCreate(savedInstanceState);

 value = (String) savedInstanceState.get("name");

}

The way I did this is: 我这样做的方式是:

Before I run the test, I save a text file in the sdcard\\ 在我运行测试之前,我在sdcard中保存了一个文本文件

When the test starts, in the setUp, I parse each line of the text file and extract the key/value 测试开始时,在setUp中,我解析文本文件的每一行并提取键/值

arg1=valueX arg2=valueY arg1 = valueX arg2 = valueY

I made a hack solution to this. 我为此做了一个黑客解决方案。 It doesn't look like there is a clean solution here. 这看起来不像是一个干净的解决方案。

My hack is to utilize the "small", "medium", and "large" attributes that you can attach to tests. 我的黑客是利用你可以附加到测试中的“小”,“中”和“大”属性。

   @MediumTest
    public void testPortraitTest1() throws Exception{
        this.MetaDataTest(Solo.PORTRAIT);        
    }

    @LargeTest
    public void testLanscapeTest1() throws Exception{
        this.MetaDataTest(Solo.LANDSCAPE);        
    }

then you can use your batch file to call the medium tests first and then the large tests, like this: 然后你可以使用你的批处理文件先调用中等测试然后再调用大型测试,如下所示:

adb shell am instrument -w -e size medium com.me.client.test/android.test.InstrumentationTestRunner 
adb shell am instrument -w -e size large com.me.client.test/android.test.InstrumentationTestRunner

shame on google for not making this easier. 谷歌的耻辱,因为没有让这更容易。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM