Kacper Dziubek
Kacper Dziubek

Reputation: 1693

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!

Upvotes: 4

Views: 2093

Answers (4)

Roger Alien
Roger Alien

Reputation: 3060

With new ActivityScenario you can launch Activity with intent the following way:

    val intent = Intent(InstrumentationRegistry.getInstrumentation().targetContext, YourActivity::class.java).apply {
        putExtra("EXTRA_KEY, extraValue)
    }

    val scenario = launchActivity<YourActivity>(intent)

Upvotes: 0

Stefano Zanella
Stefano Zanella

Reputation: 113

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)
}

Upvotes: 2

Kacper Dziubek
Kacper Dziubek

Reputation: 1693

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);

Upvotes: 4

billo
billo

Reputation: 157

Try using espresso for Android UI testing. https://developer.android.com/training/testing/espresso

Upvotes: -2

Related Questions