您的位置:首页 > Web前端 > JavaScript

Presentation Layer Testing Framework

2012-02-14 18:00 316 查看
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.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息