如何 运行 Android UI 测试并将参数传递给 App/Activity

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

我想 运行 UI 测试 Android 应用程序的一些预定义数据,这些数据会因测试用例而异。在 iOS 中,可以像这样将参数传递给被测试的应用程序:

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

这些参数以后可以在应用程序进程中使用。

Android UI 测试是否有类似的东西?最好的方法是通过 intent 的附加功能访问这些参数。

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

感谢帮助!

尝试使用浓缩咖啡进行 Android UI 测试。 https://developer.android.com/training/testing/espresso

好吧,事实证明用 Intents 模拟启动参数非常简单。在 Android UI 测试中,当使用 ActivityTestRule 时,可以使用特定的 Intent.

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

Intent i = new Intent();
i.putExtra("specificArgument", argumentValue);
activityRule.launchActivity(i);

这是 Kotlin 语言。我把意图指令放在测试的@Before方法中。

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

@Before
fun setupTestConfiguration() {
    val intent = Intent()
    intent.putExtra("mocked", true)
    mActivityTestRule.launchActivity(intent)
}