1 / 17

Testing Web Applications with HtmlUnit and Dbunit

Learn about Java APIs HtmlUnit and Dbunit for automated functional testing of web and database applications, benefits, designing tests, and more.

sabrinav
Download Presentation

Testing Web Applications with HtmlUnit and Dbunit

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Testing Web Applications with HtmlUnit and Dbunit Michael J. Bresnahan Fruition Consulting Inc.

  2. Agenda • About Me, Mike Bresnahan • Brief Overview of HtmlUnit and Dbunit • Benefits of Automated Testing • About HtmlUnit • About Dbunit • Designing Tests • Questions and Answers

  3. 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

  4. 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.

  5. Benefits of Automated Testing • More efficient than manual testing. • Less error prone than manual testing. • Enables collective code ownership. • Enables refactoring. • Enables frequent integration.

  6. About HtmlUnit HtmlUnit Test Client Web Server Application Server

  7. HtmlUnit Features • HTTP 1.0 and 1.1 • HTTPS • HTML 4 • Cookies • Proxy Servers • JavaScript • Simulated Mouse Clicks • DOM Search and Navigation

  8. 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 }

  9. 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 }

  10. 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 }

  11. About Dbunit XML Database

  12. Dbunit Features • DTD database schemas • Import/export XML datasets • Manipulate datasets • Compare datasets • Ant task

  13. 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 }

  14. 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>

  15. 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 }

  16. 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

  17. Further Reading • http://htmlunit.sf.net • http://dbunit.sf.net • http://junit.org

More Related