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.
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.
相关文章推荐
- Yersinia(A Framework for layer 2 attack)
- Google C++ Testing Framework Primer
- 关于WPF 中 “System.Windows.Markup.XamlParseException”类型的未经处理的异常在 PresentationFramework.dll 中发生 异常的处理。
- Automation Testing Framework by c++, Practice-1: tips from POSA4
- Google C++ Testing Framework 简介
- The Ethical Hack: A Framework for Business Value Penetration Testing
- A Model-Based Framework for Security Policy Specification, Deployment and Testing
- fix LayerKit framework不能提交App Store
- A First Look at the Android Testing Framework
- PTF 安装及简单测试 Packet Testing Framework
- [MacrayKV] An innovative parallel testing framework
- Unit Testing on Visual Studio 2013 with NUnit framework
- websecurity - Web Security Testing Framework 超级牛B扫描器
- Android application testing with the Android test framework
- Websecurify – Web Security Testing Framework
- 【安全牛学习笔记】Penetration testing presentation
- Locust - A modern load testing framework
- MVP for Android: how to organize the presentation layer
- Presentation Modeling Framework 介绍
- Google C++ Testing Framework AdvancedGuide