美文网首页
Katalon - 自定义关键字方法(Keywords)

Katalon - 自定义关键字方法(Keywords)

作者: idealCity | 来源:发表于2019-12-30 12:56 被阅读0次

    问题

    在测试过程中会遇到一些常用的操作,但是Katalon中的keyword又没有提供,那么应该怎么扩展Katalon的keyword呢?比如,当我们点击了一个筛选器中显示近一个月更新的内容的按钮,我们需要判断这些内容是不是近一个月的,如果不是那么就报错。

    解决方案

    1.创建自定义Keyword

    依次点击File->New->Keyword,打开并填写Package和Class Name,这里可以勾选上生成Web的样例,这样可以参考一下如何编写,点击ok

    image.jpeg
    2. 编辑自定义keyword

    点击ok后,会自动新建一个有样例程序的以.groovy结尾的文件,接下来编写我们校验时间的方法,这个方法接受一个Elements对象,然后循环获取到每个Element的时间信息,最后根据这个时间信息与传入的天数进行比较,如果超过这个范围,标记一个错误。详细代码如下:

    class VerifyDateTime {
    
           @Keyword
           def verifyDateTime(List<WebElement> eles, int day) {
                  for(WebElement ele : eles){
                         DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
                         LocalDateTime updateDate = LocalDateTime.parse(ele.getText(), 
    formatter);
                         LocalDateTime now = LocalDateTime.now();
                         Duration duration = Duration.between(updateDate, now);
                         System.out.println('添加的日期是:'+ele.getText()+'与今天相差'+(duration.toDays())+'天');
                         if(duration.toDays()>day){
                               KeywordUtil.markFailed("Datetime display wrong!")
                         }
                  }
           }
    }
    
    3.使用自定义Keyword

    这个自定义的keyword生成好以后就可以拿来用了,点击Add->Custom Keyword就可以添加到当前的testcase中

    image.jpeg

    相关文章

      网友评论

          本文标题:Katalon - 自定义关键字方法(Keywords)

          本文链接:https://www.haomeiwen.com/subject/iezpoctx.html