• Selenium Video Tutorials

Selenium WebDriver - Edge 選項



EdgeOptions 是 Selenium Webdriver 中的一個特定類,它有助於處理僅適用於 Edge 驅動程式的選項。它有助於在 Edge 上執行自動化測試時修改瀏覽器的設定和功能。EdgeOptions 類擴充套件了另一個稱為 MutableCapabilities 類的類。

EdgeOptions 類在最新版本的 Selenium 中可用。預設情況下,Selenium Webdriver 從一個新的瀏覽器配置檔案開始,該配置檔案沒有任何關於 Cookie、歷史記錄等的預定義設定。

使用 EdgeOptions 新增 Edge 擴充套件

使用 EdgeOptions 類啟動帶有 Selenium IDE 擴充套件的 Edge 瀏覽器。Edge 擴充套件應具有 .crx 副檔名。我們將保留擴充套件的 .crx 檔案,並將其放置在專案中的 Resources 資料夾下。

Selenium Edge Options 1

示例

package org.example;

import org.openqa.selenium.*;
import org.openqa.selenium.edge.EdgeDriver;
import org.openqa.selenium.edge.EdgeOptions;
import java.io.File;
import java.util.concurrent.TimeUnit;

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

      // object of EdgeOptions
      EdgeOptions opt = new EdgeOptions();

      // adding .crx extension to project structure
      opt.addExtensions(new File("./Resources/SeleniumIDE.crx"));

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

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

      // Opening the webpage with Selenium IDE extension
      driver.get("https://tutorialspoint.tw/selenium/practice/register.php");
   }
}

它將顯示以下輸出 -

Process finished with exit code 0
Selenium Edge Options 2

Edge 瀏覽器已啟動 Selenium IDE 擴充套件,以及資訊欄“**Microsoft Edge 正在由自動化測試軟體控制**”。

使用 EdgeOptions 停用彈出視窗阻止程式

讓我們舉一個例子,我們將開啟停用彈出視窗阻止程式的 Edge 瀏覽器。

package org.example;

import org.openqa.selenium.*;
import org.openqa.selenium.edge.EdgeDriver;
import org.openqa.selenium.edge.EdgeOptions;
import java.io.File;
import java.util.List;
import java.util.concurrent.TimeUnit;

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

      // object of EdgeOptions
      EdgeOptions opt = new EdgeOptions();

      // adding .crx extension to project structure
      opt.addExtensions(new File("./Resources/SeleniumIDE.crx"));

      // disable pop-up blocker
      opt.setExperimentalOption("excludeSwitches", List.of("disable-popup-blocking"));

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

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

      // Opening the webpage with Selenium IDE extension
      driver.get("https://tutorialspoint.tw/selenium/practice/register.php");
   }
}

它將顯示以下輸出 -

Process finished with exit code 0

在上面的示例中,Edge 瀏覽器已啟動 Selenium IDE 擴充套件,並且阻止了彈出視窗。

使用 EdgeOptions 開啟最大化瀏覽器

在此示例中,我們將以最大化大小開啟並啟動 Edge 瀏覽器。

package org.example;

import org.openqa.selenium.*;
import org.openqa.selenium.edge.EdgeDriver;
import org.openqa.selenium.edge.EdgeOptions;
import java.io.File;
import java.util.List;
import java.util.concurrent.TimeUnit;

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

      // object of EdgeOptions
      EdgeOptions opt = new EdgeOptions();

      // adding .crx extension
      opt.addExtensions(new File("./Resources/SeleniumIDE.crx"));

      // disable pop-up blocker
      opt.setExperimentalOption("excludeSwitches", List.of("disable-popup-blocking"));

      // open browser in maximized mode
      opt.addArguments("--start-maximized");

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

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

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

它將顯示以下輸出 -

Process finished with exit code 0

在上面的示例中,我們觀察到 Edge 瀏覽器已啟動 Selenium IDE 擴充套件,並帶有彈出視窗阻止程式,並且處於最大化瀏覽器狀態。

使用 EdgeOptions 在無頭瀏覽器中開啟

在此示例中,我們將以 Edge 無頭模式開啟並啟動應用程式。

package org.example;

import org.openqa.selenium.*;
import org.openqa.selenium.edge.EdgeDriver;
import org.openqa.selenium.edge.EdgeOptions;
import java.io.File;
import java.util.List;
import java.util.concurrent.TimeUnit;

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

      // object of EdgeOptions
      EdgeOptions opt = new EdgeOptions();

      // adding .crx extension to project structure
      opt.addExtensions(new File("./Resources/SeleniumIDE.crx"));

      // disable pop-up blocker
      opt.setExperimentalOption("excludeSwitches", List.of("disable-popup-blocking"));

      // open in headless mode
      opt.addArguments("--headless=new");

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

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

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

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

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

它將顯示以下輸出 -

Getting the page title: Selenium Practice - Student Registration Form

在上面的示例中,我們觀察到 Edge 瀏覽器已啟動 Selenium IDE 擴充套件,並帶有彈出視窗阻止程式,並且處於無頭模式。我們還在控制檯中獲得了帶有訊息的瀏覽器標題 - **在無頭模式下獲取頁面標題:Selenium 實踐 - 進度條**。

結論

這總結了我們關於 Selenium Webdriver Edge 選項教程的全面內容。我們從描述 EdgeOptions 類開始,並逐步介紹瞭如何向 Edge 瀏覽器新增擴充套件、如何阻止彈出視窗、如何最大化瀏覽器以及如何使用 EdgeOptions 和 Selenium Webdriver 處理無頭 Edge 瀏覽器執行的示例。這使您深入瞭解 Selenium Webdriver 中的 EdgeOptions 類。明智的做法是不斷練習您所學的內容,並探索與 Selenium 相關的其他內容,以加深您的理解並擴充套件您的視野。

廣告