How to…Appium: Testing Web Views with Appium

Welcome to a our addition to our How..To Series. The How..To series is designed to help you with you specific testing tool problems. Each month a new post will be published with a guide on a specific problem that you might need help with. On the second Tuesday of each month, Anuja will publish a post on a particular aspect of Appium. This post examines testing webviews with Appium. If you have suggestion of any future posts, please comment below and we will add them to the list.



There are two types of apps Native Apps and Hybrid apps. Native app means apps developed using native APIs and Hybrid apps means apps containing Web Views. As we know Appium supports Webviews and hence we can test Hybrid Applications using Appium very effectively. Web View is nothing but an Android/iOS application opening a web page inside their application.Before we start with our test case we need to know how to inspect HTML code of an Webview, How to Locate web elements and how to perform actions on web elements.

How to Inspect Webviews in Chrome Browser –

1. Go to Chrome browser

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

3. click on any url listed on web page.

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

How To Locate Webview Elements

Locating Webview elements is very easy as because it has same APIs like findElementById(). Hence we can use find elements by id,class name,xpath etc to locate our web elements before performing any kind of actions on it.

How to Switch To Webview

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();
        for (String contextName : contextNames) {
            if (contextName.contains("WEBVIEW")){

Sample Webview Test Case Example –

Consider you have an app which has web view for Registration. Where you need to perform below test case

1. Check web view displays text message “Welcome To My Page”

2. Enter Username

3. Enter Password

4. Click on Submit

5. Assert that text message saying  “Successfully Registered” is Displayed

In the below test case we need to set up desired capabilities for test case execution. Then inside you webview test case add the code to switch to Web View and then start with your test case code.  Before and After Test are JUnit Annotations used to define the flow of execution.

public class TestWebViews
    public void testCaseSetup()throws  Exception
        DesiredCapabilities cap=new DesiredCapabilities();
        cap.setCapability(MobileCapabilityType.DEVICE_NAME, "Android device");
        cap.setCapability(MobileCapabilityType.NEW_COMMAND_TIMEOUT, "4000");
        cap.setCapability(MobileCapabilityType.APP, "c://apk//sample.apk");
        driver=new AndroidDriver<MobileElement>("",cap);

public void testWebViews()throws Exception
        // Switch to WebView
        Set<String> contextNames = driver.getContextHandles();
        for (String contextName : contextNames) {
            if (contextName.contains("WEBVIEW")){
        // check WELCOME text is present
       Assert.assertEquals("Welcome To My Page", driver.findElement("welcome_text")).getText());
        //Check UI elements text boxes and buttons are present on the page


        //enter email id and password
        driver.findElementById("email_edit").sendKeys("[email protected]");

        // /click on submit button

        //Explicit wait for submission
        //Check for successful submission
        Assert.assertEquals("Successfully Registered",driver.findElementById("display_text"));

About The Author

anuja-bhatt Testing WebViews with Appium

Anuja works as QA Engineer. Anuja started her blog to share her experience regarding automation testing for Mobile Applications & Web Applications. Anuja has used various tools like Selenium,Appium, Selendroid, Espresso, Robitium and UI Automator for Android App Testing. As a tester she has spent a lot of time in the setting up of various tools and learning how to write different functional test cases. Even though it was simple to understand but finding a right tutorial among many confusing ones can make it difficult. So she decided to share her knowledge in very simple manner for the benefit of other testers.

About the Author


Find out more about @anujabhatt88