qileilove

          blog已經轉移至github,大家請訪問 http://qaseven.github.io/

          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
          01

          posted on 2013-10-08 11:35 順其自然EVO 閱讀(265) 評論(0)  編輯  收藏 所屬分類: selenium and watir webdrivers 自動化測試學習

          <2013年10月>
          293012345
          6789101112
          13141516171819
          20212223242526
          272829303112
          3456789

          導航

          統計

          常用鏈接

          留言簿(55)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 安远县| 渝中区| 潜江市| 越西县| 镇江市| 正镶白旗| 师宗县| 祥云县| 玛纳斯县| 建平县| 新巴尔虎右旗| 稻城县| 任丘市| 昌邑市| 景德镇市| 开平市| 江门市| 南丹县| 慈利县| 浦北县| 永和县| 连云港市| 苍南县| 太仆寺旗| 富蕴县| 剑河县| 柘城县| 鄂伦春自治旗| 邯郸县| 瑞丽市| 于都县| 东丰县| 佛冈县| 响水县| 格尔木市| 京山县| 施秉县| 宁安市| 兴宁市| 靖边县| 永年县|