Selenium IDE: How do I get today's date?

前端 未结 5 699
情歌与酒
情歌与酒 2021-02-05 22:16

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 th

相关标签:
5条回答
  • 2021-02-05 22:54

    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>
    
    0 讨论(0)
  • 2021-02-05 23:08

    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> 
    
    0 讨论(0)
  • 2021-02-05 23:12

    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>
    
    0 讨论(0)
  • 2021-02-05 23:12

    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:

    0 讨论(0)
  • 2021-02-05 23:17

    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>
    
    0 讨论(0)
提交回复
热议问题