首页 > 解决方案 > 如何运行 Android UI 测试并将参数传递给 App/Activity

问题描述

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

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

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

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

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

感谢帮助!

标签: androidtestingandroid-espresso

解决方案


好吧,事实证明用Intents. 在 Android UI 测试中,使用时ActivityTestRule可以使用特定的Intent.

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

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

推荐阅读