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,我为自己代言!!!!!!!!!!!!!
  • 相关阅读:
    Mysql中判断一个点是否落在多边形内
    Linux进阶之环境变量文件/etc/profile、/etc/bashrc、/etc/environment
    gitlab打开group_member页面500错误
    jenkins连接k8s集群
    svn备份迁移
    npm管理nodejs版本
    linux远程执行shell脚本或者python脚本无法获取远程主机的环境变量
    ansible-playbook对服务器安装阿里云日志服务agent--ilogtail
    linux安装Arachni进行web网站扫描
    nginx使用免费ssl证书
  • 原文地址:https://www.cnblogs.com/xiamuyouren/p/3272931.html
Copyright © 2011-2022 走看看