It's one thing to have a web application in production that requires active monitoring (you are monitoring it right?), but it's another issue completely when that web application contains a "contact us" form. All good teams will use various tools to gather emails so they can manage their subscriber lists appropriately, and that's the rub; what happens when code changes in the app that impacts the form?
Nothing – why? Because you will be blissfully unaware your form is failing unless you test it.
Testing a web form is what we are going to demonstrate using
Python and the Mechanize library. Mechanize allows you to load a page, inspect any forms on the page and then manipulate the form just as a user would.
Let's see some code!
#!/usr/bin/env python import sys import mechanize br = mechanize.Browser() r = br.open("http://example.com") if r.code != 200: print("Unable to load the test page") sys.exit(2) # if your devs are nice and name the form... # br.select_form("name") # if they don't name it, then you have to use... # br.form = list(br.forms()) # To discover what the control field names are... # for control in br.form.controls: # print(control.type, control.name) br.form = list(br.forms()) nameField = br.form.find_control("name") emailField = br.form.find_control("email") nameField.value = "Testing Form" emailField.value = "email@example.com" r = br.submit() if r.code != 200: print("Error submitting form") sys.exit(3)
That's it! With about a dozen lines of code we can now feel comfortable that any new work that the dev team does as they walk their path is being actively tested.
For any team walking the Operations Path, more code would need to be added to this in order to make it production ready. The type of each input control could be checked and you could also add a range of values to test on submission.
This small example can also be extended to test other aspects of your web application: link health, user agent response, device response. Each of these, and others, will be getting further attention in upcoming articles.