170 likes | 175 Views
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.
E N D
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 publicclassSimpleHtmlUnitTestextendsjunit.framework.TestCase{ 2 3 publicvoidtestHomePage()throwsException{ 4 WebClientwebClient=newWebClient(); 5 java.net.URLurl=new 6 java.net.URL("http://htmlunit.sourceforge.net"); 7 8 HtmlPagepage=(HtmlPage)webClient.getPage(url); 9 10 assertEquals("Welcome to HtmlUnit",page.getTitleText()); 11 } 12 }
HTML Form Test 1 publicclassFormTestextendsjunit.framework.TestCase{ 2 3 publicvoidtestForm()throwsException{ 4 WebClientwebClient=newWebClient(); 5 java.net.URLurl= 6 newjava.net.URL("http://htmlunit.sourceforge.net"); 7 8 HtmlPagepage=(HtmlPage)webClient.getPage(url); 9 10 HtmlFormform=page.getFormByName("myform"); 11 12 HtmlTextInputtextInput= 13 (HtmlTextInput)form.getInputByName("myfield"); 14 15 textInput.setValueAttribute("zappa zoo yeah!"); 16 17 form.submit(); 18 } 19 }
Button Click Test 1 publicclassButtonClickTestextendsjunit.framework.TestCase{ 2 3 publicvoidtestButtonClick()throwsException{ 4 WebClientwebClient=newWebClient(); 5 java.net.URLurl= 6 newjava.net.URL("http://htmlunit.sourceforge.net"); 7 8 HtmlPagepage=(HtmlPage)webClient.getPage(url); 9 10 HtmlFormform=page.getFormByName("myform"); 11 12 HtmlButtonInputbuttonInput= 13 (HtmlButtonInput)form.getInputByName("mybutton"); 14 15 HtmlPagenewPage=(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 classAbstractDatabaseTestCase 2 extendsorg.dbunit.DatabaseTestCase{ 3 4 protectedIDatabaseConnectiongetConnection()throwsException{ 5 java.sql.Connectionconnection= java.sql.DriverManager. 6 getConnection("jdbc:oracle:thin:@localhost:1521:foo"); 7 returnnewDatabaseConnection(connection); 8 } 9 10 protectedDatabaseOperationgetSetUpOperation()throwsException{ 11 returnDatabaseOperation.CLEAN_INSERT; 12 } 13 14 protectedIDataSetgetDataSet()throwsException{ 15 returnnewFlatXmlDataSet(newjava.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 publicvoidtest()throwsException{ 2 3 // exercise the application 4 5 IDataSetexpectedDataSet= 6 newFlatXmlDataSet(newjava.io.File("expected.xml")); 7 8 ITableexpectedTable=expectedDataSet.getTable(“readings"); 9 10 ITabledatabaseTable=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