In the first post about functional testing, we went through the basics of what functional testing is and how it complements and differs from other types of testing used for software quality assurance. This time, I will spend some time talking about using functional testing in a practical sense to set up a testing suite for a web based software project.
As with any other tool, selection of a platform for functional testing is largely a matter of personal choice, and I do not particularly have a preference or an endorsement for this. What I will steer you toward is a good site for trying to figure out how to properly think about building PHP projects, a site called PHP The Right Way (www.phptherightway.com), which I find myself referring to whenever I need ideas about good tools to use, or ways to think about a concept. They suggest looking at 3 tools for functional testing:
- Selenium: A browser controller
- Mink: a wrapper of sorts that standardizes how you interact with testing API’s
- Codeception: a full stack testing framework, which includes functional testing in its offering.
Each of these can provide a powerful means of performing automated functional testing and you should select your tool of choice based on what works best for you. I typically find myself using Selenium for functional testing, but again, I would encourage you to look at each available option out there and select the best fit. That said, today’s example will use Selenium to show you how functional testing can be done via automated browser control.
Selenium is a Firefox browser plugin. In the event you do not have Firefox installed, you will need to install it and then install the Selenium IDE Plugin.
Once you have installed the plugin, you can open the Selenium IDE by selecting the appropriate option on the Firefox ‘Tools’ menu.
Once you have Selenium up and running, you can begin by running a few simple functional tests. First, we need to set the URL of the site that we will be testing. That is done by filling out the appropriate field in the Base URL text box near the top of the IDE. Now we are ready to start testing.
For our first test, let’s check and make sure that we get directed to the right page when we click a given link. For this example, we will click the Publications link and then run a test to verify that the Publications page loads up. The easiest way to get this done (and really any command) is to record it. To do this, click the red circle icon near the upper right part of the Selenium IDE. From there, we can switch back to the Firefox window and click the Publications link from the main navigation area. Once you have done this, switch back to the Selenium IDE and click the record button again to stop recording. You should see 2 new items in the IDE that represent what was recorded:
Now we need to add an assertion to make sure that things worked out as they were supposed to when we clicked the link. To do this, click immediately below the ‘clickAndWait’ line and use the drop down box below to set the command to ‘assertTextPresent’. This command allows us to search the page and determine if there is a particular word or phrase present. On the Publications page, the phrase ‘Software Contribution’ is present in the title of one of the articles, so that is what we will search for. To do this, set the target field to ‘Software Contribution’.
Once this is all done, we are now ready to run the test. You can simply click the ‘Play the current test case’ button (which is the play button with one darkened bar) on the Selenium IDE. Selenium will beginning running each step of your test by controlling the Firefox browser and executing each command in sequence. If you see all green, the test was successful!
The first test was extremely simple, so let’s take a look at something a little more useful. Filling out forms, whether for logging into an administration area, performing a search, or registering for an account are all very common tasks in a web application. Fortunately, Selenium can help with that too. In this example, we will test Google, and see if it is working properly. The test will be composed of filling in the search text box, checking the auto suggestion’s functionality, then submitting the search and checking to make sure we get results. Again, the record button is our friend for executing the actions. This time we need to be wary of the fact that there is Ajax involved in the auto suggestion, so we need to slow Selenium down a bit by moving the slider toward slow. Then we need to add a ‘assertTextPresent’ command prior to the click event to look for an auto suggestion. Then we will add another assertion to look for text that should appear on the results page. After running the test, you should have an IDE that looks like the screenshot below:
Wrapping it up
These two examples are extremely simple and really represent the tip of the iceberg with regards to what can be done with functional testing. You have almost total control over the testing tool and you can test things in a variety of ways to make sure your users are getting the experience they are supposed to. Additionally, you can save the test suites so that they can be used again in the future, like after a patch or upgrade to the system. It will help you identify any unintended consequences early and often.
No matter which tool you use for functional testing, adding a regimen like this to your automated quality assurance process will help you make sure that you have a simple, repeatable means of making certain that your software is producing the desired result from the user’s point of view. I highly recommend considering the use of functional testing in any application that you develop, it will save you time in the long run.
Best of Luck