Skip to Content
使用 Java 实践 Selenium WebDriver
book

使用 Java 实践 Selenium WebDriver

by Boni Garcia
May 2025
Intermediate to advanced
422 pages
6h 1m
Chinese
O'Reilly Media, Inc.
Content preview from 使用 Java 实践 Selenium WebDriver

第 9 章 第三方集成 第三方集成

本作品已使用人工智能进行翻译。欢迎您提供反馈和意见:translation-feedback@oreilly.com

本章介绍了不同的第三方技术(如库或框架),我们可以将其与 Selenium WebDriver 结合使用。当 Selenium WebDriver API 不足以执行特定任务时,我们就需要使用这些技术。文件下载就是这种情况,我们需要使用第三方工具来等待文件正确下载,或者使用 HTTP 客户端来控制下载。我们还使用第三方代理捕获 HTTP 流量。

我们需要在 Selenium WebDriver 中使用外部工具的另一种情况是实施非功能测试,如性能、安全性、可访问性或 A/B 测试。我们还可以使用第三方库,使用流畅的 API 开发 Selenium WebDriver 测试、生成虚假测试数据或改进测试报告。最后,我们还可以集成相关框架,如用于行为驱动开发(BDD)的 Cucumber 或 Spring 框架(用于开发网络应用程序)。我们将在本章中介绍所有这些用途。

提示

要使用本章介绍的第三方实用程序,首先必须在项目中包含所需的依赖项。您可以在附录 C 中找到使用 Maven 和 Gradle 解析每个依赖项的详细信息。

文件下载

Selenium WebDriver 对文件下载的支持有限(),因为其 API 不公开下载进度。换句话说,我们可以使用 Selenium WebDriver 从网络应用程序下载文件,但无法控制将这些文件复制到本地文件系统所需的时间。因此,我们可以使用第三方库来增强 Selenium WebDriver 的网络下载体验。为此,我们有不同的选择。下面各小节将解释如何使用。

使用特定浏览器的功能

我们可以使用 浏览器特定功能(如第 5 章中所做的)来配置文件下载的几个参数,如目标文件夹。这种方法很方便,因为 Selenium WebDriver API 开箱即可提供这些功能,但它也有几个缺点。首先,它与不同类型的浏览器(Chrome、Firefox 等)不兼容。换句话说,不同浏览器所需的功能是不同的。其次,更重要的是,我们无法控制下载进度。要解决这个问题,我们需要使用第三方库。在本书中,我建议使用开源库Awaitility

Awaitility 是一种流行的库,它提供了处理异步操作的功能。这样,它就为管理线程、超时和并发问题提供了流畅的 API。在使用 Selenium WebDriver 下载文件时,我们使用 Awaitility API 等待下载的文件存储到文件系统中。例 9-1展示了一个使用 Chrome 浏览器和 Awaitility 的示例。例 9-2显示了使用 Firefox 时的相应测试设置。

例 9-1. 使用 Chrome 浏览器和 Awaitility 测试下载文件
class DownloadChromeJupiterTest {

    WebDriver driver;

    File targetFolder;

    @BeforeEach
    void setup() {
        targetFolder = new File(System.getProperty("user.home"), "Downloads"); 1
        Map<String, Object ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

现代 Java 配方

现代 Java 配方

Ken Kousen
Java Server Pages

Java Server Pages

Hans Bergsten

Publisher Resources

ISBN: 9798341657847