How can I get the app in espresso?

I am using espresso to add tests to an android app.

In mine TestActivity, I have some views that are visible only when the user subscribes. The flag userIsSignedInis stored in MyApp, which is a subclass android.app.Application. Is there a way to access the instance MyAppin a test case?

If not, what is an alternative way to do this?

thanks

+4
source share
3 answers

In your test class, you can use something like this:

@Rule
public ActivityTestRule<MainActivity> mActivityRule =
        new ActivityTestRule<>(MainActivity.class);

Then in your testing method:

@Test
public void doSomething() {

   //....
   mActivityRule.getActivity().getApplication();

}
+12
source

@Rule -

@Rule
public ActivityTestRule<MainActivity> mActivityRule = new ActivityTestRule<> MainActivity.class);
0
@RunWith(AndroidJUnit4.class)
public class Regression extends BaseTest {
    private static final String TAG = TestConstants.TAGPRETEXT + Regression.class.getSimpleName() + " ";

    private ActivityTestRule<MainActivity> activityTestRule;
    private TestName testName;

    @Rule
    public RuleChain chain = RuleChain
            .outerRule(activityTestRule = new ActivityTestRule<>(MainActivity.class))
            .around(testName = new TestName());

    @Override
    public <MainActivity extends AppCompatActivity> MainActivity getActivity() {
        return (MainActivity) activityTestRule.getActivity();
    }

    @Override
    public TestName getTestName() {
        return testName;
    }

    @Test
    public void addBasalProgram24Segments() {
        String programName = "testing" + System.currentTimeMillis() / 1000;

        try {
            new Main()
                    .doBasal()
                    .createBasalSegment(new float[]{1.15f, 14.55f, 0.05f, 12.05f, 19.05f, 21.5f, 20, 1.85f, 10.0f, 1, 2, 4, 15, 26.05f, 10, 30, 15, 4, 25, 15, 5, 12, 10, 20}, programName, new Random().nextInt(5), activityTestRule)
                    .verifyBasalProgramExist(programName);
        } catch (MyEspressoException e) {
            testFailRoutine(e);

        }
    }
}
-1

Source: https://habr.com/ru/post/1616036/


All Articles