• Selenium Video Tutorials

Selenium - JUnit 報告



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

建立 JUnit 報告的先決條件

建立 JUnit 報告的步驟

步驟 1 - 建立一個 Maven 專案,併為以下專案在 pom.xml 檔案中新增適當的依賴項:

步驟 2 - 建立一個包含以下示例實現的 JUnit 測試類,我們將首先單擊新建使用者按鈕,然後驗證歡迎頁面上的文字歡迎,登入

Selenium JUnit Report 1

單擊新建使用者按鈕後,我們將導航到註冊頁面,其中包含以下影像中突出顯示的返回登入按鈕。

Selenium JUnit Report 2

程式碼實現

package Report;

import org.junit.*;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.TestMethodOrder;
import java.util.concurrent.TimeUnit;
import static org.junit.Assert.assertEquals;

@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class JunitTest {

   WebDriver driver;

   @Before
   public void setup() throws Exception{

      // Initiate browser driver
      driver = new ChromeDriver();

      // adding implicit wait of 20 secs
      driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);

      // Opening the webpage
      driver.get("https://tutorialspoint.tw/selenium/practice/login.php");
   }

   @Test
   @Order(1)
   public void verifyLoginAndRegisterPage() {
   
      // identify header then get text
      WebElement header = driver.findElement
         (By.xpath("//*[@id='signInForm']/h1"));
      String text = header.getText();

      // assertions to test case to check login page
      assertEquals("Welcome, Login In", text);

      // navigate to register page
      WebElement btn = driver.findElement
         (By.xpath("//*[@id='signInForm']/div[3]/a"));
      btn.click();

      // assertions added to test case to check register page
      WebElement btnchk = driver.findElement
         (By.xpath("//*[@id='signupForm']/div[5]/a"));
      boolean displayed = btnchk.isDisplayed();

      // assertions to test case
      assertEquals(true, displayed);
   }

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

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.junit.jupiter/junit-jupiter-api -->
      <dependency>
         <groupId>org.junit.jupiter</groupId>
         <artifactId>junit-jupiter-api</artifactId>
         <version>5.10.2</version>
      </dependency>
      
      <!-- https://mvnrepository.com/artifact/junit/junit -->
      <dependency>
         <groupId>junit</groupId>
         <artifactId>junit</artifactId>
         <version>4.13.2</version>
         <scope>test</scope>
      </dependency>

      <!-- https://mvnrepository.com/artifact/org.apache.maven.plugins/maven-site-plugin -->
      <dependency>
         <groupId>org.apache.maven.plugins</groupId>
         <artifactId>maven-site-plugin</artifactId>
         <version>4.0.0-M13</version>
      </dependency>
   </dependencies>

   <!-- https://mvnrepository.com/artifact/org.apache.maven.plugins/maven-surefire-report-plugin -->
   <reporting>
      <plugins>
         <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-report-plugin</artifactId>
            <version>3.2.5</version>
         </plugin>
      </plugins>
   </reporting>
</project>

步驟 3 - 使用以下命令從命令列執行測試:mvn clean test suite

步驟 4 - 重新整理專案,應該在target資料夾中生成一個名為site的新資料夾。

Selenium JUnit Report 3

步驟 5 - 右鍵單擊surefire-report.html,然後選擇在瀏覽器中開啟的選項。

Selenium JUnit Report 4

JUnit 報告將在瀏覽器中開啟,顯示摘要,總測試方法數為 1,透過成功率為 100%。它還顯示了包列表的詳細資訊(包名稱、測試數量、透過計數、失敗計數、透過成功率、測試持續時間等)。報告中還包含測試方法名稱 verifyLoginAndRegisterPage。

結論

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

廣告