Selenium 简明教程
Selenium WebDriver - FirefoxOptions
FirefoxOptions 是 Selenium Webdriver 中的一个特定类,它有助于处理仅适用于 Firefox 驱动的选项。它可以修改浏览器在 Firefox 上运行自动化测试时的设置和功能。FirefoxOptions 类扩展了另一个称为 MutableCapabilities 的类。FirefoxOptions 类在最新版本的 Selenium 中可用。
FirefoxOptions is a specific class in Selenium Webdriver which helps to handle options which are only applicable to the Firefox driver. It helps to modify the settings and capabilities of the browser while running an automated test on Firefox. The FirefoxOptions class extends another class known as the MutableCapabilities class. The FirefoxOptions class is available in the latest version of Selenium.
Open in Headless Browser Using FirefoxOptions
在此示例中,我们将使用 FirefoxOptions 类以 headless 模式打开并启动应用程序。
In this example, we would open and launch an application in the headless mode using the FirefoxOptions class.
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://www.tutorialspoint.com/selenium/practice/selenium_automation_practice.php");
// getting page title
System.out.println("Getting the page title: " + driver.getTitle());
// Quitting browser
driver.quit();
}
}
它将显示以下输出 −
It will show the following output −
Getting the page title: Selenium Practice - Student Registration Form
Process finished with exit code 0
在上面的示例中,我们观察到 Firefox 浏览器以无头模式启动。我们还通过控制台中的消息 Getting the page title: Selenium Practice Student Registration Form 获得了浏览器标题。
In the above example, we observed that the Firefox browser was launched in a headless mode. We had also obtained the browser title with the message in the console - Getting the page title: Selenium Practice Student Registration Form.
SSL Certificates Using FirefoxOptions
对于 Firefox 浏览器里的 SSL 证书错误,可以使用 FirefoxOptions 类和 DesiredCapabilities 类来处理。
SSL certificate errors in the Firefox browser, can be handled with the FirefoxOptions class along with the DesiredCapabilities class.
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();
}
}
它将显示以下输出 −
It will show the following output −
Browser title in Firefox: Privacy error
在上例中,我们在 Firefox 中遇到了 SSL Certificate 错误,随后我们启动应用程序,然后通过控制台中的消息得知浏览器标题为 Browser title in Firefox: Privacy error 。
In the above example, we had the SSL Certificate error in Firefox and launched the application and then obtained the browser title with the message in the console - Browser title in Firefox: Privacy error.
Firefox Profile Using FirefoxOptions
我们使用一个示例来说明我们如何为 Firefox 浏览器启动专用 Firefox 概要文件。
Let us take an example, where we would launch the Firefox browser with our own Firefox profile.
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://www.tutorialspoint.com/selenium/practice/selenium_automation_practice.php");
// getting page title
System.out.println("Getting the page title: " + driver.getTitle());
// Quitting browser
driver.quit();
}
}
它将显示以下输出 −
It will show the following output −
Getting the page title: Selenium Practice - Student Registration Form
在上例中,我们观察到 Firefox 浏览器使用一个已创建的概要文件启动。我们还通过控制台中的消息得知浏览器标题为 Getting the page title: Selenium Practice - Student Registration Form 。
In the above example, we observed that the Firefox browser was launched with a created profile. We had also obtained the browser title with the message in the console - Getting the page title: Selenium Practice - Student Registration Form.
Open Maximized Browser Using FirefoxOptions
在这个示例中,我们将在浏览器最大化尺寸状态下启动并打开一个应用程序。
In this example, we would open and launch an application in the browser maximized size.
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://www.tutorialspoint.com/selenium/practice/tabs.php");
// quitting browser
driver.quit();
}
}
它将显示以下输出 −
It will show the following output −
Process finished with exit code 0
在上例中,我们观察到 Firefox 浏览器以最大化状态启动。
In the above example, we observed that the Firefox browser was launched in a maximized browser.
Conclusion
这结束了我们对 Selenium Webdriver Firefox 选项教程的讲解。我们从 FirefoxOptions 类开始讲解,并逐步演示了如何向 Firefox 浏览器中添加概要文件,如何以最大化状态启动 Firefox 浏览器,如何在无头模式下打开 Firefox 浏览器,以及如何借助 FirefoxOptions 和 Selenium Webdriver 来处理 SSL 证书错误。这将让你对 Selenium Webdriver 中的 FirefoxOptions 类有深入的了解。明智的做法是继续练习你所学到的知识,并探索其他与 Selenium 相关的知识,以加深你的理解并拓展你的视野。
This concludes our comprehensive take on the tutorial on Selenium Webdriver Firefox Options. We’ve started with describing a FirefoxOptions class, and walked through examples of how to add profiles to Firefox browser, how to launch the Firefox browser maximized, how to open the Firefox browser in headless mode, and handle SSL certificates errors taking help of FirefoxOptions along with Selenium Webdriver. This equips you with in-depth knowledge of the FirefoxOptions class in Selenium Webdriver. It is wise to keep practicing what you’ve learned and exploring others relevant to Selenium to deepen your understanding and expand your horizons.