• Selenium Video Tutorials

Selenium - Allure



Allure 可用於建立詳細的自動化測試報告。它是一個開源框架,可以與 Selenium 測試整合並用於報告目的。

建立 Allure 報告的先決條件

在系統中安裝 Java(版本高於 8)並使用命令檢查它是否存在:java -version。如果安裝成功完成,則將顯示已安裝的 Java 版本。可以使用連結 Java 下載 下載和安裝 Java。

要了解更多關於 Java 設定的知識,請參閱以下連結 -

https://www.youtube.com/watch?v=bxIZ1GVWYkQ.

在系統中安裝 Maven 並使用命令檢查它是否存在:mvn -version。如果安裝成功完成,則將顯示已安裝的 Maven 版本。可以使用連結 Apache Maven 下載和安裝 Maven。

要了解更多關於 Maven 設定的知識,請參閱連結 Maven 環境搭建

安裝 IntelliJ。要了解更多關於 IntelliJ 設定的知識,請參閱連結 Selenium IntelliJ

建立 Allure 報告的步驟

步驟 1 - 建立一個 Maven 專案,並將以下專案的依賴項新增到 pom.xml 檔案中 -

  • 從連結 TestNG 新增 TestNG 依賴項。

  • 從連結 Selenium Java 新增 Selenium Java 依賴項

  • 從連結 Allure TestNG 新增 Allure TestNG 依賴項

  • 儲存包含所有依賴項的 pom.xml 並更新 Maven 專案。

步驟 2 - 在系統中下載 Allure。對於 Mac 和 Linux,執行命令:brew install allure。如果系統中安裝了 Homebrew,則此命令應該可以工作。對於 Windows,執行命令:scoop install allure。如果系統中安裝了 Scoop,則此命令應該可以工作。使用命令檢查 Allure 是否存在:allure –version。如果安裝成功完成,則將顯示已安裝的 Allure 版本。

步驟 3 - 建立一個 TestNG 測試類,並注意以下示例。在歡迎頁面上點選新建使用者按鈕

Selenium Allure 1

點選新建使用者後,我們將進入註冊頁面,頁面上顯示歡迎,註冊文字,如下面的圖片所示。

Selenium Allure 2

程式碼實現

package Report;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.*;
import java.util.concurrent.TimeUnit;
import static org.testng.Assert.assertEquals;

public class AllureTest {

   WebDriver driver;
   @BeforeTest
   public void setup(@Optional String browser) throws Exception{

      // Initiate browser driver
      driver = new ChromeDriver();
	  
      // adding implicit wait of 12 secs
      driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
	  
      // Opening the webpage
      driver.get("https://tutorialspoint.tw/selenium/practice/login.php");
   }
   @Test(priority = 1)
   public void verifyWelcomePageHeading() {
   
      // identify header then get text
      WebElement header = driver.findElement
         (By.xpath("//*[@id='signInForm']/h1"));
      String text = header.getText();
	  
      // assertion to verify login page header
      assertEquals("Welcome, Login In", text);
   }
   @Test(priority = 2)
   public void moveToRegisterPage() {
   
      // identify button then click
      WebElement btn = driver.findElement
         (By.xpath("//*[@id='signInForm']/div[3]/a"));
      btn.click();
   }
   @Test(priority = 3)
   public void verifyRegisterPageHeading() {
   
      // identify header then get text
      WebElement heder = driver.findElement
         (By.xpath("//*[@id='signupForm']/h1"));
      String text = heder.getText();
	  
      // assertion to verify register page header
      assertEquals("Welcome,Register", text);
   }

   @AfterTest
   public void teardown() {
   
      // quitting browser
      driver.quit();
   }
}

testng.xml 檔案中的配置。

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="All Test Suite">
   <test verbose="2" preserve-order="true" name="AllureTest.java">
      <classes>
         <class name="Report.AllureTest">
            <methods>
               <include name="verifyWelcomePageHeading"/>
               <include name="moveToRegisterPage"/>
               <include name="verifyRegisterPageHeading"/>
            </methods>
         </class>
      </classes>
   </test>
</suite>

pom.xml 檔案中的依賴項。

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
   http://maven.apache.org/xsd/maven-4.0.0.xsd">
   
   <modelVersion>4.0.0</modelVersion>
   <groupId>org.example</groupId>
   <artifactId>SeleniumJava</artifactId>
   <version>1.0-SNAPSHOT</version>
   
   <properties>
      <maven.compiler.source>16</maven.compiler.source>
      <maven.compiler.target>16</maven.compiler.target>
      <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
   </properties>
   
   <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
   <dependencies>
      <dependency>
         <groupId>org.seleniumhq.selenium</groupId>
         <artifactId>selenium-java</artifactId>
         <version>4.11.0</version>
      </dependency>
      
      <!-- https://mvnrepository.com/artifact/org.testng/testng -->
      <dependency>
         <groupId>org.testng</groupId>
         <artifactId>testng</artifactId>
         <version>7.9.0</version>
         <scope>test</scope>
      </dependency>
      
      <!-- https://mvnrepository.com/artifact/io.qameta.allure/allure-testng -->
      <dependency>
         <groupId>io.qameta.allure</groupId>
         <artifactId>allure-testng</artifactId>
         <version>2.25.0</version>
      </dependency>
   </dependencies>
</project>

以上實現的專案結構如下圖所示 -

Selenium Allure 3

步驟 4 - 使用 testng.xml 執行測試。

它將顯示以下輸出 -

===============================================
All Test Suite
Total tests run: 3, Passes: 3, Failures: 0, Skips: 0
===============================================
Process finished with exit code 0

我們使用了 TestNG 框架來設計測試並獲取頁面標題,最後對其進行驗證。

控制檯中的結果顯示總共執行的測試:3,因為有三個帶有 @Test 註解的方法 - verifyWelcomePageHeading()、moveToRegisterPage() 和 verifyRegisterPageHeading()。

最後,收到訊息透過:3程序已完成,退出程式碼為 0,表示程式碼執行成功。

步驟 5 - 重新整理專案資料夾,一個名為allure-results的新資料夾應該會生成。

Selenium Allure 4

步驟 6 - 從專案資料夾位置執行命令:allure serve。這裡,專案資料夾名稱為 Selenium Java。執行命令後,伺服器啟動,同時 Allure 報告將在瀏覽器中開啟,總測試方法(或用例)數為 3,透過率為 100%。

Selenium Allure 5

點選左側的套件選項卡,我們將獲得有關測試方法 moveToRegisterPage()、verifyPageHeading() 和 verifyWelcomePageHeading() 的執行持續時間及其結果的資訊。

Selenium Allure 6

切換到左側的圖表選項卡,我們將獲得有關狀態、嚴重性、持續時間等的所有資訊。

Selenium Allure 7

結論

這總結了我們對 Selenium Allure 教程的全面介紹。我們從描述 Allure 報告、設定 Allure 報告的先決條件開始,並逐步演示瞭如何使用示例建立 Allure 報告,說明了如何將其與 Selenium 一起使用。這使您能夠深入瞭解 Allure。明智的做法是不斷練習您所學到的知識,並探索與 Selenium 相關的其他內容,以加深您的理解並擴充套件您的視野。

廣告