Selenium-IDE & JavaScript: Generating Unique New Account Names

Most of us have to create tests for registering new users at some point. In my last QA gig, I generated account names like this:

companyName+yyyymmdd-hhmmss

I was using Selenium-RC/Perl to do this, but the same thing can be accomplished via Selenium-IDE/HTML tests with JavaScript in an external user-extensions file. Here’s a demo:

1. Paste the JavaScript code below into a file; I named mine generating-unique-acct-name.js.

function generateUniqueAccountName(prefix) {
var now = new Date()
var month = zeroPad(now.getMonth()+1); //getMonth returns 0..11 so must add 1
var date = zeroPad(now.getDate());
var hours = zeroPad(now.getHours());
var minutes = zeroPad(now.getMinutes());
var seconds = zeroPad(now.getSeconds());
var milliseconds = now.getMilliseconds();
return(prefix +
"+" +
now.getFullYear() +
month +
date +
"-" +
hours +
minutes +
seconds +
milliseconds);
}


function zeroPad(number) {
if (number < 10)
{number = "0" + number}
return String(number)
}

2. Install this extension via Selenium-IDE’s Options=>Options=>Selenium Core Extensions field.

3. Select the Reload button adjacent to the Selenium Core Extensions field (a couple times as there’s a bug which sometimes prevents it from working after just one click!).

4. Create the following two-line Selenium-IDE demo test case:

storeEval generateUniqueAccountName(“SEL”) newAccountName
echo ${newAccountName}

5. Run the test repeatedly. You should see output in your Log pane that looks something like this:

[info] Executing: |storeEval | generateUniqueAccountName(“SEL”) | newAccountName |
[info] Executing: |echo | ${newAccountName} | |
[info] echo: SEL+20110415-233042980
[info] Executing: |storeEval | generateUniqueAccountName(“SEL”) | newAccountName |
[info] Executing: |echo | ${newAccountName} | |
[info] echo: SEL+20110415-23304488
[info] Executing: |storeEval | generateUniqueAccountName(“SEL”) | newAccountName |
[info] Executing: |echo | ${newAccountName} | |
[info] echo: SEL+20110415-233044909

Note that I’ve “improved” the uniqueness of the new account names being generated by adding milliseconds to the end of the hhmmss string. If you don’t want this improvement, simply eliminate the call to getMilliseconds and the string concatenation of the value of variable milliseconds.

Finally, note that I’ve “zero-padded” all one-digit values for month, date, hours, minutes, and seconds. I leave it as an exercise for you to do this for all values of milliseconds < 1000!

Here’s a video demo…

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

3 responses to “Selenium-IDE & JavaScript: Generating Unique New Account Names

  • Mary Ann May-Pumphrey

    Ouch! This code had a tiny bug which becomes really noticeable once October rolls around, which it just did a couple days ago! To fix it, I’ve changed this line at the end of the tiny zeroPad function…

    return(number)

    to this line….

    return String(number)

    Without this, the generateUniqueAccountName function was adding the integer value of month (10) to the year, and creating a seriously messed up datestamp.

    You’d think a QA engineer would test her code better, huh?

    –mam-p

  • A H Khan

    Hi mam-p
    Thanks for the help, i am new to selenium could you please give me an example of using your .js file in a script? i am trying to generate a random user ID but i have no idea on how to use .js file.I have tried to load the .js file but i have no clue on how run the two line demo code you mentioned in your script.
    Cheers,
    Khan

  • Mary Ann May-Pumphrey

    Hi Khan! I generated a 22-second video which I added at the end of this post. You can create that two-line Selenium-IDE test case by re-entering the two lines (or write back and I can include a link to the file). Hopefully, the video will at least make clear what to do with the .js vs. the .html.

    –mam-p

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: