Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run Android UI Tests and pass arguments to the App/Activity

I want to run UI tests for Android app with some predefined data which will vary depending on the test case. In iOS, it's possible to pass arguments to the tested app like this:

app = XCUIApplication()
app.reset()
app.launchArguments += ["--myArgument"]
app.launch()

These arguments are later available inside the app process.

Is there something similar for Android UI testing? Best way would to access these arguments via intent's extras.

getIntent().getExtras().getString(key)

Thanks for help!

like image 944
Kacper Dziubek Avatar asked Oct 20 '25 04:10

Kacper Dziubek


2 Answers

Well, it turned out it's quite simple to simulate launch arguments with Intents. In Android UI tests when using ActivityTestRule it's possible to start an activity with a specific Intent.

@Rule
public ActivityTestRule<MainActivity> activityRule
        = new ActivityTestRule<>(MainActivity.class, false, false);

Intent i = new Intent();
i.putExtra("specificArgument", argumentValue);
activityRule.launchActivity(i);
like image 180
Kacper Dziubek Avatar answered Oct 22 '25 18:10

Kacper Dziubek


Here is in Kotlin. I put the intent instructions in the @Before method of the test.

@Rule
@JvmField
var mActivityTestRule = ActivityTestRule(MainActivity::class.java, false, false)

@Before
fun setupTestConfiguration() {
    val intent = Intent()
    intent.putExtra("mocked", true)
    mActivityTestRule.launchActivity(intent)
}
like image 29
Stefano Zanella Avatar answered Oct 22 '25 18:10

Stefano Zanella