Maven-UI自动化框架引入allure报告


1.防止项目jdk构建版本变动

<build>
        <plugins>
            <!-- 指定jdk版本,防止jdk构建版本变动 解决每次右键项目名-maven->update project 时候,项目jdk版本变了,变回1.5版本或者其他版本
                解决使用maven编译其他问题:如提示不能在内部类访问外部非final局部变量 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.5.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
        </plugins>
 </build>

2.配置allure报告

  1. 配置pom.xml依赖文件
    1. <!-- allure测试报表 -->
              <dependency>
                  <groupId>io.qameta.allure</groupId>
                  <artifactId>allure-testng</artifactId>
                  <version>2.12.1</version>
                  <scope>test</scope>
              </dependency>
  2. 配置surefire插件,供Jenkins持续集成时maven插件调用
    1. <!-- 为了后期集成jenkins等自动化平台,我们必须保证自己的脚本可以通过命令脚本来调动执行。 实现方式:集成maven的surefire插件,
                      Surefire插件用于Maven项目的test阶段,以执行单元测试。集成后我们就可以通过maven命令 maven test 来调动脚本执行了。 -->
                  <plugin>
                      <groupId>org.apache.maven.plugins</groupId>
                      <artifactId>maven-surefire-plugin</artifactId>
                      <version>2.22.1</version>
                      <configuration>
                          <argLine>
                              -javaagent:"${settings.localRepository}/org/aspectj/aspectjweaver/${aspectj.version}/aspectjweaver-${aspectj.version}.jar"
                          </argLine>
                          <suiteXmlFiles>
      <!--此处在jenkins 调用mvn test执行时,选取执行的文件,可以为多个suiteXmlFiles,根路径为当前项目名-->
                              <suiteXmlFile>testng.xml</suiteXmlFile>
                          </suiteXmlFiles>
                      </configuration>
                      <dependencies>
                          <dependency>
                              <groupId>org.aspectj</groupId>
                              <artifactId>aspectjweaver</artifactId>
                              <version>${aspectj.version}</version>
                          </dependency>
                      </dependencies>
                  </plugin>
    2. 需要添加${aspectj.version}类似的属性在pom.xml文件中
      <properties>
              <aspectj.version>1.8.10</aspectj.version>
              <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
              <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
              <maven.compiler.encoding>UTF-8</maven.compiler.encoding>
          </properties>
  3. 将UI自动化中的截图作为Allure报告中的附件
    1. 此处以testNG为例,提供一个测试监听器,监听失败时截图并添加至allure报告
       1 public class WebAutoListener extends TestListenerAdapter {
       2     @Override
       3     public void onTestFailure(ITestResult tr) { 6         // 截图
       7         String methodName = tr.getMethod().getMethodName()+System.currentTimeMillis();
       8         takeScreenShot(methodName);
       9     }
      10 
      11     @Attachment(value = "Failure in method {0}", type = "image/png")  // 此处将调用此方法时的返回值转化为图片附件添加至对应方法的allure报告中
      12     private byte[] takeScreenShot(String methodName){
      13         TakesScreenshot takesScreenshot= (TakesScreenshot) BaseTester.getDriver();
      14         File screenFile = takesScreenshot.getScreenshotAs(OutputType.FILE);
      15         try {
      16             FileUtils.copyFile(screenFile, new File("/Users/userName/IdeaProjects/EasyUi/target/screenShort/"+methodName+".jpg"));
      17         } catch (IOException e) {
      18             e.printStackTrace();
      19         }
      20         return takesScreenshot.getScreenshotAs(OutputType.BYTES);
      21     }
      22 
      23 }

       

    2. 测试生成的报告
      1. 如果已经集成好jenkins,可以直接运行jenkins来测试报告生成,
      2. 使用本地安装的allure-commandline测试,测试代码 allure serve allure-results 在当前文件下生测测试报告可进入allure自启动的网址端口查看


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM