多测师是一家拥有先进的教学理念,强大的师资团队,业内好评甚多的接口自动化测试培训机构!

17727591462

联系电话

您现在所在位置:接口自动化测试培训 > 新闻资讯

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

更新时间:2022-05-13 09:15:19 作者:多测师 浏览:54

  用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();

  }

  以上内容为大家介绍了自动化测试中的Selenium IDE 扩展函数:日期计算与表现,本文由多测师亲自撰写,希望对大家有所帮助。了解更多自动化测试相关知识:https://www.aichudan.com/xwzx/

联系电话

17727591462

返回顶部