Selenium IDE 扩展函数: 日期计算与表现

发表于:2013-9-30 10:37

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:zhouchen0603    来源:51Testing软件测试网采编

  用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();
}
  使用方法如下,详见以上代码
《2023软件测试行业现状调查报告》独家发布~

精彩评论

  • qq66061923
    2013-10-05 00:38:27

    代码挺复杂的,支持楼主的辛苦工作

  • qq66061923
    2013-10-05 00:38:27

    代码挺复杂的,支持楼主的辛苦工作

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号