• Selenium Video Tutorials

Selenium WebDriver - 無頭執行



Selenium Webdriver 可用於在任何瀏覽器上無頭執行測試。無頭執行是在瀏覽器不可見的情況下執行測試。

什麼是無頭執行?

現在更傾向於無頭執行,因為測試用例的執行速度更快,因為在此過程中不會呈現網頁。此外,在無頭模式下執行測試時,所需的系統資源更少。在執行測試以從 Web 應用程式中抓取資料時,大多數情況下都首選無頭執行。

此外,如果我們觸發涉及多個瀏覽器的並行執行,如果我們進行無頭執行,則資源利用率將受到限制。

但是,我們無法對測試進行即時除錯,也無法在測試失敗時使用無頭執行模式向開發人員報告問題。

示例 1

讓我們以以下頁面為例,我們將在 Chrome 瀏覽器中以無頭模式啟動具有 URL 的應用程式,Selenium 版本高於 4。可以使用 Selenium Webdriver 中的 ChromeOptions 類實現無頭執行。

然後,獲取其頁面標題:**Selenium 實踐 - 學生登錄檔單**。接下來,我們將在“姓名”標籤旁邊的輸入框中輸入文字**Selenium**。

Selenium Headless Execution 1

然後,我們將單擊“登入”按鈕,之後我們將導航到另一個頁面,該頁面的瀏覽器標題為**Selenium 實踐 - 登入**。

Selenium Headless Execution 2

程式碼實現

package org.example;

import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.chrome.ChromeOptions;

public class Headless {
   public static void main(String[] args) throws InterruptedException {

      // setting Chrome options to browser in headless mode
      ChromeOptions opt = new ChromeOptions();
      opt.addArguments("--headless=new");

      // Initiate the Webdriver
      WebDriver driver = new ChromeDriver(opt);

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

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

      // getting page title
      System.out.println("Getting the page title: " + driver.getTitle());

      // identify edit box then enter text
      WebElement e = driver.findElement(By.xpath("//*[@id='name']"));
      e.sendKeys("Selenium");

      // get test entered
      System.out.println("Value entered: " + e.getAttribute("value"));

      // perform click
      WebElement b = driver.findElement(By.xpath("//*[@id='collapseTwo']/div/ul/li[2]/a"));
      b.click();

      // getting page title after click
      System.out.println("Getting the current title after click: " + driver.getTitle());

      // 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>
   </dependencies>
</project>

輸出

Getting the page title: Selenium Practice - Student Registration Form
Value entered: Selenium
Getting the current title after click: Selenium Practice - Login

Process finished with exit code 0

在上面的示例中,我們以無頭模式在瀏覽器上啟動了一個 URL,並獲得了瀏覽器頁面標題,並在控制檯中顯示了訊息 - **獲取頁面標題:Selenium 實踐 - 學生登錄檔單**。然後,我們在輸入框中輸入文字 Selenium 並檢索輸入的值,並在控制檯中顯示訊息 - **輸入的值:Selenium**。之後,我們單擊“登入”連結並在導航到下一頁後獲取當前頁面標題,並在控制檯中顯示訊息:**單擊後獲取當前標題:Selenium - Selenium 實踐 - 登入**。

示例 2

讓我們以以下頁面為例,我們將在 Chrome 瀏覽器中以無頭模式啟動一個應用程式並開啟一個 URL,並獲取其頁面標題 - **Selenium 實踐 - 日期選擇器** 這將透過使用 ChromeOptions 類來完成。然後將值 --headless=new 傳遞給 addArguments 方法。

Selenium Headless Execution 3

程式碼實現

package org.example;

import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.chrome.ChromeOptions;

public class HeadlessBrow {
   public static void main(String[] args) throws InterruptedException {

      // setting Chrome options to browser in headless mode
      ChromeOptions opt = new ChromeOptions();
      opt.addArguments("--headless=new");

      // Initiate the Webdriver
      WebDriver driver = new ChromeDriver(opt);

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

      // Opening the webpage
      driver.get("https://tutorialspoint.tw/selenium/practice/date-picker.php");

      // getting page title
      System.out.println("Getting the page title: " + driver.getTitle());

      // Quitting browser
      driver.quit();
   }
}

輸出

Getting the page title: Selenium Practice - Date Picker

在上面的示例中,我們以無頭模式在瀏覽器上啟動了一個 URL,並獲得了瀏覽器頁面標題,並在控制檯中顯示了訊息 - **獲取頁面標題:Selenium 實踐 - 日期選擇器**。

結論

這總結了我們關於 Selenium Webdriver 無頭執行教程的全面概述。我們從描述什麼是無頭執行開始,並逐步介紹瞭如何使用 Selenium Webdriver 處理無頭執行的示例。這使您能夠深入瞭解 Selenium Webdriver 無頭執行。明智的做法是不斷練習您學到的知識,並探索與 Selenium 相關的其他內容,以加深您的理解並擴充套件您的視野。

廣告