Issue
I'm using Cucumber and Espresso. I have the following feature file:
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
in the step defining Then User is navigated to the profile screen
I had to add a pressBack
otherwise the starting of the MainActivity
for the second test was not working (it was timing out) and I could see on the emulator that the ProfileActivity
was still shown.
This is the 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();
}
}
I have found this that might be useful but I find surprising that I need to do it by myself: it seems to me such a basic feature.
Is there a better way to kill the ProfileActivity
after the first test?
Solution
This is what I'm using:
dependencies {
implementation "an androidx library:1.0.0"
...
}
android {
defaultConfig {
testInstrumentationRunnerArguments clearPackageData: 'true'
}
testOptions {
animationsDisabled true
execution 'ANDROIDX_TEST_ORCHESTRATOR'
unitTests {
includeAndroidResources true
}
}
}
So please notice that I'm using AndroidX for both the dependencies and the Orchestrator.
Since you are not using AndroidX you need to use execution 'ANDROID_TEST_ORCHESTRATOR'
.
The testInstrumentationRunnerArguments clearPackageData: 'true'
is not strictly necessary if you just want to kill the activities.
Answered By - kingston
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.