Testing Web Applications with HtmlUnit and Dbunit - PowerPoint PPT Presentation

1 / 17
About This Presentation
Title:

Testing Web Applications with HtmlUnit and Dbunit

Description:

Testing Web Applications with HtmlUnit and Dbunit. Michael J. Bresnahan. Fruition Consulting Inc. ... 1 year of HtmlUnit and Dbunit experience. A Brief ... – PowerPoint PPT presentation

Number of Views:681
Avg rating:3.0/5.0
Slides: 18
Provided by: nerthu
Category:

less

Transcript and Presenter's Notes

Title: Testing Web Applications with HtmlUnit and Dbunit


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 APIs, 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 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.sourc
    eforge.net")
  • 7
  • 8 HtmlPage page (HtmlPage)
    webClient.getPage(url)
  • 9
  • 10 assertEquals("Welcome to HtmlUnit",
    page.getTitleText())
  • 11
  • 12

9
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.sou
    rceforge.net")
  • 7
  • 8 HtmlPage page (HtmlPage)webClient.getP
    age(url)
  • 9
  • 10 HtmlForm form page.getFormByName("myfo
    rm")
  • 11
  • 12 HtmlTextInput textInput
  • 13 (HtmlTextInput)form.getInputByName("
    myfield")
  • 14
  • 15 textInput.setValueAttribute("zappa zoo
    yeah!")
  • 16
  • 17 form.submit()
  • 18
  • 19

10
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("myf
    orm")
  • 11
  • 12 HtmlButtonInput buttonInput
  • 13 (HtmlButtonInput)
    form.getInputByName("mybutton")
  • 14
  • 15 HtmlPage newPage (HtmlPage)buttonInpu
    t.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 class AbstractDatabaseTestCase
  • 2 extends org.dbunit.DatabaseTestCase
  • 3
  • 4 protected IDatabaseConnection
    getConnection() throws Exception
  • 5 java.sql.Connection connection
    java.sql.DriverManager.
  • 6 getConnection("jdbcoraclethin_at_loc
    alhost1521foo")
  • 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

14
Dataset.xml
  • lt!DOCTYPE dataset SYSTEM "dataset.dtd"gt
  • ltdatasetgt
  • ltSTUDY_SESSIONS ID"1" AIMR_AREAS_ID"1"
    STUDY_SESSION_CODE"1/gt
  • ltSTUDY_SESSIONS ID"2" AIMR_AREAS_ID"1"
    STUDY_SESSION_CODE"2/gt
  • ltREADINGS ID"1" STUDY_SESSIONS_ID"1"
    READING_CODE"1"/gt
  • ltREADINGS ID"2" STUDY_SESSIONS_ID"2"
    READING_CODE"2"/gt
  • lt/datasetgt

15
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.get
    Table(readings")
  • 9
  • 10 ITable databaseTable 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
Write a Comment
User Comments (0)
About PowerShow.com