杀死所有活动,然后在Android上使用Espresso运行第二次测试

问题描述 投票:2回答:1

我正在使用黄瓜和浓缩咖啡。我有以下功能文件:

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类:

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

android automated-tests android-espresso
1个回答
0
投票

这是我正在使用的:

dependencies {
    implementation "androidx.legacy:legacy-support-v4:1.0.0"
    implementation "androidx.appcompat:appcompat:1.0.0"
}

android {
    defaultConfig {
        testInstrumentationRunnerArguments clearPackageData: 'true'
    }

    testOptions {
        animationsDisabled true
        execution 'ANDROIDX_TEST_ORCHESTRATOR'

        unitTests {
            includeAndroidResources true
        }
    }
}

所以请注意,我同时使用AndroidX来处理依赖项和Orchestrator。

由于您未使用AndroidX,因此需要使用execution 'ANDROID_TEST_ORCHESTRATOR'

testInstrumentationRunnerArguments clearPackageData: 'true'并不是严格必要的,如果您只是想终止活动,而我发现在需要时可以自己更有效地清除数据。

© www.soinside.com 2019 - 2024. All rights reserved.