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是一個(gè)瀏覽器自動(dòng)化測(cè)試工具,他被經(jīng)常用于編寫端到端的web應(yīng)用程序測(cè)試腳本。就像你對(duì)“自動(dòng)化”這詞的期待一樣:自動(dòng)的控制瀏覽器以讓他可以完成重復(fù)的任務(wù)。噢!這看起來是件很容易的事情,不過正如我們將會(huì)看到的, 在讓他跑起來的背后,還會(huì)發(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的架構(gòu)之前,理解這個(gè)項(xiàng)目的各個(gè)部分是如何組織起來的是很有幫助的。在一個(gè)非常高的抽象等級(jí)下,selenium是一個(gè)集成了三個(gè)工具的套件。第一個(gè):seleniumIDE,他是一個(gè)firefox的擴(kuò)展,允許用戶記錄和播放測(cè)試腳本。這個(gè)工具對(duì)很多用戶來說并不合適,并且存在限制。因此第二個(gè)工具:seleniumWebDriver,提供不同語言平臺(tái)的API為用戶提供對(duì)測(cè)試更多的控制,允許用戶構(gòu)建符合標(biāo)準(zhǔn)軟件開發(fā)實(shí)踐的應(yīng)用。最后一個(gè)工具,seleniumGrid,讓SeleniumAPIS來控制分布在不同機(jī)器的瀏覽器實(shí)例,允許并行測(cè)試。在這個(gè)項(xiàng)目中,他們被稱為IDE、WebDriver和Grid。該章節(jié)探索的是WebDriver的架構(gòu)。
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測(cè)試的時(shí)期。如果你在這之后讀這本書,那么你可能會(huì)發(fā)現(xiàn)selenium的架構(gòu)已經(jīng)演進(jìn),并且你能夠看到在本章中描述的關(guān)于架構(gòu)的選擇已經(jīng)被展開。如果你在這個(gè)日期之前讀這本書:恭喜你!你是有時(shí)間機(jī)器的人。你能給我一些彩票的中獎(jiǎng)號(hào)碼么?
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項(xiàng)目。盡管IE當(dāng)時(shí)還是主流瀏覽器,thoughtworks還是會(huì)使用一些其他的瀏覽器(尤其是mozilla),并且在當(dāng)這些瀏覽器不能使用T&E時(shí),會(huì)提交Bug報(bào)告。開源測(cè)試工具在當(dāng)時(shí)要么專注于單個(gè)瀏覽器(比如IE),或者僅僅是瀏覽器的仿真(比如HttpUnit)。商業(yè)工具的許可證對(duì)一些小的項(xiàng)目來說成本實(shí)在太高了,因此在當(dāng)時(shí)并沒有多少可用的測(cè)試工具。
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.
由于當(dāng)時(shí)進(jìn)行自動(dòng)化測(cè)試的困難,大部分都依賴于手動(dòng)測(cè)試。這個(gè)方法在團(tuán)隊(duì)很小或者發(fā)布流程極度頻繁的時(shí)候幾乎沒什么可擴(kuò)展性。另外,要求人員測(cè)試本來就可以被自動(dòng)化測(cè)試的腳本也是一種人力的浪費(fèi)。更直接的說,人們?cè)谶M(jìn)行無聊的重復(fù)性工作時(shí),效率低下、更易犯錯(cuò)。所以,手動(dòng)測(cè)試并不是一項(xiàng)好的選擇。
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.
幸運(yùn)的是,所有有待進(jìn)行測(cè)試的瀏覽器都支持javascript。那么Jason和他的團(tuán)隊(duì)使用JS來開發(fā)一個(gè)測(cè)試工具就顯得非常合理。被FIT1(一個(gè)基于HTML的表語法被放置在原始的js上,并且允許人們?cè)谥挥杏邢薜木幊探?jīng)驗(yàn)的時(shí)候測(cè)試工具)。這個(gè)工具,開始叫做“selemium”后來被更名為“seleniumCore”,在2004年用Apache2許可證發(fā)布。
更多建議: