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