11

I'm testing my web application using Selenium IDE. There are test cases in which I have to assert that today's date appears on the page. I cannot hard code today's date in the test because today's date changes every day. How do I get the current day, month & year in Selenium IDE?

5 Answers 5

17

Not sure what format your date is in, but you could do something like this:

<tr> 
        <td>storeEval</td> 
        <td>var d=new Date(); d.getDate()+'-'+((d.getMonth()+1)) 
+'-'+d.getFullYear();</td> 
        <td>date2</td> 
</tr> 
<tr> 
        <td>echo</td> 
        <td>${date2}</td> 
        <td></td> 
</tr> 
2

The line <td>var d=new Date(); d.getDate()+'-'+((d.getMonth()+1))+'-'+d.getFullYear();</td> works but returns the month as single-digit.

For my test case, I need the date returned in the format YYYY-MM-DD, so I use

<td>var d= new Date(); var m=((d.getMonth()+1)<10)?'0'+(d.getMonth()+1):(d.getMonth()+1); d.getFullYear()+"-"+m+"-"+d.getDate();</td>
1

Double-digits for M and D (i.e. MM/DD/YYYY format):

<td>var t= new Date(); var d=((t.getDate())&lt;10)?'0'+(t.getDate()):(t.getDate()); var m=((t.getMonth()+1)&lt;10)?'0'+(t.getMonth()+1):(t.getMonth()+1); m+&quot;/&quot;+d+&quot;/&quot;+t.getFullYear();</td>
1

I am a bit confused about the answers here. From my side, what I wanted to do is to include the current date in an input text-area.

Here is how I did it: use the date as the value of an input text

0

Try this in Selenium IDE. It will print the timestamp as yyyymmddhhmmss.

<tr>
    <td>storeExpression</td>
    <td>javascript{var date = new Date();date.getFullYear() + '' + ((date.getMonth()+1&lt;10)?('0'+(date.getMonth()+1)):(date.getMonth()+1)) + '' + ((date.getDate()+1&lt;10)?('0'+(date.getDate()+1)):(date.getDate()+1)) + '' + date.getHours() + '' + date.getMinutes() + '' + date.getSeconds();}</td>
    <td>date</td>
</tr>
<tr>
    <td>echo</td>
    <td>${date}</td>
    <td></td>
</tr>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.