当前位置: 首页 > 文档资料 > 开源软件架构 >

第16章 Selenium WebDriver

优质
小牛编辑
126浏览
2023-12-01

Selenium is a browser automation tool, commonly used for writing end-to-end tests of web applications. A browser automation tool does exactly what you would expect: automate the control of a browser so that repetitive tasks can be automated. It sounds like a simple problem to solve, but as we will see, a lot has to happen behind the scenes to make it work.

selenium是一个浏览器自动化测试工具,他被经常用于编写端到端的web应用程序测试脚本。就像你对“自动化”这词的期待一样:自动的控制浏览器以让他可以完成重复的任务。噢!这看起来是件很容易的事情,不过正如我们将会看到的, 在让他跑起来的背后,还会发生很多事情。

Before describing the architecture of Selenium it helps to understand how the various related pieces of the project fit together. At a very high level, Selenium is a suite of three tools. The first of these tools, Selenium IDE, is an extension for Firefox that allows users to record and playback tests. The record/playback paradigm can be limiting and isn't suitable for many users, so the second tool in the suite, Selenium WebDriver, provides APIs in a variety of languages to allow for more control and the application of standard software development practices. The final tool, Selenium Grid, makes it possible to use the Selenium APIs to control browser instances distributed over a grid of machines, allowing more tests to run in parallel. Within the project, they are referred to as "IDE", "WebDriver" and "Grid". This chapter explores the architecture of Selenium WebDriver.

在描述selenium的架构之前,理解这个项目的各个部分是如何组织起来的是很有帮助的。在一个非常高的抽象等级下,selenium是一个集成了三个工具的套件。第一个:seleniumIDE,他是一个firefox的扩展,允许用户记录和播放测试脚本。这个工具对很多用户来说并不合适,并且存在限制。因此第二个工具:seleniumWebDriver,提供不同语言平台的API为用户提供对测试更多的控制,允许用户构建符合标准软件开发实践的应用。最后一个工具,seleniumGrid,让SeleniumAPIS来控制分布在不同机器的浏览器实例,允许并行测试。在这个项目中,他们被称为IDE、WebDriver和Grid。该章节探索的是WebDriver的架构。

This chapter was written during the betas of Selenium 2.0 in late 2010. If you're reading the book after then, then things will have moved forward, and you'll be able to see how the architectural choices described here have unfolded. If you're reading before that date: Congratulations! You have a time machine. Can I have some winning lottery numbers?

该章写于selenium2.0处于beta测试的时期。如果你在这之后读这本书,那么你可能会发现selenium的架构已经演进,并且你能够看到在本章中描述的关于架构的选择已经被展开。如果你在这个日期之前读这本书:恭喜你!你是有时间机器的人。你能给我一些彩票的中奖号码么?

16.1. History Jason Huggins started the Selenium project in 2004 while working at ThoughtWorks on their in-house Time and Expenses (T&E) system, which made extensive use of Javascript. Although Internet Explorer was the dominant browser at the time, ThoughtWorks used a number of alternative browsers (in particular Mozilla variants) and would file bug reports when the T&E app wouldn't work on their browser of choice. Open Source testing tools at the time were either focused on a single browser (typically IE) or were simulations of a browser (like HttpUnit). The cost of a license for a commercial tool would have exhausted the limited budget for a small in-house project, so they weren't even considered as viable testing choices.

Jason Huggins 在thoughtworks工作期间于2004年开始selenium项目。尽管IE当时还是主流浏览器,thoughtworks还是会使用一些其他的浏览器(尤其是mozilla),并且在当这些浏览器不能使用T&E时,会提交Bug报告。开源测试工具在当时要么专注于单个浏览器(比如IE),或者仅仅是浏览器的仿真(比如HttpUnit)。商业工具的许可证对一些小的项目来说成本实在太高了,因此在当时并没有多少可用的测试工具。

Where automation is difficult, it's common to rely on manual testing. This approach doesn't scale when the team is very small or when releases are extremely frequent. It's also a waste of humanity to ask people to step through a script that could be automated. More prosaically, people are slower and more error prone than a machine for dull repetitive tasks. Manual testing wasn't an option.

由于当时进行自动化测试的困难,大部分都依赖于手动测试。这个方法在团队很小或者发布流程极度频繁的时候几乎没什么可扩展性。另外,要求人员测试本来就可以被自动化测试的脚本也是一种人力的浪费。更直接的说,人们在进行无聊的重复性工作时,效率低下、更易犯错。所以,手动测试并不是一项好的选择。

Fortunately, all the browsers being tested supported Javascript. It made sense to Jason and the team he was working with to write a testing tool in that language which could be used to verify the behavior of the application. Inspired by work being done on FIT1, a table-based syntax was placed over the raw Javascript and this allowed tests to be written by people with limited programming experience using a keyword-driven approach in HTML files. This tool, originally called "Selenium" but later referred to as "Selenium Core", was released under the Apache 2 license in 2004.

幸运的是,所有有待进行测试的浏览器都支持javascript。那么Jason和他的团队使用JS来开发一个测试工具就显得非常合理。被FIT1(一个基于HTML的表语法被放置在原始的js上,并且允许人们在只有有限的编程经验的时候测试工具)。这个工具,开始叫做“selemium”后来被更名为“seleniumCore”,在2004年用Apache2许可证发布。