Selenium 支持无界面浏览器测试吗?
是的,Selenium 支持无界面浏览器测试。借助 HTMLUnitDriver 即可实现此功能。它是所有浏览器驱动程序中速度最快的,并且与平台无关。
在 Selenium 2.53 版本之后,必须在项目中明确添加 HTMLUnitDriver jar。要添加依赖关系,请按如下步骤操作:-
导航到链接:https://github.com/SeleniumHQ/htmlunitdriver/releases。
点击下图中突出显示的链接。

在项目上右键单击,然后选择构建路径。再单击配置构建路径。

转到Java 构建路径,然后选择库。单击添加外部 JAR。然后浏览并添加 HTMLUnitDriver jar。

我们必须将 org.openqa.selenium.htmlunit.HtmlUnitDriver 添加到项目。
示例
代码实现。
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
public class HeadlessMode{
public static void main(String[] args) {
//HtmlUnitDriver initialization
HtmlUnitDriver driver = new HtmlUnitDriver();
driver.get("https://tutorialspoint.com/questions/index.php");
// wait of 5 seconds
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
System.out.println("Page title: " + driver.getTitle());
}
}输出

此外,在执行过程中不会显示任何浏览器。
广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
JavaScript
PHP