(原)Selenium IDE 扩展函数,日期计算与表现。(Selenium IDE extension function: deal with date.)...

I use Selenium IDE for performing web functional testing and often work with date, so I created a extension function to deal with date calculate and presentation, the code is listed below:
用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();
}

the image below shows how to use this function, for details please see the previous code:
使用方法如下,详见以上代码:


转载于:https://www.cnblogs.com/nuelaleo/archive/2008/04/10/1147493.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值