- Selenium 教程
- Selenium - 首页
- Selenium - 概述
- Selenium - 组件
- Selenium - 自动化测试
- Selenium - 环境搭建
- Selenium - 远程控制
- Selenium IDE 教程
- Selenium - IDE 简介
- Selenium - 特性
- Selenium - 限制
- Selenium - 安装
- Selenium - 创建测试
- Selenium - 创建脚本
- Selenium - 控制流
- Selenium - 存储变量
- Selenium - 警报和弹出窗口
- Selenium - Selenese 命令
- Selenium - Actions 命令
- Selenium - Accessors 命令
- Selenium - Assertions 命令
- Selenium - Assert/Verify 方法
- Selenium - 定位策略
- Selenium - 脚本调试
- Selenium - 验证点
- Selenium - 模式匹配
- Selenium - JSON 数据文件
- Selenium - 浏览器执行
- Selenium - 用户扩展
- Selenium - 代码导出
- Selenium - 代码输出
- Selenium - JavaScript 函数
- Selenium - 插件
- Selenium WebDriver 教程
- Selenium - 简介
- Selenium WebDriver vs RC
- Selenium - 安装
- Selenium - 第一个测试脚本
- Selenium - 驱动程序会话
- Selenium - 浏览器选项
- Selenium - Chrome 选项
- Selenium - Edge 选项
- Selenium - Firefox 选项
- Selenium - Safari 选项
- Selenium - 双击
- Selenium - 右键单击
- Python 中的 HTML 报告
- 处理编辑框
- Selenium - 单个元素
- Selenium - 多个元素
- Selenium Web 元素
- Selenium - 文件上传
- Selenium - 定位器策略
- Selenium - 相对定位器
- Selenium - 查找器
- Selenium - 查找所有链接
- Selenium - 用户交互
- Selenium - WebElement 命令
- Selenium - 浏览器交互
- Selenium - 浏览器命令
- Selenium - 浏览器导航
- Selenium - 警报和弹出窗口
- Selenium - 处理表单
- Selenium - 窗口和选项卡
- Selenium - 处理链接
- Selenium - 输入框
- Selenium - 单选按钮
- Selenium - 复选框
- Selenium - 下拉框
- Selenium - 处理 iframe
- Selenium - 处理 Cookie
- Selenium - 日期时间选择器
- Selenium - 动态 Web 表格
- Selenium - Actions 类
- Selenium - Action 类
- Selenium - 键盘事件
- Selenium - 键上/下
- Selenium - 复制和粘贴
- Selenium - 处理特殊键
- Selenium - 鼠标事件
- Selenium - 拖放
- Selenium - 笔事件
- Selenium - 滚动操作
- Selenium - 等待策略
- Selenium - 显式/隐式等待
- Selenium - 支持特性
- Selenium - 多选
- Selenium - 等待支持
- Selenium - 选择支持
- Selenium - 颜色支持
- Selenium - ThreadGuard
- Selenium - 错误和日志记录
- Selenium - 异常处理
- Selenium - 杂项
- Selenium - 处理 Ajax 调用
- Selenium - JSON 数据文件
- Selenium - CSV 数据文件
- Selenium - Excel 数据文件
- Selenium - 跨浏览器测试
- Selenium - 多浏览器测试
- Selenium - 多窗口测试
- Selenium - JavaScript 执行器
- Selenium - 无头执行
- Selenium - 捕获屏幕截图
- Selenium - 捕获视频
- Selenium - 页面对象模型
- Selenium - 页面工厂
- Selenium - 记录和回放
- Selenium - 框架
- Selenium - 浏览上下文
- Selenium - DevTools
- Selenium Grid 教程
- Selenium - 概述
- Selenium - 架构
- Selenium - 组件
- Selenium - 配置
- Selenium - 创建测试脚本
- Selenium - 测试执行
- Selenium - 端点
- Selenium - 自定义节点
- Selenium 报告工具
- Selenium - 报告工具
- Selenium - TestNG
- Selenium - JUnit
- Selenium - Allure
- Selenium & 其他技术
- Selenium - Java 教程
- Selenium - Python 教程
- Selenium - C# 教程
- Selenium - Javascript 教程
- Selenium - Kotlin 教程
- Selenium - Ruby 教程
- Selenium - Maven 和 Jenkins
- Selenium - 数据库测试
- Selenium - LogExpert 日志记录
- Selenium - Log4j 日志记录
- Selenium - Robot Framework
- Selenium - AutoIT
- Selenium - Flash 测试
- Selenium - Apache Ant
- Selenium - Github 教程
- Selenium - SoapUI
- Selenium - Cucumber
- Selenium - IntelliJ
- Selenium - XPath
Selenium WebDriver - 无头执行
Selenium Webdriver 可用于在任何浏览器上无头执行测试。无头执行是在浏览器不可见的情况下执行测试。
什么是无头执行?
现在更倾向于无头执行,因为测试用例的执行速度更快,因为在此过程中不会呈现网页。此外,在无头模式下执行测试时,所需的系统资源更少。在运行测试以从 Web 应用程序中抓取数据时,大多数情况下都首选无头执行。
此外,如果我们触发涉及多个浏览器的并行执行,如果我们进行无头执行,则资源利用率将受到限制。
但是,我们无法对测试进行实时调试,也无法在测试失败时使用无头执行模式向开发人员报告问题。
示例 1
让我们以以下页面为例,我们将在 Chrome 浏览器中以无头模式启动具有 URL 的应用程序,Selenium 版本高于 4。可以使用 Selenium Webdriver 中的 ChromeOptions 类实现无头执行。
然后,获取其页面标题:**Selenium 实践 - 学生注册表单**。接下来,我们将在“姓名”标签旁边的输入框中输入文本**Selenium**。
然后,我们将单击“登录”按钮,之后我们将导航到另一个页面,该页面的浏览器标题为**Selenium 实践 - 登录**。
代码实现
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 方法。
代码实现
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 相关的其他内容,以加深您的理解并扩展您的视野。
