HTMLUnitDriver 中的 Selenium无头浏览器教程
⚡ 智能摘要
HtmlUnitDriver 是速度最快的无头实现。 Selenium WebDriver,而曾经流行的基于 GhostDriver 的无头浏览器 PhantomJS,自 2018 年起已被弃用,现在已被无头 Chrome 和无头浏览器所取代。 Firefox以及适用于现代自动化工作流程的 Playwright。

无头浏览器是一种没有图形用户界面的网络浏览器。它可以后台运行,而不会造成视觉干扰。trac它提供了一种高效便捷的方式来测试 Web 应用程序,同时节省时间和资源。在本文中,我们将探讨什么是无头浏览器、何时使用无头浏览器测试以及如何使用它进行测试。 Selenium HtmlUnitDriver 和 PhantomJS。
什么是无头浏览器?
无头浏览器是一种网络浏览器。 没有图形用户界面该程序运行方式与普通浏览器类似,但在运行时不会显示任何图形用户界面窗口。
无头浏览器驱动程序的一些示例包括:
- 单位
- 鬼
- PhantomJS(已弃用)
- 僵尸JS
- Watir-webdriver
- 无头铬
- 无头 Firefox
何时使用无头浏览器测试?
在当今的数字时代,Web 应用程序的开发目标是兼容各种设备和平台。这给开发人员带来了挑战,他们必须确保应用程序在不同环境下无缝运行。无头浏览器测试是一种理想的解决方案,因为它允许开发人员在无需图形用户界面的情况下验证其 Web 应用程序。通过使用无头浏览器测试,开发人员可以快速覆盖包含众多组件和依赖项的复杂 Web 应用程序,从而加快开发速度、减少错误并提升用户体验。
使用 Headless 浏览器进行测试 Selenium
Selenium 是一个功能强大的无头浏览器测试工具,它允许开发人员在没有可见用户界面的情况下运行自动化测试。通过在后台运行测试, Selenium 节省时间和资源,同时也有助于ping 识别在传统的基于 UI 的测试环境中可能不明显的问题。这包括性能和布局问题,这些问题只有在无头环境下才会显现。然而,为了确保测试的全面覆盖,平衡无头测试和传统的基于 UI 的测试至关重要。
无头浏览器的常见示例
市面上有许多无头浏览器可供选择,它们各自具有不同的特性和优势,适用于不同的使用场景。我们将在下文中进行探讨。
PhantomJS
弃用通知: PhantomJS 的积极开发已于 2018 年停止,该项目不再维护。新项目应优先选择无头 Chrome 或无头 Chrome。 Firefox 或 剧作家 适用于现代无头自动化。以下 PhantomJS 示例仅供参考。
PhantomJS 是一款无头浏览器,它使用 WebKit 作为渲染引擎,并支持 HTML5、CSS3 等 Web 标准。 Java脚本。它可用于屏幕截图和页面自动化任务。它是开源的,并且兼容多种操作系统。
示例 Selenium 使用 Headless PhantomJS Python:
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
# Set up PhantomJS options
phantomjs_options = webdriver.DesiredCapabilities.PHANTOMJS.copy()
phantomjs_options['phantomjs.page.settings.userAgent'] = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'
# Set up the PhantomJS driver
driver = webdriver.PhantomJS('/path/to/phantomjs', desired_capabilities=phantomjs_options)
# Perform actions using the driver
driver.get('https://www.example.com')
print(driver.title)
# Close the driver
driver.quit()
铬系列
Chrome 是全球最流行的浏览器,并且提供无头模式。它可在多个平台上使用,并支持流行的编程语言和框架。其内置的调试工具和丰富的文档使其易于使用,并能轻松解决测试过程中可能出现的任何问题。
Headless Chrome 示例 Selenium in Python:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
# Set up Chrome options
chrome_options = Options()
chrome_options.add_argument('--headless') # Run Chrome in headless mode
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-dev-shm-usage')
# Set up the Chrome driver
driver = webdriver.Chrome('/path/to/chromedriver', options=chrome_options)
# Perform actions using the driver
driver.get('https://www.example.com')
print(driver.title)
# Close the driver
driver.quit()
Firefox
Firefox 是一款流行的网络浏览器,也可以用作无头浏览器进行测试。使用这款浏览器的一个好处是…… Firefox 作为一款无头浏览器,其轻量级的体积使其成为在不同操作系统上进行测试的理想选择。再加上其丰富的文档和社区支持, Firefox 对于正在尝试无头浏览器自动化的团队来说,这是一个绝佳的选择。
无头示例 Firefox - Selenium in Python:
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
# Set up Firefox options
firefox_options = Options()
firefox_options.add_argument('--headless') # Run Firefox in headless mode
# Set up the Firefox driver
driver = webdriver.Firefox(options=firefox_options)
# Perform actions using the driver
driver.get('https://www.example.com')
print(driver.title)
# Close the driver
driver.quit()
无头浏览器测试的优点
- 更快的测试执行
- 经济高效的测试
- 更好的测试覆盖率
- 运行测试的灵活性
- 更容易与 CI/CD 流水线集成
无头浏览器测试的缺点
- 缺少用于手动检查的可见图形用户界面
- 如果没有屏幕,调试会更困难。
- 浏览器特定行为覆盖范围有限
HtmlUnitDriver
HtmlUnitDriver 是目前最轻量级、速度最快的无头浏览器实现。 Selenium WebDriver。它基于HtmlUnit,也被称为…… 无头浏览器驱动程序它的行为类似于 Chrome、IE 或 Firefox 虽然有驱动程序,但它没有图形用户界面 (GUI),因此测试执行过程不会显示在屏幕上。HtmlUnitDriver 仍在维护中,并通过以下方式分发: org.seleniumhq.selenium:htmlunit3-driver Maven 工件。
HtmlUnitDriver 的功能特性:
- 支持 HTTPS 和 HTTP 协议
- 支持HTML响应(点击链接、提交表单、遍历HTML文档的DOM模型等)
- 支持 Cookie
- 代理服务器支持
- 支持基本和 NTLM 身份验证
- 固体 JavaScript 支持
- 支持 GET 和 POST 提交方法
- 能够自定义发送到服务器的请求标头
- 能够控制服务器返回失败响应时是抛出异常还是返回相应类型的页面。
使用 HtmlUnitDriver 的步骤 Selenium
步骤1) In Eclipse复制以下代码。添加标准 Selenium 将库文件添加到项目中。无需额外的 JAR 文件。
package htmldriver;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
public class htmlUnitYest {
public static void main(String[] args) {
// Creating a new instance of the HtmlUnit driver
WebDriver driver = new HtmlUnitDriver();
// Navigate to Google
driver.get("http://www.google.com");
// Locate the searchbox using its name
WebElement element = driver.findElement(By.name("q"));
// Enter a search query
element.sendKeys("Guru99");
// Submit the query. WebDriver searches for the form using the text input element automatically
// No need to locate/find the submit button
element.submit();
// This code will print the page title
System.out.println("Page title is: " + driver.getTitle());
driver.quit();
}
}
步骤2) 运行代码。 你会发现没有浏览器启动,结果显示在控制台中。
HtmlUnitDriver 的优势
- 由于它不使用任何图形用户界面,测试在后台运行,不会造成视觉干扰。
- 与其他驱动程序实例相比,执行速度更快
- 您还可以通过 HtmlUnitDriver 在运行测试时模拟不同的浏览器版本。
-
它与平台无关,并且可以轻松地同时运行多个测试。它非常适合…… 负载测试.
限制:
- 它无法完全模拟 Java每个真实浏览器引擎的脚本行为
PhantomJS
提醒: PhantomJS 已弃用且不再维护。对于新的无头项目,请使用 Selenium 使用无头 Chrome 或 Firefox或者转到剧作家版块。以下部分仅供历史参考。
PhantomJS 是一个无头浏览器,它具有 Java脚本 API。它过去用于无头网站测试、访问和操作网页,并且与标准 DOM API 一起提供。
将 PhantomJS 与 Selenium你需要 GhostDriver。 幽灵司机 是 WebDriver Wire 协议的纯文本实现。 JavaPhantomJS脚本。
PhantomJS 的后续版本 集成 GhostDriver,所以 无需单独安装。
系统运作方式如下:
运行步骤 Selenium 使用 PhantomJS
步骤1) 你需要 Eclipse - Selenium 安装。
步骤2) 下载 PhantomJS 开始.
步骤3) Extrac将下载的文件夹放入 Program Files 文件夹。
步骤4) 从以下网址下载 PhantomJS 驱动程序 开始将该 JAR 文件添加到您的项目中。
步骤5) 将以下代码粘贴到 Eclipse.
package htmldriver;
import java.io.File;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.phantomjs.PhantomJSDriver;
public class phantom {
public static void main(String[] args) {
File file = new File("C:/Program Files/phantomjs-2.0.0-windows/bin/phantomjs.exe");
System.setProperty("phantomjs.binary.path", file.getAbsolutePath());
WebDriver driver = new PhantomJSDriver();
driver.get("http://www.google.com");
WebElement element = driver.findElement(By.name("q"));
element.sendKeys("Guru99");
element.submit();
System.out.println("Page title is: " + driver.getTitle());
driver.quit();
}
}
步骤6) 运行代码。您将在控制台中看到输出,但不会启动任何浏览器。
注意: 首次运行时,根据您的设置,您可能会看到来自安全警告的信息。 Windows 允许 PhantomJS 运行。点击 允许访问.
许多组织历来都使用 PhantomJS 来执行以下任务:
- 无头测试
- 屏幕截图
- 页面自动化
- 网络监控
- 为用户渲染仪表盘截图
- 在命令行运行单元测试
- 将员工手册从 HTML 生成为 PDF
- 结合 QUnit 进行测试套件开发
对于新的工作,请将这些 PhantomJS 用例替换为无头 Chrome、无头 Chrome 等。 Firefox或者 Playwright,它们会持续获得安全性和渲染方面的更新。






