How to access elements on external website using Espresso

前端 未结 2 1224
心在旅途
心在旅途 2021-02-03 11:42

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.

相关标签:
2条回答
  • 2021-02-03 12:24

    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:

    0 讨论(0)
  • 2021-02-03 12:27

    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)));
    }
    
    0 讨论(0)
提交回复
热议问题