Frage

I am new to uiAutomator. I tried passing the parameters to method, it run but just ignores anything passed to it. Just wondering whether we can pass any arguments to test class or test method in uiAutomator?

War es hilfreich?

Lösung 3

Reference

You can send in parameters via command line:

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

You can access the value using the bundle that is available if you override the onCreatemethod of the InstrumentationTestRunner.

public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  value = (String) savedInstanceState.get("name");
}

Andere Tipps

I know this question is a bit old but I just started working on a UIAutomator project and figured I would share what I found.

The original question is a little ambiguous. It seems from the existing answers everyone assumed the question was about command line arguments/ startup arguments so I'm going to follow suit.

Neither of the answewrs worked well for what I was doing. The first can be used I think, but its a bit overkill to over ride onCreate for such a simple task. The second just didn't seem to work at all for me. I believe it is missing some information.

Get parameters is a call from the arguments Bundle. You need to get the from the instrumentation test. You can do that in AndroidTest by calling to InstrumentationRegistry

Bundle testBundle = InstrumentationRegistry.getArguments();

Then you can make the get string call from that bundle.

testBundle.getString(key);

I'm not sure how to do this exactly from eclipse or android studio, but from command line you can then specify running your test with the following ADB call.

adb shell am instrument -w -r   -e debug false -e class com.example.testApp.testappone.ExampleClass -e ParamKey 'your variable value' com.example.testApp.testappone.test/android.support.test.runner.AndroidJUnitRunner

Just to show a full example, if you make the adb call above you would then access the parameter with the following code.

@Test
public void FirstTest(){   
    Bundle testBundle = InstrumentationRegistry.getArguments();
    String paramValue = testBundle.getString("ParamKey");
}

I do want to note that you can make the adb call numerous ways so please don't get caught up in using instrument vs uiautomator. The important part is you specify the parameter with -e key value.

sources: Android Junit runner - used to run UI automator projects

and

Instrumentation documentation

You dont have to call onCreate to get the passed argument values. When you pass parameters to the testcase using

adb shell uiautomator runtest UiTest.jar -c package.name.ClassName -e stringKey stringValue

wherever in the testcase you wish to access stringValue, use:

String stringValue = getParams().getString("stringKey");

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top