Presentation Layer Testing Framework

HtmlUnit

Selenium


HtmlUnit

The general pattern with HtmlUnit is to get a page, find the element, click on it, and check the resulting page contents.

HtmlPage is HtmlUnit's model of an HTML page returned from a server. Once you have a page, you access its contents in one of three ways:

Call methods reflecting specific HTML concepts like forms, anchors, and frames.

Call methods that address HTML elements by references using names and ids.

Call methods using XPath, a web standard for addressing XML document nodes.


//Navigate to a page and enter Lisa and click Search button

 @Test
    public void testInternetNavigation() throws Exception{
        WebClient webClient = new WebClient();
        webClient.setThrowExceptionOnScriptError(false);
        HtmlPage mainPage = webClient.getPage("http://www.google.com");
        HtmlPage mainpage1 = webClient.getPage(new URL("http://www.google.com"));
        HtmlForm form = mainPage.getFormByName("f");
        HtmlTextInput queryText = form.getInputByName("q");
        queryText.setValueAttribute("Lisa");
        HtmlSubmitInput searchButton = form.getInputByName("btnG");
        HtmlPage resultPage = searchButton.click();
        System.out.println("result is: " + resultPage);
    }


Selenium

// first start the server then run the test

private Selenium selenium;
    @Before
    public void setUp() throws InterruptedException {
        selenium = new DefaultSelenium("localhost", 4444, "*googlechrome", "https://www.google.com/");
//        Thread.sleep(5000);
        selenium.start();
    }

@Test
    public void testGoogleSearch(){
        selenium.open("http://www.baidu.com/");
        selenium.type("wd", "Avril");
        selenium.click("su");
        selenium.waitForPageToLoad("60000");
    }



HtmlUnit vs. Selenium

Similarities:

Both are free and open source and both require Java 5 as the minimum platform requirement.

Major difference between the two is that HtmlUnit emulates a specific web browser while Selenium drives a real web browser process. When using Selenium, the browser itself provides support for JavaScript. In HtmlUnit 2.5, Mozilla's Rhino 1.7 Release 2 engine provides JavaScript support and specific browser behavior is emulated.

The HtmlUnit pros are that it is a 100% Java solution, it is easy to integrate in a build process, and Cactus can integrate HtmlUnit code for in-container testing as can other frameworks. HtmlUnit provides an HTML object model, which can validate web pages to the finest level of detail. HtmlUnit also supports XPath to collect data; Selenium XPath support is limited to referencing elements.

The Selenium pros are that the API is simpler and drives native browsers, which guarantees that the behavior of the tests is as close as possible to a user installation.

Finally:

Use HtmlUnit when...

Use HtmlUnit when your application is independent of operating system features and browser specific implementations of JavaScript, DOM, CSS, etc, not account for by HtmlUnit.

Use Selenium when...

Use Selenium when you require validation of specific browsers and operating systems, especially if the application takes advantage of or depends on a browser's specific implementation of JavaScript, DOM, CSS, etc.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值