In January, I made a post on the Sauce Labs blog about how to use Selenium-IDE to do data-driven testing. In that post, I raised the issue of what one might do if faced with building a test for a lengthy list of links. The data-driven test case would be quite similar to the one I included in my post, but producing the test data for the XML file would be a big pain.
One solution is to use Selenium-IDE to generate the data. Below is an example that generates the XML needed for data-driven testing of the options in the Foothills College Quick Links drop-down.
- Install the File Logging and Flow Control extensions for Selenium-IDE from the Download page.
- Enable File Logging.
- Create an HTML test like this one within Selenium-IDE:
- Execute the script within Selenium-IDE.
- Use an editor on your logging file to remove everything prior to the opening
- Convert all occurrences of an ampersand into its character entity
&to conform to XML requirements.
- View your former log file, now converted into an XML file, in the browser to ensure that no errors are present. (This step is only necessary if you haven’t used an XML editor.)
- Create a data-driven test case similar to that I showed in my January post, to utilize the generated test data.
|storeEval||storedVars[‘xmlCode’] + ‘<test optionLabel=”‘||xmlCode|
|storeEval||storedVars[‘xmlCode’] + storedVars[‘option’]||xmlCode|
|storeEval||storedVars[‘xmlCode’] + ‘” title=”‘||xmlCode|
|storeEval||storedVars[‘xmlCode’] + storedVars[‘pageTitle’]||xmlCode|
|storeEval||storedVars[‘xmlCode’] + ‘”/>’||xmlCode|
totalOptions and index are the two variables used to control the while loop that makes up the bulk of the script.
The loop begins by retrieving one of the stored options values, and then using it with selectAndWait to mimic a user selecting that drop-down option. Once the target page has loaded, storeTitle is used to store the target page’s title in pageTitle.
At this point in the code, five rather ugly calls to storeEval take care of actually creating a single line of test data in the eventual XML file, via concatenation onto xmlCode.
When using a Selenium app like this one to generate test data, it is critical to remember to review the generated data, to make sure no bugs are revealed! For example, a line like this one in the generated data…
<test optionLabel="Classified Senate" title="Campus Clubs"/>
…clearly indicates a bug with the option for Classified Senate, and should be reported via the defect-tracking system. The XML file would also need to be modified to show the correct value for the title attribute in the line above.
P. S. If you want to run my code above, copy/paste the contents of this .doc file into Selenium-IDE’s Source tab, inside the tbody container.