【selenium-ide】js扩展时间函数
1 Selenium.prototype.doStoreDateByFormat = function(format,args){ 2 // format of args: month,day,year 3 if(args.trim().length < 3) 4 throw new SeleniumError("arguments must contain Month,Date,Year variables!"); 5 6 var formats = format.split(","); 7 var days = parseInt(formats[3]); 8 9 var sysDate = getSysDate(); //get the sysdate 10 var specDate = dateAfterDays(sysDate,parseFloat(days),1); //get specified date 11 var arrArgs = args.split(","); 12 13 var month = specDate.getMonth()+1; 14 var date = specDate.getDate(); 15 var year = specDate.getFullYear(); 16 var hours=specDate.getHours(); 17 var minute = specDate.getMinutes(); 18 var second = specDate.getSeconds(); 19 // get Month string 20 switch(formats[0].toUpperCase()){ 21 case "MM": // return 2 digits of month number, such as: 01 22 month = (month+"").length==1?"0"+month:month; 23 break; 24 case "MMM": //return the first 3 chars of the month word, such as: Jan 25 month = this.getMonthShortName(month); 26 break; 27 case "MMMM": //return the full word of the month word, such as: January 28 month = this.getMonthFullName(month); 29 break; 30 case "M": 31 default: 32 // return 1 digit when month is lower than 10. 33 // do nothing 34 } 35 36 //get Date string 37 switch(formats[1].toUpperCase()){ 38 case "DD": //always return 2 digits of the month number, such as: 05 39 date = (date+"").length==1?"0"+date:date; 40 break; 41 case "D": 42 date 43 default: 44 // return 1 digit when Date is lower than 10. 45 // do nothing 46 } 47 48 //get Year string 49 switch(formats[2].toUpperCase()){ 50 case "YY": // return last 2 digits of the year number, such as: 08 (2008) 51 year = (year+"").substr(2); 52 break; 53 case "YYYY": 54 default: 55 //return full year number, such: 2008. 56 } 57 58 storedVars[arrArgs[0]] = month; 59 storedVars[arrArgs[1]] = date; 60 storedVars[arrArgs[2]] = year; 61 storedVars[arrArgs[3]] = hours; 62 storedVars[arrArgs[4]] = minute; 63 storedVars[arrArgs[5]] = second; 64 } 65 66 Selenium.prototype.getMonthFullName = function(month){ 67 var monthArr = new Array("January","February","March","April","May","June","July","August","September","October","November","December"); 68 69 if(month == null){ 70 throw new SeleniumError("you didn't specify a Month"); 71 } 72 73 try{ 74 month = parseInt(month); 75 }catch (e){ 76 throw new SeleniumError("Month is not a Integer!"); 77 } 78 79 return monthArr[month-1]; 80 } 81 82 83 /* return the date N days(N*24 hours) before/after some day. 84 * args : num - positive/negative integer/float number,default is "1"; 85 * type - 0 (second) or 1 (day), default is second. 86 * return : Date 87 */ 88 function dateAfterDays(date, num, type){ 89 date = (date == null?new Date():date); 90 num = (num == null?1:num); 91 92 if(typeof(num)!="number") 93 throw new SeleniumError("dateAfterDays(date, num, type),num argument must be Number type."); 94 95 if(typeof(date)!="object") 96 throw new SeleniumError("dateAfterDays(date, num, type),date argument must be Date type."); 97 98 var iType = (type == null?0:type); 99 var arr = [1000,86400000]; 100 var dd = date.valueOf(); 101 dd += num * arr[iType]; 102 var d=new Date(dd); 103 return d; 104 } 105 106 function getSysDate(){ 107 return new Date(); 108 }
心若沉浮,浅笑安然