卷1:第16章 Selenium WebDriver

2018-02-24 15:55 更新

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應用程序測試腳本。就像你對“自動化”這詞的期待一樣:自動的控制瀏覽器以讓他可以完成重復的任務。噢!這看起來是件很容易的事情,不過正如我們將會看到的, 在讓他跑起來的背后,還會發(fā)生很多事情。

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為用戶提供對測試更多的控制,允許用戶構建符合標準軟件開發(fā)實踐的應用。最后一個工具,seleniumGrid,讓SeleniumAPIS來控制分布在不同機器的瀏覽器實例,允許并行測試。在這個項目中,他們被稱為IDE、WebDriver和Grid。該章節(jié)探索的是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測試的時期。如果你在這之后讀這本書,那么你可能會發(fā)現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)。商業(yè)工具的許可證對一些小的項目來說成本實在太高了,因此在當時并沒有多少可用的測試工具。

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.

由于當時進行自動化測試的困難,大部分都依賴于手動測試。這個方法在團隊很小或者發(fā)布流程極度頻繁的時候幾乎沒什么可擴展性。另外,要求人員測試本來就可以被自動化測試的腳本也是一種人力的浪費。更直接的說,人們在進行無聊的重復性工作時,效率低下、更易犯錯。所以,手動測試并不是一項好的選擇。

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來開發(fā)一個測試工具就顯得非常合理。被FIT1(一個基于HTML的表語法被放置在原始的js上,并且允許人們在只有有限的編程經驗的時候測試工具)。這個工具,開始叫做“selemium”后來被更名為“seleniumCore”,在2004年用Apache2許可證發(fā)布。

以上內容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號