Download Testing Web Applications with HtmlUnit and Dbunit

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Testing Web Applications
with HtmlUnit and Dbunit
Michael J. Bresnahan
Fruition Consulting Inc.
Agenda
•
•
•
•
•
•
•
About Me, Mike Bresnahan
Brief Overview of HtmlUnit and Dbunit
Benefits of Automated Testing
About HtmlUnit
About Dbunit
Designing Tests
Questions and Answers
About Me, Mike Bresnahan
• 9 years of software development experience
–
–
–
–
–
–
Enterprise applications
C, C++, Java
Relational Databases
Client/Server
Web
1 year of HtmlUnit and Dbunit experience
A Brief Overview of
HtmlUnit and Dbunit
• Both are Java APIs for automated functional
testing.
– HtmlUnit is a Java API for automated testing of web
applications.
– Dbunit is a JUnit extension for automated testing of
database applications.
• Both are Java API’s, but can be used to test
applications written in any language.
• Both are Open Source.
Benefits of Automated
Testing
• More efficient than manual testing.
• Less error prone than manual testing.
• Enables collective code ownership.
• Enables refactoring.
• Enables frequent integration.
About HtmlUnit
HtmlUnit
Test Client
Web
Server
Application
Server
HtmlUnit Features
•
•
•
•
•
•
•
•
HTTP 1.0 and 1.1
HTTPS
HTML 4
Cookies
Proxy Servers
JavaScript
Simulated Mouse Clicks
DOM Search and Navigation
Simple HtmlUnit Test
1 public class SimpleHtmlUnitTest extends junit.framework.TestCase {
2
3
public void testHomePage() throws Exception {
4
WebClient webClient = new WebClient();
5
java.net.URL url = new
6
java.net.URL("http://htmlunit.sourceforge.net");
7
8
HtmlPage page = (HtmlPage) webClient.getPage(url);
9
10
assertEquals("Welcome to HtmlUnit", page.getTitleText());
11
}
12 }
HTML Form Test
1 public class FormTest extends junit.framework.TestCase {
2
3
public void testForm() throws Exception {
4
WebClient webClient = new WebClient();
5
java.net.URL url =
6
new java.net.URL("http://htmlunit.sourceforge.net");
7
8
HtmlPage page = (HtmlPage)webClient.getPage(url);
9
10
HtmlForm form = page.getFormByName("myform");
11
12
HtmlTextInput textInput =
13
(HtmlTextInput)form.getInputByName("myfield");
14
15
textInput.setValueAttribute("zappa zoo yeah!");
16
17
form.submit();
18
}
19 }
Button Click Test
1 public class ButtonClickTest extends junit.framework.TestCase {
2
3
public void testButtonClick() throws Exception {
4
WebClient webClient = new WebClient();
5
java.net.URL url =
6
new java.net.URL("http://htmlunit.sourceforge.net");
7
8
HtmlPage page = (HtmlPage) webClient.getPage(url);
9
10
HtmlForm form = page.getFormByName("myform");
11
12
HtmlButtonInput buttonInput =
13
(HtmlButtonInput) form.getInputByName("mybutton");
14
15
HtmlPage newPage = (HtmlPage)buttonInput.click();
16
}
17 }
About Dbunit
XML
Database
Dbunit Features
•
•
•
•
•
DTD database schemas
Import/export XML datasets
Manipulate datasets
Compare datasets
Ant task
Abstract Database Test
1 public abstract class AbstractDatabaseTestCase
2
extends org.dbunit.DatabaseTestCase {
3
4
protected IDatabaseConnection getConnection() throws Exception {
5
java.sql.Connection connection = java.sql.DriverManager.
6
getConnection("jdbc:oracle:thin:@localhost:1521:foo");
7
return new DatabaseConnection(connection);
8
}
9
10
protected DatabaseOperation getSetUpOperation() throws Exception{
11
return DatabaseOperation.CLEAN_INSERT;
12
}
13
14
protected IDataSet getDataSet() throws Exception {
15
return new FlatXmlDataSet(new java.io.File("dataset.xml"));
16
}
17 }
Dataset.xml
<!DOCTYPE dataset SYSTEM "dataset.dtd">
<dataset>
<STUDY_SESSIONS ID="1" AIMR_AREAS_ID="1" STUDY_SESSION_CODE="1“/>
<STUDY_SESSIONS ID="2" AIMR_AREAS_ID="1" STUDY_SESSION_CODE="2“/>
<READINGS ID="1" STUDY_SESSIONS_ID="1" READING_CODE="1"/>
<READINGS ID="2" STUDY_SESSIONS_ID="2" READING_CODE="2"/>
</dataset>
Simple Dbunit Test
1 public void test() throws Exception {
2
3
// exercise the application
4
5
IDataSet expectedDataSet =
6
new FlatXmlDataSet(new java.io.File("expected.xml"));
7
8
ITable expectedTable = expectedDataSet.getTable(“readings");
9
10
ITable databaseTable = getConnection().
11
createQueryTable(“readings","select * from readings");
12
13
Assertion.assertEquals(expectedTable, databaseTable);
14 }
Designing Tests
•
•
•
•
•
•
Keep tests independent
Keep tests small and single purpose
Pool database connections
Load common reference data once
Engineer the test suite
Decouple tests from the application as
much as possible
• Use Dbunit to hold the expected data of
web pages
Further Reading
• http://htmlunit.sf.net
• http://dbunit.sf.net
• http://junit.org
Related documents