Selenium-IDE & JavaScript: Data-Driven Testing

In January, I made a post on the Sauce Labs blog re: how to do data-driven testing with Selenium-IDE, using XML input. But data-driven testing with Selenium-IDE can also be accomplished via storing the test data in JavaScript objects in a user-extensions file. Here’s how to create the same test case as in my January post – checking the links in the Sauce Labs home page’s footer – only with JavaScript test data this time.

  1. Download goto_sel_ide.js from the flowControl page on the OpenQA Wiki.
  2. Create the datafile containing the footer links to be clicked and the expected titles of the respective landing pages. I named my datafile saucelabs-footer.js:
  3. var testdata = [
    {linkText:"How It Works",title:"Sauce OnDemand: Cross browser testing with hosted Selenium testing in the cloud - Sauce Labs"},
    {linkText:"Downloads",title:"Sauce Labs Downloads"},
    {linkText:"Pricing",title:"Sauce Labs Pricing For Hosted Selenium in the Cloud"},
    {linkText:"Support",title:"Support: Sauce RC (Selenium RC) - Sauce Labs"},
    {linkText:"Forums",title:"Sauce Labs Forums"},
    {linkText:"Blog",title:"Selenium Testing? Do Cross Browser Testing with Sauce Labs"},
    {linkText:"Flash/Flex Solution",title:"Automate testing of your Flex and Flash Web Apps - Sauce Labs"},
    {linkText:"Documentation",title:"Documentation - Sauce Labs"},
    {linkText:"About",title:"About - Sauce Labs"},
    {linkText:"Team",title:"The Sauce Labs Team"},
    {linkText:"News",title:"Selenium News & Events - Sauce Labs"},
    {linkText:"Webinars",title:"Webinars - Sauce Labs"},
    {linkText:"Contact us",title:"Support Contact - Sauce Labs"},
    {linkText:"Service Status",title:"Status - Sauce Labs"},
    ];

    linkText and title are field names of my choosing. You can use any names you want, and also any number of fields you want; I only needed two for this simple test case.

    Note that I have used an array of records, but other schemes are possible also.

  4. Install goto_sel_ide.js and your .js file via Selenium-IDE’s Options=>Options=>Selenium Core extensions field.
  5. Utilize your .js file via creating and executing an HTML test case similar to this one:
    setSpeed 500
    open http://www.saucelabs.com
    storeEval testdata.length; tests
    store 0 index
    while (${index} < ${tests})
    storeEval testdata[storedVars[‘index’]].linkText linkText
    storeEval testdata[storedVars[‘index’]].title title
    echo DEBUG: $index = ${index}, $linkText = ${linkText}, $title = ${title}
    clickAndWait link=${linkText}
    verifyTitle ${title}
    goBackAndWait
    storeEval parseInt(storedVars[‘index’])+1 index
    endWhile

    The storeEval command gets the number of elements in the JavaScript testdata array via the length property. That number is then used to control the number of iterations of the while loop.

    The first two storeEval commands inside the loop are the trickiest/ugliest part of the test case. The outer square brackets need to hold the value of the test case’s index variable. But within JavaScript, such variables are stored in the storedVars associative array. So storedVars[‘index’] must be used to represent the numeric index for the element of interest within the testdata array.

    The last point of interest is the final storeEval command. Its first argument requires parseInt to ensure that arithmetic addition is performed as a result of the plus-sign operator rather than string concatenation. (JavaScript uses the plus-sign for both operations.)

Why learn how to do data-driven testing within Selenium-IDE using JavaScript when one can use XML data instead? The biggest advantage I can see of the approach above is that it requires only ONE outside extension – goto_sel_ide.js. The XML method requires THREE. Beyond that, JavaScript is the primary tool for extending the power of Selenium-IDE, so it’s good to be as familiar as possible regarding how to utilize it in various ways.

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

7 responses to “Selenium-IDE & JavaScript: Data-Driven Testing

  • vik-gupta

    Hi mam I am getting this error message when loading selenium ide:

    “Failed to load user-extensions.js!
    files=C:\Login_data.js
    lineNumber=1
    error=SyntaxError: illegal character”

    The login_data.js file is listed bellow:
    “var testdata=[
    {emailid:”vikas@gmail.com”,password:”master”},
    {emailid:”vikas1@gmail.com”,password:”master1″},
    {emailid:”vikas12@gmail.com”,password:”master11″},
    {emailid:”ram@gmail.com”,password:”sikander”},
    ];

  • Mary Ann May-Pumphrey

    Vik-gupta: Please send me your Login_data.js file as an attachment to my email address. It’s problematic looking at copy/pastes. The only thing I notice is that your data file appears to start with a double quote instead of var. That’s probably something you added to make things more readable but if it’s really in your .js file, that’s indeed the problem.

    –mam-p

  • Barry Horgan

    Hi Mary,

    Excellent write up. I have followed your example above, created testdata.js file and added it as user extension. However, I get the following error when I try ‘storeEval testdata.lenght; num ‘ -> [error] Threw an exception: testdata is not defined

    Am I missing a step?

    Thanks,

    Barry.

  • vish

    Nice Blog Mary, Can you please let me know how to set xml file path with load command?

    • Mary Ann May-Pumphrey

      Hi Vish! Not sure why you’re asking about how to set the xml file path here because this particular post is about using a .js file instead of an .xml file for data-driven testing with Se-IDE. I actually think using a .js file is better because it’s not dependent on the two very dated extensions–include.js and datadriven.js–that are needed for using an xml file as input.

      That said, here’s an example of a working loadTestData command which I tried just now. Obviously, you’ll need to tweak it a bit if you’re working on a Windows system. If you have any problems, please write me directly and I’ll see if I can help.

      loadTestData file:///Users/mamp/Desktop/BLOG/20130626/testdata.xml
  • Peter Kehl

    Hi Mary Ann,

    You may be interested in SeLite: https://code.google.com/p/selite/wiki/ProjectHome. It helps the scripts to use SQLite. It adds to convenience of Selenium IDE by enhancing Selenese syntax. It allows fine-grain configuration of custom test properties via https://code.google.com/p/selite/wiki/Settings, which supports team work. https://code.google.com/p/selite/wiki/AutoCheck collects errors and warnings generated by webserver or the server side framework.

    The tests can have an isolated (exported) copy of application database. SeLite can also serve for non-QA automation, where the Selenese scripts consume or generate the data stored in SQLite.

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: