12

Using espresso, we click a Login button which launches an external website (Chrome Custom Tab) where you can login and then it redirects back to our android application.

Is there a way in Espresso to:
1) Verify the correct URL is being launched
2) Access the elements on the website so that I can enter the login information and continue to login

enter image description here

When I try viewing it in the Espresso Launch Navigator, nothing shows up for the page, and if I try to record, it doesn't pick up on me entering anything on the page.

This is what I have so far (it is in Kotlin (not Java)): enter image description here

And here is the error that gets displayed: enter image description here

It launches my application, select the login button, opens the website but then it isn't able to access the elements.

I also tried:

enter image description here

Update: This is using Chrome Custom Tabs (not a Web View) so Espresso Web is not working.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
reutsey
  • 1,743
  • 1
  • 17
  • 36

2 Answers2

11

I was able to resolve this issue using both Espresso and UI Automator. You are able to combine the two. The selection of the login button I used Espresso (and the rest of the app, I will use Espresso). To handle the Chrome Custom tab for logging in, I used UIAutomator:

enter image description here

reutsey
  • 1,743
  • 1
  • 17
  • 36
  • Thank you so, so much. I never would have figured out having to press the same button twice like that. How did you figure it out, and why do you think it's necessary? – Noah Andrews Aug 12 '17 at 23:17
  • Are you referring to the sign in button? It is actually clicking a sign in header first and then the sign in button but i had to do that because i needed it to dismiss the keyboard that comes up. The keyboard was covering the sign in button so i clicked an element elsewhere on the screen first to get it to go away and then i could access the sign in button. Hope that helps! – reutsey Aug 14 '17 at 00:06
  • Thanks! You made me realize that I was having the same issue, but it worked because my selector wasn't specific enough and was clicking something different than I thought it was. Now I can use a different, more obvious selector. – Noah Andrews Aug 20 '17 at 01:21
  • 2
    how are you clicking on the button in the webpage using UIautomator ? I'm trying to click on the button in webpage but I'm not able to do this. – Umar Hussain Oct 02 '17 at 07:47
2

Update:

You can't use Espresso for testing Chrome Custom Tabs. Espresso works when testing your own app.

For testing the Chrome tabs you could use UI Automator but you probably don't want to do that.

1) Verify the correct URL is being launched

A unit test would be enough here. You just need to make sure the URL passed to the Chrome custom tabs library is the correct one. You are making sure YOUR code works fine. What happens next is handled by the library and the tests belong there.

2) Access the elements on the website so that I can enter the login information and continue to login

Here you are testing a simple web page. Why would you like the additional overhead of starting an emulator? Maybe Selenium or whatever is cool for web would work here (not a web dev)?

You can use Espresso Web

Here is an example test:

@Test
public void typeTextInInput_clickButton_SubmitsForm() {
    // Lazily launch the Activity with a custom start Intent per test.
    mActivityRule.launchActivity(withWebFormIntent());

    // Selects the WebView in your layout. If you have multiple WebView objects,
    // you can also use a matcher to select a given WebView,
    // onWebView(withId(R.id.web_view)).
    onWebView()
        // Find the input element by ID.
        .withElement(findElement(Locator.ID, "text_input"))
        // Clear previous input.
        .perform(clearElement())
        // Enter text into the input element.
        .perform(DriverAtoms.webKeys(MACCHIATO))
        // Find the submit button.
        .withElement(findElement(Locator.ID, "submitBtn"))
        // Simulate a click using JavaScript.
        .perform(webClick())
        // Find the response element by ID.
        .withElement(findElement(Locator.ID, "response"))
        // Verify that the response page contains the entered text.
        .check(webMatches(getText(), containsString(MACCHIATO)));
}
LordRaydenMK
  • 13,074
  • 5
  • 50
  • 56
  • What is the withWebFormIntent()? Also, I have the app launched already using the LaunchActivity then I select the Login button and that is when I want it to use a web view. – reutsey Jul 28 '17 at 20:55
  • If i try to launch it with JS enabled (in the override for afterActivityLaunched) it gives me: android.support.test.espresso.NoMatchingViewException: No views in hierarchy found matching: WebView with JS enabled – reutsey Jul 28 '17 at 21:05
  • Updated question with more information on what I currently have – reutsey Jul 28 '17 at 21:29
  • @rfodge check this for your second comment: https://stackoverflow.com/questions/39558202/onwebview-no-views-in-hierarchy-found-matching-webview-with-js-enabled – LordRaydenMK Jul 29 '17 at 11:16
  • I tried that solution (unless something is incorrect still with my setup). See examples of what i tried in the question. I have two screenshots with using a matcher. – reutsey Jul 29 '17 at 12:11
  • I updated my question, but the web view does not work because I found out we are using a chrome custom tab not a web view for it. – reutsey Jul 31 '17 at 20:14
  • Why do you say we wouldn't want to use UI Automator? Is there a particular con to trying that? I start up the emulator already for testing. I launch our application in an emulator, select our login button, and then I need to login (via the chrome custom tab), and then we get redirected back to our application to login. – reutsey Aug 02 '17 at 12:33
  • @reutsey, did you find the solution for this issue? I'm also facing the same issue. – Keshav Kumar Nov 21 '19 at 06:50
  • @LordRaydenMK - What exactly this withWebFormIntent()? How to get this method? – Keshav Kumar Nov 21 '19 at 06:51
  • @KeshavKumar you can find that in https://github.com/android/testing-samples/blob/38155c782fca4730e9553a5ae6e98d9f04897999/ui/espresso/WebBasicSample/app/src/androidTest/java/com/example/android/testing/espresso/web/BasicSample/WebViewActivityTest.java#L130 – Chetan Jan 20 '21 at 22:58