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?

snakile
  • 52,936
  • 62
  • 169
  • 241

5 Answers5

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> 
highlycaffeinated
  • 19,729
  • 9
  • 60
  • 91
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>
arman1991
  • 1,166
  • 1
  • 18
  • 28
Xalfur
  • 21
  • 1
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>
priorww1
  • 193
  • 7
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

galeop
  • 1,685
  • 14
  • 13
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>
miteshB666
  • 161
  • 1
  • 3