Using Selenium-IDE to Generate the Data for a Data-Driven Test

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.

The original version of this testdata-generator program was written at my behest by two former students from my Selenium-IDE class–Lini Kuriyan & Archana Sajith.  Back then, there was no datadriven.js extension for Selenium-IDE , so their version of the program generated an extension file with the test data stored in a JavaScript array.  My update below generates the test data in XML format, ready for use with the datadriven.js extension. Here’s how to see this program in action:

  1. Install the File Logging and Flow Control extensions for Selenium-IDE from the Download page.
  2. Enable File Logging.
  3. Create an HTML test like this one within Selenium-IDE:
  4. open http://www.foothill.edu
    store //select[@name=’topnav’] selectLocator
    waitForElementPresent ${selectLocator}
    storeSelectOptions ${selectLocator} options
    runScript javascript{storedVars[‘options’].splice(0,2)}
    storeEval storedVars[‘options’].length totalOptions
    store 0 index
    store <testdata> xmlCode
    while (${index}<${totalOptions})
    storeEval storedVars[‘options’][storedVars[‘index’]] option
    selectAndWait ${selectLocator} label=${option}
    waitForPageToLoad 30000
    storeTitle pageTitle
    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
    goBackAndWait
    storeEval ${index}+1 index
    endWhile
    runScript javascript{storedVars[‘xmlCode’] += ‘</testdata>’;}
    echo ${xmlCode}

    This script uses storeSelectOptions to store all the option labels from the Foothills Quick Links drop-down into a variable named options. The first two (irrelevant) options are then removed from options via the runScript call to JavaScript’s splice method.

    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.

  5. Execute the script within Selenium-IDE.
  6. Use an editor on your logging file to remove everything prior to the opening <testdata> container.
  7. Convert all occurrences of an ampersand into its character entity &amp; to conform to XML requirements.
  8. 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.)
  9. Create a data-driven test case similar to that I showed in my January post, to utilize the generated test data.

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.

Advertisements

About Mary Ann May-Pumphrey

I'm a software QA automation engineer, focusing primarily on Selenium/Webdriver automation of the front end of web apps. View all posts by Mary Ann May-Pumphrey

2 responses to “Using Selenium-IDE to Generate the Data for a Data-Driven Test

  • qatester

    When I attempted to run this test, I receive an “Threw an exception: illegal character” error. I copied and pasted the above code into selenium. The test dies when it attempts this command:

    storedVars[‘xmlCode’] + ‘<test optionLabel=”‘

    what am I doing wrong?

  • Mary Ann May-Pumphrey

    Hi qatester! I am *very* sorry that I didn’t test this blog better before I published it. What happened is that WordPress changed some of the single quotes in the test case code (in the table above) to non-ASCII quotes. (Why it didn’t change all of them is the real mystery!)

    Anyway, I have added a P.S. to the blog, which provides a link to a .doc file. The contents of the .doc file should be copy/pasted into IDE’s Source tab (inside the tbody container) in order to create an executable test. And yes, I tested it this time!

    In order to see the problem, view this page with Firefox. Select just the code (in the table above) which caused IDE to die. Then select Firefox’s context menu’s =>View Selection Source. (The context menu is the one you get via a right-click if you’re using your mouse right-handed.) If you look very carefully at the new window containing just that one source line, you’ll see that the first pair of single quotes are correct, but the second pair are slanted backwards. That latter set are non-ASCII and won’t work. And they came from WordPress, not from the test case I ran before creating the post above.

    Again, I’m very sorry that you encountered this problem. I hope that you’ve at least learned something about how dangerous quotes can be!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: