Wednesday, May 09, 2007

The Selenium storEval Method in Action

I was creating Selenium tests for the module on a J2EE app at work and noted that the requirements wanted the beginning and end dates in the search text boxes to be the current date. I then thought, "Hmmm, these tests will be run on various dates. How can I get the current date to validate that the current date, in a specified format, is entered into the beginning and end date textbox entries for a search query?"

A little digging on the OpenQA site in the reference for Selenium and I found the storEval method. According to the reference this method, "Gets the result of evaluating the specified JavaScript snippet. The snippet may have multiple lines, but only the result of the last line will be returned." That was just what I needed.

Here is where the method is used in the test that is stored in an HTML file:

<tr>
<td>storeEval</td>
<td>var d = new Date(); var mday = d.getDate();
var mmonth = d.getMonth() + 1; var myear; if (navigator.appName == 'Microsoft
Internet Explorer') {myear = d.getYear();} else {myear = d.getYear() + 1900;}
if (mday.toString().length == 1) { mday = '0' + mday; } if
(mmonth.toString().length == 1) { mmonth = '0' + mmonth; } todaysDate = mmonth
+ '/' + mday + '/' + myear;</td>
<td>todaysDate</td>
</tr>
<tr>

Note that the todaysDate variable above is passed the formatted current date. I then use it later in the test to assert that this is what is entered into the beginning and end date text boxes.

<tr>
<td>verifyValue</td>
<td>searchBeginDate</td>
<td>${todaysDate}</td>
</tr>
<tr>
<td>verifyValue</td>
<td>searchEndDate</td>
<td>${todaysDate}</td>
</tr>