zoukankan      html  css  js  c++  java
  • javatestngselenium优化

    由于项目中webui测试的需要,是用testng+selenium的方式,其中遇到过几个问题,记录下,方便以后查看

    1.重复运行多次case

    因为是selenium,所以有的时候需要运行多次,方法是写一个Retry的类,继承testng的retry类就可以。

    public class Retry implements IRetryAnalyzer {
        private int retryCount = 0;
        private int maxRetryCount = 3;
    
        public boolean retry(ITestResult result) {
    
            if (retryCount < maxRetryCount) {
                retryCount++;
                System.out.println(String.format("test failed!try %d time!",retryCount+1));
                return true;
            }
            return false;
        }
    运行的时候就在case上加上
    @Test(retryAnalyzer=Retry.class)
    就可以拉

     2.自定义annotions

    虽然testng自带有很多注释,很好用,但是在项目中,组长要求在每个case前面能够描述一下这个case的作用,以后当case失败的时候,可以输出这个case的作用和失败原因,要外人不看代码也可以看的懂,所以就想自定义一个annotation,添加个decsription属性。

     1 //UIMessage.java
     2 import static com.myhexin.common.Assert.AssertEquals;
     3 
     4 import java.lang.annotation.Retention;
     5 import java.lang.annotation.RetentionPolicy;
     6 import java.lang.annotation.Target;
     7 import java.lang.annotation.ElementType;
     8 
     9 import org.testng.annotations.Parameters;
    10 import org.testng.annotations.Test;
    11 @Retention(RetentionPolicy.RUNTIME)
    12 @Target({ElementType.METHOD})
    13 public @interface UIMessage {
    14      String description();
    15 }
    16 
    17 //应用
    18 @UIMessage(description = "我是来试试的")@Test(retryAnalyzer=Retry.class)
    19 @Parameters()
    20 public void testDengLuZhangHao(){
    21 /*
    22  * 函数体
    23  */
    24 }    
    25 //如何获取annotation的注释消息,我是用反射来获取的
    26 
    27 Method[] methods=Class.forName("core.Member").getDeclaredMethods();
    28             for(Method method:methods)
    29            {
    30                  if(method.isAnnotationPresent( UIMessage. class))
    31                 {
    32                      System. out.println(method.getAnnotation( UIMessage. class).desciption());
    33                 }
    34            }

     3.selenium截图

    目前该截图方式当在remote执行selenium时候,我只是在firefox下成功过,ie打死不行,场景是当case运行失败的时候截图,主要是重写下assert,当断言失败时候,截图,直接上,截图的代码

        public  void screenShot(WebDriver driver,int i)
        {
                String dir_name="./pic";
              if (!(new File(dir_name).isDirectory())) {  // 判断是否存在该目录
                 new File(dir_name).mkdir();  // 如果不存在则新建一个目录
              }
             
              SimpleDateFormat sdf = new SimpleDateFormat("-HHmmss");
              String time = sdf.format(new Date()); 
             WebDriver augmentedDriver = new Augmenter().augment(driver);
            
              try {
                 File screenshot = ((TakesScreenshot)augmentedDriver).getScreenshotAs(OutputType.FILE); // 关键代码,执行屏幕截图,默认会把截图保存到temp目录
                 FileUtils.copyFile(screenshot, new File(dir_name + File.separator + i+ time  + ".png"));  // 这里将截图另存到我们需要保存
              } catch (IOException e) {
                 e.printStackTrace();
              }
        }
    就算不在校园,也要保持饥渴,学到老,活到老嘛 我是一个小QA,我为自己代言!!!!!!!!!!!!!
  • 相关阅读:
    PHP面试总结
    yii2-dingtalk 钉钉群机器人
    分布式锁机制原理及实现方式
    strtotime的一个使用问题
    JavaScript的程序构成
    libsvm源码凝视+算法描写叙述:svm_train
    android事件分发(二)
    Windows 上通过本地搭建 Jekyll环境
    重点:用户画像
    easyui英文提示变中文
  • 原文地址:https://www.cnblogs.com/xiamuyouren/p/3272931.html
Copyright © 2011-2022 走看看