Selenium IDE 扩展函数: 日期计算与表现
用
Selenium IDE做网页的
功能测试经常会碰到与日期有关的验证,本人结合实际
工作中遇到的应用,写了一个扩展,代码如下:
Selenium.prototype.doStoreDateByFormat = function(format,args){
// format of args: month,day,year
if(args.trim().length < 3)
throw new SeleniumError("arguments must contain ""Month"",""Date"",""Year"" variables!");
var formats = format.split(",");
var days = parseInt(formats[3]);
var sysDate = getSysDate(); //get the sysdate
var specDate = dateAfterDays(sysDate,parseFloat(days),1); //get specified date
var arrArgs = args.split(",");
var month = specDate.getMonth()+1;
var date = specDate.getDate();
var year = specDate.getFullYear();
// get Month string
switch(formats[0].toUpperCase()){
case "MM": // return 2 digits of month number, such as: 01
month = (month+"").length==1?"0"+month:month;
break;
case "MMM": //return the first 3 chars of the month word, such as: Jan
month = this.getMonthShortName(month);
break;
case "MMMM": //return the full word of the month word, such as: January
month = this.getMonthFullName(month);
break;
case "M":
default:
// return 1 digit when month is lower than 10.
// do nothing
}
//get Date string
switch(formats[1].toUpperCase()){
case "DD": //always return 2 digits of the month number, such as: 05
date = (date+"").length==1?"0"+date:date;
break;
case "D":
default:
// return 1 digit when Date is lower than 10.
// do nothing
}
//get Year string
switch(formats[2].toUpperCase()){
case "YY": // return last 2 digits of the year number, such as: 08 (2008)
year = (year+"").substr(2);
break;
case "YYYY":
default:
//return full year number, such: 2008.
}
storedVars[arrArgs[0]] = month;
storedVars[arrArgs[1]] = date;
storedVars[arrArgs[2]] = year;
}
Selenium.prototype.getMonthFullName = function(month){
var monthArr = new Array("January","February","March","April","May","June","July","August","September","October","November","December");
if(month == null){
throw new SeleniumError("you didn't specify a Month");
}
try{
month = parseInt(month);
}catch (e){
throw new SeleniumError("""Month"" is not a Integer!");
}
return monthArr[month-1];
}
/* return the date N days(N*24 hours) before/after some day.
* args : num - positive/negative integer/float number,default is "1";
* type - 0 (second) or 1 (day), default is second.
* return : Date
*/
function dateAfterDays(date, num, type){
date = (date == null?new Date():date);
num = (num == null?1:num);
if(typeof(num)!="number")
throw new SeleniumError("dateAfterDays(date, num, type),""num"" argument must be Number type.");
if(typeof(date)!="object")
throw new SeleniumError("dateAfterDays(date, num, type),""date"" argument must be Date type.");
var iType = (type == null?0:type);
var arr = [1000,86400000];
var dd = date.valueOf();
dd += num * arr[iType];
var d=new Date(dd);
return d;
}
function getSysDate(){
return new Date();
}
|
使用方法如下,详见以上代码
01