Dubbelfnutt's blog

Life's too short to remove USB safely.

Dynamic dates in Selenium IDE

If you have a test where you need to input a date somewhere, chances are that you want a date sometime in the future. You don’t want to update your test everytime so you want the date to be dynamic. This can be achieved using javascript. In the following example, I use three selenese commands to do this. You can use just one command, but I want to make it simple to understand.

1) Stores a variable with the number of months into the future that I want my date to be. In this case 4 months. If I put this in a separate variable it will be much easier to change at a later time, rather than searching and replacing in the javascript below.

Command: store
Target: 4
Value: monthsToAdd

2) Formats and stores the date we want to use. Format in this example is Swedish, like so: “2011-02-18“.

Command: store
Target: javascript{var dates = new Date();var day = dates.getDate();if (day < 10){day = ‘0’ + day;} month = dates.getMonth() + 1 + parseInt(storedVars[‘monthsToAdd’]);var year = dates.getFullYear();if (month > 12){month = month – 12;year = year + 1;}if (month < 10){month = ‘0’ + month;}year + ‘-‘ + month + ‘-‘ + day}
Value: dynamicDate

3) Types our dynamic date into a textbox, whos id contains “dateInputBox”.

Command: type
Target: //input[contains(@id, ‘dateInputBox’)]
Value: ${dynamicDate}

You can read more about javascripts and Selenium here. If you feel that I am wrong or you have something to add, please comment.

February 18, 2011 - Posted by | Selenium | , ,

4 Comments »

  1. [error] Unexpected Exception: fileName -> chrome://selenium-ide/content/selenium-core/scripts/selenium-api.js, lineNumber -> 2531

    Comment by Anonymous | August 6, 2012 | Reply

  2. [error] Unexpected Exception: fileName -> chrome://selenium-ide/content/selenium-core/scripts/selenium-api.js, lineNumber -> 2535, columnNumber -> 0

    Comment by Anonymous | July 4, 2013 | Reply

  3. This Is not working in Fire fox. Getting below mentioned error:
    [error] Unexpected Exception: SyntaxError: illegal character. fileName -> chrome://selenium-ide/content/selenium-core/scripts/selenium-api.js, lineNumber -> 2547, columnNumber -> 0

    Comment by Anonymous | April 2, 2014 | Reply

  4. This code is working for me in Firefox (2nd command) :
    javascript{ var dates = new Date(); var day = dates.getDate(); if (day 12) { month = (month – 12); year = (year + 1) } if (month < 10) { month = '0' + month } day + '/' + month + '/' + year }

    Comment by WΔNØ | May 2, 2014 | Reply


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 )

Connecting to %s

%d bloggers like this: