Tuesday 16 February 2016

Testing Web Views with Appium



Now a days many apps are hybrid. Hybrid apps means app containing Web Views.  We have seen in previous post that how to perform different kinds of interaction with native android app using Appium but now it time see the how to perform web interaction on Web Views. We just have to notify the appium that we will be testing Web Views from the specific point in the test case and appium WebDriver handles it for us.Let us how to do with a simple example.

How to Inspect Web Views in Chrome Browser -

1. Go to chrome browser
2. Connect the Device with USB Debugging ON

Enable USB Debugging
Enable USB Debugging

3. Type this url -chrome://inspect/devices#devices

Chrome Inspector
Chrome inspector

4. click on any url listed on web page.

Chrome inspector console
Chrome inspector console

5. Then click on any UI element and you will get associated html source code which you can use to write your own xpath.

App under testing -


Suppose you have a app with a Web page Displaying a Login screen which displays page Title "WELCOME". The app also displays two text-boxes first to enter email id and second to enter password and there are two button "Cancel" and "Submit". After click on Submit the page shows "Successfully Logged In".

How to Switch To Web Views - 


Check out the code sample given below which switched to web view and then test cases perform actions mentioned in your code on directly on the Web Views.

  // Switch to WebView
        Set<String> contextNames = driver.getContextHandles();
        System.out.println(contextNames.size());
        for (String contextName : contextNames) {
            System.out.println(contextName);
            if (contextName.contains("WEBVIEW")){
                driver.context(contextName);
            }


Test Scenario-


1. First test that title "WELCOME" is displayed.

2.Test that two text-boxes are displayed and two buttons are also displayed.

3. Enter email is and password.

4. click on Submit

5. Test Success Message is displayed.

Appium Test Case-

@Test
public void testWebViews()throws Exception
    {
        // Switch to WebView
        Set<String> contextNames = driver.getContextHandles();
        System.out.println(contextNames.size());
        for (String contextName : contextNames) {
            System.out.println(contextName);
            if (contextName.contains("WEBVIEW")){
                driver.context(contextName);
            }
        }
        // check WELCOME text is present
       Assert.assertEquals("WELCOME", driver.findElement(By.id("welcome_text")).getText());
        //Check UI elements text boxes and buttons are present on the page
        Assert.assertTrue(driver.findElementById("email_edit").isDisplayed());
        Assert.assertTrue(driver.findElementById("password_edit").isDisplayed());
        Assert.assertTrue(driver.findElementById("cancel_button").isDisplayed());
        Assert.assertTrue(driver.findElementById("submit_button").isDisplayed());

        //enter email id and password
        driver.findElementById("email_edit").sendKeys("anujabhatt88@gmail.com");
        driver.findElementById("password_edit").sendKeys("12345");

        // /click on submit button
        driver.findElementById("submit_button").click();

        //Explicit wait for submission
        Thread.sleep(3000);
        //Check for successful submission
        Assert.assertEquals("Successfully Logged In",driver.findElementById("display_text"));
    }

Now you can start testing web views in Appium :-)
If you find this post helpful then pleas share your feedback in comments section below. Do follow me on social media for latest post updates. Thanks :-)

3 comments:

  1. Nice Article.

    Could you please explain how can we identify web element in hybrid app when we switch to web view.

    Thanks!!

    ReplyDelete
    Replies
    1. Hi,

      Thanks for visiting my blog. You can use chrome inspector to create xpath to find web elements . check out http://qaautomated.blogspot.in/2016/02/how-to-locate-android-app-ui-elements.html

      Delete
  2. This comment has been removed by a blog administrator.

    ReplyDelete