Dynamic dates in Selenium IDE

来源:互联网 发布:c语言黑客代码大全 编辑:程序博客网 时间:2024/06/06 10:41

http://dubbelfnutt.wordpress.com/2011/02/18/dynamic-dates-in-selenium-ide/

If you have a test where you need to input a date somewhere, chances are that you want a date sometime in the future. You don’t want to update your test everytime so you want the date to be dynamic. This can be achieved using javascript. In the following example, I use three selenese commands to do this. You can use just one command, but I want to make it simple to understand.

1) Stores a variable with the number of months into the future that I want my date to be. In this case 4 months. If I put this in a separate variable it will be much easier to change at a later time, rather than searching and replacing in the javascript below.

Command: store
Target: 4
Value: monthsToAdd

2) Formats and stores the date we want to use. Format in this example is Swedish, like so: “2011-02-18“.

Command: store
Target: javascript{var dates = new Date();var day = dates.getDate();if (day < 10){day = ‘0’ + day;} month = dates.getMonth() + 1 + parseInt(storedVars['monthsToAdd']);var year = dates.getFullYear();if (month > 12){month = month – 12;year = year + 1;}if (month < 10){month = ‘0’ + month;}year + ‘-‘ + month + ‘-‘ + day}
Value: dynamicDate

3) Types our dynamic date into a textbox, whos id contains “dateInputBox”.

Command: type
Target: //input[contains(@id, 'dateInputBox')]
Value: ${dynamicDate}

You can read more about javascripts and Selenium here. If you feel that I am wrong or you have something to add, please comment.


0 0
原创粉丝点击