• Selenium Video Tutorials

Selenium WebDriver - 瀏覽器選項



瀏覽器選項指的是所有瀏覽器共享的功能和能力。它有助於在任何瀏覽器上執行自動化測試時修改瀏覽器的設定和功能。預設情況下,Selenium Webdriver 從一個新的瀏覽器配置檔案開始,沒有任何關於 Cookie、歷史記錄等的預定義設定。

向瀏覽器新增擴充套件程式

讓我們舉一個例子,我們將使用 **ChromeOptions** 類啟動帶有 Selenium IDE 擴充套件程式的 Chrome 瀏覽器。每個 Chrome 擴充套件程式都應該有一個 .crx 檔案。在這裡,我們將獲取 Selenium IDE Chrome 擴充套件程式的 .crx 檔案,並將其儲存在專案中的 Resources 資料夾中。

Selenium Browser Options 1

示例

package org.example;

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

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

      // object of ChromeOptions class
      ChromeOptions opt = new ChromeOptions();

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

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

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

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

輸出

Process finished with exit code 0

在這裡,我們觀察到 Chrome 瀏覽器啟動了 Selenium IDE 擴充套件程式,並且資訊欄顯示 **Chrome 正在被自動化測試軟體控制**。

Selenium Browser Options 2

最後,我們收到訊息 **程序已完成,退出程式碼為 0**,表示程式碼成功執行。

以無頭模式開啟瀏覽器

在這個例子中,我們將使用 **EdgeOptions** 類在無頭瀏覽器中啟動一個應用程式。

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 EdgeHeadless {
   public static void main(String[] args) throws InterruptedException {

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

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

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

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

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

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

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

它將顯示以下輸出:

Getting the page title: Selenium Practice - Student Registration Form

在上面的示例中,我們觀察到 Edge 瀏覽器以 **無頭** 模式啟動。我們還在控制檯中獲得了瀏覽器標題和訊息 - **獲取頁面標題:Selenium 實踐 - 學生登錄檔單**。

在瀏覽器中建立配置檔案

讓我們再舉一個例子,我們將使用 **FirefoxOptions** 類在瀏覽器上擁有 Firefox 配置檔案。

package org.example;

import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.firefox.ProfilesIni;
import java.util.concurrent.TimeUnit;

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

      // object of FirefoxOptions class
      FirefoxOptions opt = new FirefoxOptions();

      // object of ProfilesIni
      ProfilesIni prof = new ProfilesIni();

      // get profile
      FirefoxProfile p = prof.getProfile("<profileName>");
      opt.setProfile(p);

      // Initiate the Webdriver
      WebDriver driver = new FirefoxDriver(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");

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

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

它將顯示以下輸出:

Obtain the page title: Selenium Practice - Student Registration Form

在這裡,Firefox 瀏覽器使用建立的配置檔案開啟。我們還在控制檯中獲得了瀏覽器標題和訊息 - **獲取頁面標題:Selenium 實踐 - 學生登錄檔單**。

處理 Edge 中的 SSL 證書錯誤

為了克服 Edge 瀏覽器中的 SSL 證書錯誤,我們將使用 EdgeOptions 和 DesiredCapabilities 類。

package org.example;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.EdgeDriver;
import org.openqa.selenium.firefox.EdgeOptions;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import java.util.concurrent.TimeUnit;

public class SSLEdge {
   public static void main(String[] args) throws InterruptedException {
   
      // object of Desired Capabilities class 
      DesiredCapabilities dc = new DesiredCapabilities();
      dc.setCapability(CapabilityType.ACCEPT_INSECURE_CERTS, true);
      
      // object Edge Options class
      EdgeOptionsOptions opt = new EdgeOptions();
      opt.merge(dc);
      
      // Initiate the Webdriver along with options
      WebDriver driver = new EdgeDriver(opt);
      
      // adding implicit wait of 5 secs
      driver.manage().timeouts().implicitlyWait(55, TimeUnit.SECONDS);
      
      // launch application with SSL certificate error
      driver.get("https://expired.badssl.com");
      
      // get browser title
      System.out.println("Get Browser title in Edge: " + driver.getTitle());
      
      // quit the browser
      driver.quit();
   }
}

它將顯示以下輸出:

Get Browser title in Edge: Privacy error

在這裡,我們擺脫了 Edge 中的 SSL 證書錯誤並啟動了應用程式。然後在控制檯中獲得了瀏覽器標題和訊息 - **Edge 中的瀏覽器標題:隱私錯誤**。

頁面載入策略

瀏覽器中有多種頁面載入策略可用,分別是 NORMAL(瀏覽器中的預設頁面載入設定)、EAGER 和 NONE。

package org.example;

import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;

public class ChromePageLoadNormal {
   public static void main(String[] args) throws InterruptedException {
   
      // object of ChromeOptions
      ChromeOptions opt = new ChromeOptions();
      
      // setting pageloadStrategy to normal
      opt.setPageLoadStrategy(PageLoadStrategy.NORMAL);
      
      // Initiate the Webdriver
      WebDriver driver = new ChromeDriver(opt);
      
      // Opening the webpage
      driver.get("https://tutorialspoint.tw/selenium/practice/register.php");
      
      // quit browser
      driver.quit();
   }
}

它將顯示以下輸出:

Process finished with exit code 0

在上面的示例中,我們設定了瀏覽器選項,使頁面載入策略設定為 NORMAL。如果需要將頁面載入策略設定為 EAGER,則需要進行以下程式碼更改:

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

// setting pageloadStrategy to EAGER
opt.setPageLoadStrategy(PageLoadStrategy.EAGER);

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

如果需要將頁面載入策略設定為 NONE,則需要進行以下程式碼更改:

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

// setting pageloadStrategy to NONE
opt.setPageLoadStrategy(PageLoadStrategy.NONE);

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

結論

本教程全面介紹了 Selenium Webdriver 瀏覽器選項。我們已經透過示例演示瞭如何向瀏覽器新增配置檔案和擴充套件程式,如何在無頭模式下啟動瀏覽器,如何處理 SSL 證書錯誤以及如何使用瀏覽器選項和 Selenium Webdriver 進行頁面載入策略。這使您能夠深入瞭解 Selenium Webdriver 中的瀏覽器選項。明智的做法是不斷練習您所學到的知識,並探索與 Selenium 相關的其他知識,以加深您的理解並拓寬您的視野。

廣告