I’ve been updating an ancient yet integral Perl form handler script (based on bnbform), and I will be introducing some fairly significant changes. Thus I’d like to make sure I don’t introduce any errors, or changes that would affect existing forms on our site.
The form handler allows arbitrary fields in a form, supported by hidden fields that define its behaviour (e.g. a field with the name ‘required’ contains the mandatory field names).
Basically I’d like to be able to verify the server response, including rejecting bad/missing data and success or ‘thank you’ screens. To do this, a combination of JBehave, WebDriver and jQuery could give us a solution.
Form manipulation (WebDriver + JQuery)
I’ll need to construct a mock form, populate it and submit it to the server. Then I’ll grab the response and check it. To make the tests a bit more dynamic, I’ll create a ‘clean slate’ html page with just the form element and a submit button:
This will create a div element containing a label and the input element.
Then, in our test case, its simply a matter of getting an instance of
With this setup, I can easily build the form to contain specific elements; specific form configurations that I want to test!
Server response (WebDriver + JUnit)
When we’re ready, WebDriver can submit the form and check the contents of the page returned:
Story time! (JBehave)
Implementing some BDD for the existing
functionality, and my impending changes, will help ensure that the form
continues to behave in the expected manner. So lets write some stories that
should cover some existing functionality!
Scenario: A basic 2-field form is submitted Given a text field with name 'Name' and value 'John Citizen' And a text field with name 'Submit By' and value 'john@localhost' When form is submitted Then the confirmation page is displayed Given a text field with name 'Name' and value 'John Citizen' And a text field with name 'Submit By' and value '' And 'Submit By' is required When form is submitted Then the error page is displayed
I’ve included the type and just enough details in the ‘
Given’ lines to
required’ will include the hidden input field that bnbform uses to track
When’ line obviously relates to WebDriver
submitting the form, and ‘
Then’ is the assertions relating to the response.
We’re simply expecting either a success page or validation error page. Any
other unexpected outcome (e.g. an error in the script) will result in a failed
StepsUsing the code above as a guide, this step class will cover the
After implementing the standard class structure outlined at http://jbehave.org/reference/stable, I have two additional classes: FormSteps and FormStories. FormStories, which extends JUnitStories, is the test case runner. The functionality of the form can be further defined by additional scenarios and stories, so existing functionality and the new enhancements can be verified.
NOTE: You may need to make sure your IDE copies the
*.story files to the
test class directory (maven should automatically copy them if they are under