在 运行 在 Android 上使用 Espresso 进行第二次测试之前终止所有活动
Killing all the activities before running second test with Espresso on Android
我正在使用 Cucumber 和 Espresso。我有以下功能文件:
Feature: From Main to Profile
Background:
Given User is registered
And User is logged in
@ios @android Scenario: User can navigate from the home screen to the profile screen
Given User is on the home screen
When User taps Profile
Then User is navigated to the profile screen
@ios @android Scenario: User can navigate from the profile screen back to the home screen
Given User is on the Profile screen
When User taps back
Then User is navigated back to the home screen
在定义 Then User is navigated to the profile screen
的步骤中我必须添加一个 pressBack
否则第二个测试的 MainActivity
的开始不起作用(超时)我可以在模拟器上看到 ProfileActivity
仍然显示。
这是steps
class:
public class MainActivitySteps extends BaseActivitySteps {
public static final int PROFILE_BUTTON_ID = R.id.tvProfile;
@Rule
public ActivityTestRule<MainActivity> activityTestRule = new ActivityTestRule<>(MainActivity.class);
@Rule
public GrantPermissionRule permissionRule = GrantPermissionRule.grant(android.Manifest.permission.ACCESS_FINE_LOCATION);
@Before
public void setup() {
activityTestRule.launchActivity(new Intent());
activity = activityTestRule.getActivity();
Intents.init();
}
@After
public void tearDown() {
activityTestRule.finishActivity();
Intents.release();
}
@Given("^User is on the home screen")
public void userIsAtMainScreen() {
assertTrue(activity.findViewById(R.id.btnRecordTrip).getVisibility() == View.VISIBLE);
}
@When("^User taps Profile")
public void userTapsProfile() {
// wait for view to become visible
userTaps(PROFILE_BUTTON_ID);
}
@Given("^User is on the Profile screen")
public void userIsAtProfileScreen() {
userTaps(PROFILE_BUTTON_ID);
}
@Then("^User is navigated to the profile screen")
public void userIsNavigatedToTheProfileScreen() {
intended(hasComponent(ProfileActivity.class.getName()));
pressBack();
}
@When("^User taps back")
public void userTapsBack() {
pressBack();
}
@Then("^User is navigated back to the home screen$")
public void userIsNavigatedBackToTheHomeScreen() {
userIsAtMainScreen();
}
}
我发现 this 这可能很有用,但令我惊讶的是我需要自己做:在我看来,这是一个基本的功能。
有没有更好的方法在第一次测试后杀死ProfileActivity
?
这是我正在使用的:
dependencies {
implementation "an androidx library:1.0.0"
...
}
android {
defaultConfig {
testInstrumentationRunnerArguments clearPackageData: 'true'
}
testOptions {
animationsDisabled true
execution 'ANDROIDX_TEST_ORCHESTRATOR'
unitTests {
includeAndroidResources true
}
}
}
所以请注意,我正在为依赖项和 Orchestrator 使用 AndroidX。
由于您没有使用 AndroidX,因此您需要使用 execution 'ANDROID_TEST_ORCHESTRATOR'
。
如果您只是想终止活动,testInstrumentationRunnerArguments clearPackageData: 'true'
并不是绝对必要的。
我正在使用 Cucumber 和 Espresso。我有以下功能文件:
Feature: From Main to Profile
Background:
Given User is registered
And User is logged in
@ios @android Scenario: User can navigate from the home screen to the profile screen
Given User is on the home screen
When User taps Profile
Then User is navigated to the profile screen
@ios @android Scenario: User can navigate from the profile screen back to the home screen
Given User is on the Profile screen
When User taps back
Then User is navigated back to the home screen
在定义 Then User is navigated to the profile screen
的步骤中我必须添加一个 pressBack
否则第二个测试的 MainActivity
的开始不起作用(超时)我可以在模拟器上看到 ProfileActivity
仍然显示。
这是steps
class:
public class MainActivitySteps extends BaseActivitySteps {
public static final int PROFILE_BUTTON_ID = R.id.tvProfile;
@Rule
public ActivityTestRule<MainActivity> activityTestRule = new ActivityTestRule<>(MainActivity.class);
@Rule
public GrantPermissionRule permissionRule = GrantPermissionRule.grant(android.Manifest.permission.ACCESS_FINE_LOCATION);
@Before
public void setup() {
activityTestRule.launchActivity(new Intent());
activity = activityTestRule.getActivity();
Intents.init();
}
@After
public void tearDown() {
activityTestRule.finishActivity();
Intents.release();
}
@Given("^User is on the home screen")
public void userIsAtMainScreen() {
assertTrue(activity.findViewById(R.id.btnRecordTrip).getVisibility() == View.VISIBLE);
}
@When("^User taps Profile")
public void userTapsProfile() {
// wait for view to become visible
userTaps(PROFILE_BUTTON_ID);
}
@Given("^User is on the Profile screen")
public void userIsAtProfileScreen() {
userTaps(PROFILE_BUTTON_ID);
}
@Then("^User is navigated to the profile screen")
public void userIsNavigatedToTheProfileScreen() {
intended(hasComponent(ProfileActivity.class.getName()));
pressBack();
}
@When("^User taps back")
public void userTapsBack() {
pressBack();
}
@Then("^User is navigated back to the home screen$")
public void userIsNavigatedBackToTheHomeScreen() {
userIsAtMainScreen();
}
}
我发现 this 这可能很有用,但令我惊讶的是我需要自己做:在我看来,这是一个基本的功能。
有没有更好的方法在第一次测试后杀死ProfileActivity
?
这是我正在使用的:
dependencies {
implementation "an androidx library:1.0.0"
...
}
android {
defaultConfig {
testInstrumentationRunnerArguments clearPackageData: 'true'
}
testOptions {
animationsDisabled true
execution 'ANDROIDX_TEST_ORCHESTRATOR'
unitTests {
includeAndroidResources true
}
}
}
所以请注意,我正在为依赖项和 Orchestrator 使用 AndroidX。
由于您没有使用 AndroidX,因此您需要使用 execution 'ANDROID_TEST_ORCHESTRATOR'
。
如果您只是想终止活动,testInstrumentationRunnerArguments clearPackageData: 'true'
并不是绝对必要的。