HTMLUnitDriver 中的 Selenium

无头浏览器是一种没有图形用户界面的 Web 浏览器。它可以在后台运行而不会产生视觉干扰。它提供了一种高效且有效的方法来测试 Web 应用程序,同时节省时间和资源。在本教程中,我们将深入介绍无头浏览器是什么、何时使用无头浏览器测试以及如何使用 Selenium.

什么是无头浏览器?

无头浏览器是一种网络浏览器 没有图形用户界面。该程序的行为与浏览器一样,但不会显示任何 GUI。

无头驱动程序的一些示例包括

  • 单位
  • PhantomJS
  • 僵尸JS
  • Watir-webdriver

何时使用无头浏览器测试?

在当今的数字时代,Web 应用程序的开发是为了与各种设备和平台兼容。这通常会给网站开发人员带来挑战,他们需要确保他们的应用程序在这些平台上无缝运行。无头浏览器测试是解决此问题的理想解决方案,因为它允许开发人员在无需图形用户界面的情况下测试他们的 Web 应用程序。通过使用无头浏览器测试,开发人员可以轻松测试具有多个组件和依赖项的复杂 Web 应用程序,为更快的开发、无错误的代码和满意的用户铺平道路。

使用 Headless 浏览器进行测试 Selenium

Selenium 是一款强大的无头浏览器测试工具,允许开发人员运行自动化测试,而无需可见的用户界面。通过在后台运行测试, Selenium 可以节省时间和资源,同时还有助于识别传统基于 UI 的测试环境中可能不明显的问题。这包括与性能相关的问题和布局问题,这些问题可能只有在无头设置中才会变得明显。但是,重要的是要记住无头测试的局限性,并将其与传统基于 UI 的方法进行平衡,以确保全面的测试覆盖范围。

无头浏览器的常见示例

目前有许多无头浏览器可供选择,每种浏览器都有自己独特的功能和优势,适合不同的用例。我们在下面讨论它们:-

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 是一款流行的 Web 浏览器,也可用作无头浏览器进行测试。使用 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 管道集成

无头浏览器测试的缺点

  • 缺乏 GUI
  • 调试困难
  • 有限的浏览器支持

HTMLUnitDriver

HTML UnitDriver 是 WebDriver 最轻量、实现速度最快的无头浏览器。它基于 HtmlUnit。它被称为 无头浏览器驱动程序. 它与 Chrome、IE 或 Fire 相同Fox 驱动程序,但它没有 GUI,因此无法在屏幕上看到测试执行。

HTML单元驱动程序的特点

  • 支持 HTTPS 和 HTTP 协议
  • 支持 HTML 响应(点击链接、提交表单、遍历 HTML 文档的 DOM 模型等)
  • 支持 Cookie
  • 代理服务器支持
  • 支持基本和 NTLM 身份验证
  • JavaScript 支持
  • 支持提交方法GET和POST
  • 能够自定义发送到服务器的请求标头
  • 能够确定服务器的失败响应是否应抛出异常或应作为适当类型的页面返回

使用 HTMLUnit 驱动程序的步骤 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 HTML unit 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)运行代码。 您将发现没有启动浏览器并且结果显示在控制台中。

使用 HTMLUnit 驱动程序的步骤 Selenium

Html Unit Driver 的优点:

  • 由于它不使用任何 GUI 进行测试,因此您的测试将在后台运行,不会受到任何视觉干扰
  • 与所有其他实例相比,执行速度更快
  • 要通过 HtmlUnit 驱动程序运行测试,您还可以选择其他浏览器版本
  • 它独立于平台,更容易同时运行多个测试。非常适合 负载测试.

限制:

  • 它无法模拟其他浏览器 Java脚本行为

PhantomJS

PhantomJS 是一个无头浏览器, Java脚本 API。它是无头网站测试、访问和操作网页的最佳解决方案,并附带标准 DOM API。

为了将 PhantomJS 与 Seleniun 一起使用,必须使用 GhostDriver。 幽灵司机 是 PhantomJS 的简单 JS 中 Webdriver Wire 协议的实现。

PhatomJS 的最新版本有 集成 GhostDriver 和 无需单独安装。

该系统的工作原理如下:

PhantomJS

运行步骤 Selenium 使用 PhatomJS

步骤1) 你需要 Eclipse - Selenium 安装

步骤2) 下载 PhantomJS 点击这里

运行步骤 Selenium 使用 PhatomJS

步骤3) 将下载的文件夹解压到 Program Files 文件

运行步骤 Selenium 使用 PhatomJS

步骤4) 从以下网址下载 PhantomJS 驱动程序 点击这里. 将 jar 添加到你的项目中

运行步骤 Selenium 使用 PhatomJS

步骤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。单击“允许访问”。

运行步骤 Selenium 使用 PhatomJS

许多组织将 Phantom.JS 用于各种目的,例如,

  • 无头测试
  • 屏幕录制
  • 页面自动化
  • 网络监控
  • 为用户呈现仪表板屏幕截图
  • 在命令行上运行单元测试
  • 将员工手册从 HTML 转换为 PDF
  • 与 QUnit 结合作为测试套件

结语

为了在各种浏览器中快速测试应用程序,并且不会出现任何视觉中断,无头浏览器 测试与验证 被使用。由于其速度快、准确性高和易于访问的特点,HTML 单元驱动程序和 PhantomJS 在无头浏览器自动化测试中越来越受欢迎。通过遵循一些简单的步骤,您可以了解这些工具如何轻松地与其他工具集成并执行测试代码。