• Selenium Video Tutorials

Selenium WebDriver - 无头执行



Selenium Webdriver 可用于在任何浏览器上无头执行测试。无头执行是在浏览器不可见的情况下执行测试。

什么是无头执行?

现在更倾向于无头执行,因为测试用例的执行速度更快,因为在此过程中不会呈现网页。此外,在无头模式下执行测试时,所需的系统资源更少。在运行测试以从 Web 应用程序中抓取数据时,大多数情况下都首选无头执行。

此外,如果我们触发涉及多个浏览器的并行执行,如果我们进行无头执行,则资源利用率将受到限制。

但是,我们无法对测试进行实时调试,也无法在测试失败时使用无头执行模式向开发人员报告问题。

示例 1

让我们以以下页面为例,我们将在 Chrome 浏览器中以无头模式启动具有 URL 的应用程序,Selenium 版本高于 4。可以使用 Selenium Webdriver 中的 ChromeOptions 类实现无头执行。

然后,获取其页面标题:**Selenium 实践 - 学生注册表单**。接下来,我们将在“姓名”标签旁边的输入框中输入文本**Selenium**。

Selenium Headless Execution 1

然后,我们将单击“登录”按钮,之后我们将导航到另一个页面,该页面的浏览器标题为**Selenium 实践 - 登录**。

Selenium Headless Execution 2

代码实现

package org.example;

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

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

      // setting Chrome options to browser in headless mode
      ChromeOptions opt = new ChromeOptions();
      opt.addArguments("--headless=new");

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

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

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

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

      // identify edit box then enter text
      WebElement e = driver.findElement(By.xpath("//*[@id='name']"));
      e.sendKeys("Selenium");

      // get test entered
      System.out.println("Value entered: " + e.getAttribute("value"));

      // perform click
      WebElement b = driver.findElement(By.xpath("//*[@id='collapseTwo']/div/ul/li[2]/a"));
      b.click();

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

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

在 pom.xml 文件中添加的依赖项 -

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
   
   <modelVersion>4.0.0</modelVersion>
   <groupId>org.example</groupId>
   <artifactId>SeleniumJava</artifactId>
   <version>1.0-SNAPSHOT</version>

   <properties>
      <maven.compiler.source>16</maven.compiler.source>
      <maven.compiler.target>16</maven.compiler.target>
      <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
   </properties>
   
   <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
   <dependencies>
      <dependency>
         <groupId>org.seleniumhq.selenium</groupId>
         <artifactId>selenium-java</artifactId>
         <version>4.11.0</version>
      </dependency>
   </dependencies>
</project>

输出

Getting the page title: Selenium Practice - Student Registration Form
Value entered: Selenium
Getting the current title after click: Selenium Practice - Login

Process finished with exit code 0

在上面的示例中,我们以无头模式在浏览器上启动了一个 URL,并获得了浏览器页面标题,并在控制台中显示了消息 - **获取页面标题:Selenium 实践 - 学生注册表单**。然后,我们在输入框中输入文本 Selenium 并检索输入的值,并在控制台中显示消息 - **输入的值:Selenium**。之后,我们单击“登录”链接并在导航到下一页后获取当前页面标题,并在控制台中显示消息:**单击后获取当前标题:Selenium - Selenium 实践 - 登录**。

示例 2

让我们以以下页面为例,我们将在 Chrome 浏览器中以无头模式启动一个应用程序并打开一个 URL,并获取其页面标题 - **Selenium 实践 - 日期选择器** 这将通过使用 ChromeOptions 类来完成。然后将值 --headless=new 传递给 addArguments 方法。

Selenium Headless Execution 3

代码实现

package org.example;

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

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

      // setting Chrome options to browser in headless mode
      ChromeOptions opt = new ChromeOptions();
      opt.addArguments("--headless=new");

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

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

      // Opening the webpage
      driver.get("https://tutorialspoint.com/selenium/practice/date-picker.php");

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

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

输出

Getting the page title: Selenium Practice - Date Picker

在上面的示例中,我们以无头模式在浏览器上启动了一个 URL,并获得了浏览器页面标题,并在控制台中显示了消息 - **获取页面标题:Selenium 实践 - 日期选择器**。

结论

这总结了我们关于 Selenium Webdriver 无头执行教程的全面概述。我们从描述什么是无头执行开始,并逐步介绍了如何使用 Selenium Webdriver 处理无头执行的示例。这使您能够深入了解 Selenium Webdriver 无头执行。明智的做法是不断练习您学到的知识,并探索与 Selenium 相关的其他内容,以加深您的理解并扩展您的视野。

广告

© . All rights reserved.