Monday, October 22, 2012

Test WebView used within android native app

Unfortunately currently robotium does not support testing of webview even if it is used with native app but there is a way to test webview using Android WebDriver. So the solution is using combination of robotium + android webdriver. Here is the sample code to test contents of webview.
Little background about AUT: Native app has a button (assume ‘Google’) clicking on which opens webview with google.com.

Sample Code:
Activity mActivity = getActivity();
WebView web;
ArrayList<View> views = solo.getCurrentViews();
for(View view : views) {
if(view instanceof WebView) {
web = (WebView) view;
break;
}
}
WebDriver driver = new AndroidWebDriver(mActivity);
driver.get(web.getOriginalUrl());
WebElement element = driver.findElement(By.name("q"));
element.sendKeys("Test");
element = driver.findElement(By.xpath("//span[text()='Google Search']"));
element.click();

4 comments:

  1. Did this code worked?
    driver.get(web.getOriginalUrl()); throwing Null pointer exception.

    ReplyDelete
    Replies
    1. This is working for me. Please make sure you have only one webview by checking the count of webview.

      Delete
  2. I can't seem to import AndroidWebDriver.

    I download this https://code.google.com/p/selenium/downloads/detail?name=selenium-java-2.32.0.zip&can=2&q= & added to my Build Path.

    Where can I get the AndroidWebDriver?

    Thanks.

    ReplyDelete
  3. Nice it seems to be good post... It will get readers engagement on the article since readers engagement plays an vital role in every blog.. i am expecting more updated posts from your hands.
    Mobile App Development Company
    Android App Development Company

    ReplyDelete