HttpUnit/HtmlUnit equivalent for android

2019-01-11 20:02发布

I'm looking for a browser-simulating library on android, which handles things like

  • loading a website (http/https)
  • Redirections: HTTP (3xx Status Codes), JavaScript, HMTL tags
  • filling out html-forms
  • easy html parsing (could fall back to JSoup for that one)

HttpUnit or HtmlUnit would do just fine, but both of them are a pain to get running on android.

Is there any other option other than (Android)HttpClient (and therefore doing lots of the above on my own)? Or can I somehow get use of the android webkit/browser?

Thanks in advance!

1条回答
冷血范
2楼-- · 2019-01-11 20:49

I would recommend you to have a look at AndroidDriver for selenium. It seems to be a straightforward approach to easy test WebApplications with the Android Testing Framework.

You must use an Activity that includes a WebView in order to test HTTP/HTTPs websites. The Driver is instanciated with this Activity:

WebDriver driver = new AndroidWebDriver(getActivity());

Here is a sample test, quoted from the link above:

 public void testGoogleWorks()
    // Loads www.google.com
    driver.get("http://www.google.com");
    // Lookup the search box on the page by it's HTML name property
    WebElement searchBox = driver.findElement(By.name("q"));
    // Enter keys in the search box
    searchBox.sendKeys("Android Rocks!");
    // Hit enter
    searchBox.submit();
    // Ensure the title contains "Google"
    assertTrue(driver.getTitle().contains("Google"));
    // Ensure that there is at least one link with the keyword "Android"
    assertTrue(driver.findElements(By.partialLinkText("Android")).size() > 1);
}
查看更多
登录 后发表回答