Selenium 简明教程

Selenium WebDriver - Browser Options

浏览器选项是指所有浏览器共享的功能。在任何浏览器上运行自动化测试时,它有助于修改浏览器的设置和功能。默认情况下,Selenium Webdriver 从全新浏览器配置文件开始,对于 cookie、历史记录等没有任何预定义设置。

The browser options refer to the features and capabilities shared by all browsers. It helps to modify the settings and capabilities of the browser while running an automated test on any browser. Selenium Webdriver begins with a fresh browser profile without any predefined settings on cookies, history, and so on by default.

Adding Extensions to Browser

让我们举一个例子,它使用 * ChromeOptions* 类使用 Selenium IDE 扩展启动 Chrome 浏览器。每个 Chrome 扩展都应该有一个 .crx 文件。此处,将获取 Selenium IDE Chrome 扩展的 .crx 文件并将其存储在项目中的 Resourcs 文件夹中。

Let us take an example, where we would launch the Chrome browser with a Selenium IDE extension using the ChromeOptions class. Every Chrome extension should have a .crx file. Here, we would obtain the .crx file for Selenium IDE Chrome extension and store it within the Resources folder in project.

selenium browser options 1

Example

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://www.tutorialspoint.com/selenium/practice/register.php");
   }
}
Process finished with exit code 0

此处,观察到 Chrome 浏览器已使用 Selenium IDE 扩展启动,同时还显示了信息栏 Chrome is being controlled by automated test software

Here, we observed that the Chrome browser got launched with the Selenium IDE extension along with the information bar Chrome is being controlled by automated test software.

selenium browser options 2

最后,收到了消息 Process finished with exit code 0 ,表示代码成功执行。

Finally, the message Process finished with exit code 0 was received, signifying successful execution of the code.

Open Browser in Headless Mode

在此示例中,将使用 * EdgeOptions* 类在 headless 浏览器中启动应用程序。

In this example, we would launch an application in the headless browser using the EdgeOptions class.

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://www.tutorialspoint.com/selenium/practice/selenium_automation_practice.php");

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

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

它将显示以下输出 −

It will show the following output −

Getting the page title: Selenium Practice - Student Registration Form

在上述示例中,观察到 Edge 浏览器在 * headless* 模式中启动。我们还获取了带有控制台消息的浏览器标题 - Getting the page title: Selenium Practice - Student Registration Form

In the above example, we observed that the Edge 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.

Create Profiles in Browser

让我们看另一个示例,它使用 * FirefoxOptions* 类在浏览器上获取 Firefox 配置文件。

Let us take another example, where we would have the Firefox profiles on the browser using the FirefoxOptions class.

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://www.tutorialspoint.com/selenium/practice/selenium_automation_practice.php");

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

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

它将显示以下输出 −

It will show the following output −

Obtain the page title: Selenium Practice - Student Registration Form

此处,Firefox 浏览器使用创建的配置文件打开。我们还获取了带有控制台消息的浏览器标题 - Obtain the page title: Selenium Practice - Student Registration Form

Here, the Firefox browser opened with a created profile. We had also retrieved the browser title with the message in the console - Obtain the page title: Selenium Practice - Student Registration Form.

Handle SSL Certificate Errors in Edge

为了克服 Edge 浏览器中的 SSL 证书错误,将使用 EdgeOptions 和 DesiredCapabilities 类。

To overcome SSL certificates error in Edge browser, we would utilize the EdgeOptions and the DesiredCapabilities class.

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();
   }
}

它将显示以下输出 −

It will show the following output −

Get Browser title in Edge: Privacy error

此处,消除了 Edge 中的 SSL 证书错误并启动了应用程序。然后获取带有控制台消息的浏览器标题 - Browser title in Edge: Privacy error

Here, we got rid of the SSL certificate error in Edge and launched the application. Then got the browser title with the message in the console - Browser title in Edge: Privacy error.

Page Loading Strategy

浏览器中有可用的多种页面加载策略,它们是 NORMAL(浏览器中的默认页面加载设置)、EAGER 和 NONE。

There are multiple page loading strategies available in the browser, they are NORMAL (default page load setting in browser), EAGER, and 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://www.tutorialspoint.com/selenium/practice/register.php");

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

它将显示以下输出 −

It will show the following output −

Process finished with exit code 0

上述示例已将浏览器选项设置为将页面加载策略设置为 NORMAL。如果需要设置页面加载策略为 EAGER,则必须进行以下代码更改 −

In the above example, we had set the browser option set such that page load strategy is set to NORMAL. In case, we need to set the page load strategy is set to EAGER, we have to make the below code change −

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

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

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

如果需要设置页面加载策略为 NONE,则必须进行以下代码更改 −

In case, we need to set the page load strategy is set to NONE, we have to make the below code change −

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

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

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

Conclusion

指引教程:Selenium Webdriver 浏览器选项,本文到这里就结束了。我们逐步讲解了如何向浏览器添加配置文件和扩展名、如何在 headless 模式下启动浏览器、如何处理 SSL 证书错误以及使用 Selenium Webdriver 设置浏览器的页面加载策略。这让你全方位地了解了 Selenium Webdriver 中的浏览器选项。建议保持练习所学内容并探索其他与 Selenium 相关的内容,以加深理解并开阔视野。

This concludes our comprehensive take on the tutorial on Selenium Webdriver Browser Options. We’ve walked through examples of how to add profiles and extensions to browsers, how to launch the browser in headless mode, how to handle SSL certificates errors, and page loading strategies with browser options along with Selenium Webdriver. This equips you with in-depth knowledge of the browser options 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.