• Selenium Video Tutorials

Selenium WebDriver - FirefoxOptions



FirefoxOptions 是 Selenium WebDriver 中的一個特定類,用於處理僅適用於 Firefox 驅動的選項。它有助於在 Firefox 上執行自動化測試時修改瀏覽器的設定和功能。FirefoxOptions 類擴充套件了另一個稱為 MutableCapabilities 的類。FirefoxOptions 類在最新版本的 Selenium 中可用。

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

在這個例子中,我們將使用 FirefoxOptions 類在無頭模式下開啟和啟動一個應用程式。

package org.example;

import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
import java.io.File;
import java.util.List;
import java.util.concurrent.TimeUnit;

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

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

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

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

      // adding implicit wait of 15 secs
      driver.manage().timeouts().implicitlyWait(15, 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());

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

它將顯示以下輸出:

Getting the page title: Selenium Practice - Student Registration Form

Process finished with exit code 0

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

使用 FirefoxOptions 處理 SSL 證書

Firefox 瀏覽器中的 SSL 證書錯誤可以使用 FirefoxOptions 類和 DesiredCapabilities 類一起處理。

package org.example;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import java.util.concurrent.TimeUnit;

public class SSLErrorFirefox {
   public static void main(String[] args) throws InterruptedException {
   
      // Desired Capabilities class to profile Firefox
      DesiredCapabilities dc = new DesiredCapabilities();
      dc.setCapability(CapabilityType.ACCEPT_INSECURE_CERTS, true);
      
      // Firefox Options class
      FirefoxOptions opt = new FirefoxOptions();
      opt.merge(dc);
      
      // Initiate the Webdriver with options
      WebDriver driver = new FirefoxDriver(opt);
      
      // adding implicit wait of 15 secs
      driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
      
      // launch application with SSL error
      driver.get("https://expired.badssl.com");
      
      // get browser title
      System.out.println("Browser title in Firefox: " + driver.getTitle());
      
      // quitting the browser
      driver.quit();
   }
}

它將顯示以下輸出:

Browser title in Firefox: Privacy error

在上面的例子中,我們在 Firefox 中出現了 SSL 證書 錯誤,啟動了應用程式,然後在控制檯中獲得了瀏覽器標題和訊息 - **Firefox 中的瀏覽器標題:隱私錯誤**。

使用 FirefoxOptions 使用 Firefox 配置檔案

讓我們來看一個例子,我們將使用我們自己的 Firefox 配置檔案啟動 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 FirefoxOptnsProfile {
   public static void main(String[] args) throws InterruptedException {

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

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

      // object of ProfilesIni
      FirefoxProfile p = prof.getProfile("<profileName>");
      opt.setProfile(p);

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

      // adding implicit wait of 15 secs
      driver.manage().timeouts().implicitlyWait(15, 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());

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

它將顯示以下輸出:

Getting the page title: Selenium Practice - Student Registration Form

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

使用 FirefoxOptions 開啟最大化瀏覽器

在這個例子中,我們將開啟並啟動一個以最大化瀏覽器大小的應用程式。

package org.example;

import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions
import java.io.File;
import java.util.Collections;
import java.util.concurrent.TimeUnit;

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

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

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

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

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

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

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

它將顯示以下輸出:

Process finished with exit code 0

在上面的例子中,我們觀察到 Firefox 瀏覽器以最大化視窗啟動。

結論

本教程全面介紹了 Selenium WebDriver Firefox Options。我們首先介紹了 FirefoxOptions 類,並逐步講解了如何向 Firefox 瀏覽器新增配置檔案、如何最大化啟動 Firefox 瀏覽器、如何在無頭模式下開啟 Firefox 瀏覽器以及如何在 Selenium WebDriver 的幫助下處理 FirefoxOptions 的 SSL 證書錯誤。這將使您深入瞭解 Selenium WebDriver 中的 FirefoxOptions 類。最好繼續練習您所學的內容,並探索與 Selenium 相關的其他內容,以加深您的理解並擴充套件您的視野。

廣告