Task is to
compare two dates in format DD/MM/YYYY, e.g. 16/04/2010 and 17/04/2010
and say which is earlier, using Selenium IDE.
We’ll use
several javascript snippets. First
of all we must remember that for javascript all dates are in format MM/DD/YYYY.
So we have to switch places for DD and MM:
Result for
above will be 04/16/2010.
Same actions
for second date:
Now we’ve
got two dates:
04/16/2010
04/17/2010
In above example
parsing date means converting it into number of milliseconds which passed from
01/01/1970 until that date. We end up with two numbers:
dateMilliA =
"1271368800000"
dateMilliB =
"1271455200000"
Syntax for
comparison:
store | javascript{(condition)?returnThisWhileItsTrue:returnThisWhileItsNotTrue}
| variableHoldingResult
Whole
script:
And version
to copy:
//additional variables
store | 0 | counter0
store | 1 | counter1
store | 2 | counter2
//store 1st date to compare
store | 16/04/2010 | dateA
//switch places for DD and MM
store | javascript{storedVars['dateA'].split('/')[storedVars['counter0']]} | dateDAYA
store | javascript{storedVars['dateA'].split('/')[storedVars['counter1']]} | dateMONTHA
store | javascript{storedVars['dateA'].split('/')[storedVars['counter2']]} | dateYEARA
store | javascript{storedVars['dateMONTHA'] + "/" + storedVars['dateDAYA'] + "/" + storedVars['dateYEARA']} | dateToParseA
//store 2nd date to compare
store | 17/04/2010 | dateB
//and switch places for 2nd date
store | javascript{storedVars['dateB'].split('/')[storedVars['counter0']]} | dateDAYB
store | javascript{storedVars['dateB'].split('/')[storedVars['counter1']]} | dateMONTHB
store | javascript{storedVars['dateB'].split('/')[storedVars['counter2']]} | dateYEARB
store | javascript{storedVars['dateMONTHB'] + "/" + storedVars['dateDAYB'] + "/" + storedVars['dateYEARB']} | dateToParseB
//transform dates into milliseconds
store | javascript{Date.parse(storedVars['dateToParseA']);} | dateMilliA
store | javascript{Date.parse(storedVars['dateToParseB']);} | dateMilliB
//compare parsed dates
store | javascript{(storedVars['dateMilliA'] < storedVars['dateMilliB'])?storedVars['dateA']:storedVars['dateB']} | result
//show result
runScript | javascript{alert("Earlier date is " + storedVars['result'] + ".")}
|
store | 0 | counter0
store | 1 | counter1
store | 2 | counter2
//store 1st date to compare
store | 16/04/2010 | dateA
//switch places for DD and MM
store | javascript{storedVars['dateA'].split('/')[storedVars['counter0']]} | dateDAYA
store | javascript{storedVars['dateA'].split('/')[storedVars['counter1']]} | dateMONTHA
store | javascript{storedVars['dateA'].split('/')[storedVars['counter2']]} | dateYEARA
store | javascript{storedVars['dateMONTHA'] + "/" + storedVars['dateDAYA'] + "/" + storedVars['dateYEARA']} | dateToParseA
//store 2nd date to compare
store | 17/04/2010 | dateB
//and switch places for 2nd date
store | javascript{storedVars['dateB'].split('/')[storedVars['counter0']]} | dateDAYB
store | javascript{storedVars['dateB'].split('/')[storedVars['counter1']]} | dateMONTHB
store | javascript{storedVars['dateB'].split('/')[storedVars['counter2']]} | dateYEARB
store | javascript{storedVars['dateMONTHB'] + "/" + storedVars['dateDAYB'] + "/" + storedVars['dateYEARB']} | dateToParseB
//transform dates into milliseconds
store | javascript{Date.parse(storedVars['dateToParseA']);} | dateMilliA
store | javascript{Date.parse(storedVars['dateToParseB']);} | dateMilliB
//compare parsed dates
store | javascript{(storedVars['dateMilliA'] < storedVars['dateMilliB'])?storedVars['dateA']:storedVars['dateB']} | result
//show result
runScript | javascript{alert("Earlier date is " + storedVars['result'] + ".")}
|