vendredi 7 octobre 2016

Use Espresso to check contents of a web page loaded in the browser

I currently have a test which simulates clicking on a button in a dialog box which then loads a Google Forms document from my Google Drive:

@Test
public void testTakeSurveyNow() throws UiObjectNotFoundException {
    onView(withText(R.string.survey2))
            .check(matches(isDisplayed()));
    onView(withText(R.string.now))
            .check(matches(isDisplayed()))
            .perform(click());
    Uri surveyUri = Uri.parse(context.getString(R.string.survey2_uri));
    intended(allOf(hasAction(Intent.ACTION_VIEW), hasData(surveyUri)));
    startApp();
    Assert.assertTrue(prefs.contains(SharedPreferenceKeys.SURVEY2_DATE));
    onView(withText(startsWith("BBCT")))
            .check(matches(isDisplayed()));
    onView(withText(R.string.survey1))
            .check(doesNotExist());
}

The problem I encountered is that survey2_uri was set to an incorrect address. I noticed this while running the test and fixed it. However, I would like to fix my test to help me avoid this kind of error in the future. How do I assert that the correct document is shown, possibly looking for some expected text which is most likely unique to the document? Originally, I assumed that the document will be loaded in the device's browser. This leads me to believe I can use Espresso Web. The document suggests something like

onWebView()
      .withElement(findElement(Locator.ID, "teacher"))
      .check(webMatches(getText(), containsString("Socrates")));

I'm just not sure what to use for the parameters of findElement() nor how to determine what values to use here.

To make matters worse, I have recently started to believe that my assumption is false that the document will open in the browser. It is possible that the document will open in another app, such as Google Drive. This possibility seriously complicates this test. Any suggestions?

Aucun commentaire:

Enregistrer un commentaire