第一次接觸Selenium 的WebDriver,是在一個Web項目中。該項目使用它來進行功能性測試。當我看到Firefox中的頁面內容被一個個自動填充並且自動跳轉的時候,感覺真的很神奇。通過這段時間的學習覺得可以將我學的關於WebDriver的知識進行一個總結。
什麼是Selenium 和WebDriver?
Selenium是一個瀏覽器自動化操作框架。Selenium主要由三種工具組成。第一個工具SeleniumIDE,是Firefox的擴展插件,支持用戶錄製和回訪測試。錄製/回訪模式存在侷限性,對許多用戶來說並不適合,因此第二個工具——Selenium WebDriver提供了各種語言環境的API來支持更多控制權和編寫符合標準軟件開發實踐的應用程序。最後一個工具——SeleniumGrid幫助工程師使用Selenium API控制分佈在一系列機器上的瀏覽器實例,支持併發運行更多測試。在項目內部,它們分別被稱爲“IDE”、“WebDriver”和“Grid”。
這裏主要介紹它的第二個工具:WebDriver。
官網上是這麼介紹它的:WebDriver is a clean, fast framework for automated testing of webapps. 但是我覺得它並不侷限與進行自動化測試,完全可以用作其它用途。
WebDriver針對各個瀏覽器而開發,取代了嵌入到被測Web應用中的JavaScript。與瀏覽器的緊密集成支持創建更高級的測試,避免了JavaScript安全模型導致的限制。除了來自瀏覽器廠商的支持,WebDriver還利用操作系統級的調用模擬用戶輸入。WebDriver支持Firefox(FirefoxDriver)、IE (InternetExplorerDriver)、Opera (OperaDriver)和Chrome (ChromeDriver)。 它還支持Android (AndroidDriver)和iPhone (IPhoneDriver)的移動應用測試。它還包括一個基於HtmlUnit的無界面實現,稱爲HtmlUnitDriver。WebDriver API可以通過Python、Ruby、Java和C#訪問,支持開發人員使用他們偏愛的編程語言來創建測試。
如何使用?
首先,你需要將WebDriver的JAR包加入到你項目中CLASSPATH中。你可以Download它通過http://code.google.com/p/selenium/downloads/list。
如果你使用的是maven構建你的項目,只需要在pom.xml文件中加入下面的依賴項即可。
<dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-java</artifactId> <version>2.25.0</version> </dependency> <dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-server</artifactId> <version>2.25.0</version> </dependency>
然後,你就可以使用它了。WebDriver的API遵從”Best Fit”原則,在保持良好的用戶體驗性和靈活性之間找到一個最佳的平衡點。
下面的例子是使用HtmlUnitDriver。HtmlUnitDriver只會在內存中執行這段代碼,不會彈出一個真實的頁面。
packageorg.openqa.selenium.example; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.htmlunit.HtmlUnitDriver; public class Example { public static void main(String[] args) { // Create a new instance of the html unit driver // Notice that the remainder of the code relies onthe interface, // not the implementation. WebDriver driver = new HtmlUnitDriver(); // And now use this to visit Google driver.get("http://www.google.com"); // Find the text input element by its name WebElement element = driver.findElement(By.name("q")); // Enter something to search for element.sendKeys("Cheese!"); // Now submit the form. WebDriver will find theform for us from the element element.submit(); // Check the title of the page System.out.println("Page title is: " +driver.getTitle()); } }
如果你想使用Firefox瀏覽器。你只需要將WebDriver driver = new FirefoxDriver()。前提是你的Firefox被安裝在默認的位置。
操作系統 | Firefox默認安裝位置 |
---|---|
Linux | firefox (found using “which”) |
Mac | /Applications/Firefox.app/Contents/MacOS/firefox |
Windows | %PROGRAMFILES%\Mozilla Firefox\firefox.exe |
如果你的FireFox沒有被安裝在指定的位置,你可以設置“webdriver.firefox.bin”
環境變量的值來指定它的位置。在Java中可以使用如下代碼:
System.setProperty("webdriver.firefox.bin","thelocation of Firefox");
如果要使用Chrome瀏覽器的話相對麻煩些。你需要首先下載一個ChromeDriver(下載地址:http://code.google.com/p/chromedriver/downloads/list)。這個程序是由Chrome團隊提供的,你可以看做它是鏈接WebDriver和Chrome瀏覽器的橋樑。然後啓動ChromeDriver,你會得到一個Url及監聽端口。然後使用webDriver = newRemoteWebDriver(url, DesiredCapabilities.chrome())創建一個ChromeWebDriver進行操作。當然你可以在一個子線程中啓動ChromeDriver,並設置給WebDriver。
File file = new File(your chromedriverfile path); ChromeDriverService service = newChromeDriverService.Builder().usingChromeDriverExecutable(file).usingAnyFreePort().build(); service.start(); WebDriver webDriver = new ChromeDriver(service); …… …… …… service.stop();
WebDriver如何工作
WebDriver是W3C的一個標準,由Selenium主持。
具體的協議標準可以從http://code.google.com/p/selenium/wiki/JsonWireProtocol#Command_Reference查看。
從這個協議中我們可以看到,WebDriver之所以能夠實現與瀏覽器進行交互,是因爲瀏覽器實現了這些協議。這個協議是使用JOSN通過HTTP進行傳輸。
它的實現使用了經典的Client-Server模式。客戶端發送一個requset,服務器端返回一個response。
我們明確幾個概念。
Client
調用 WebDriverAPI的機器。
Server
運行瀏覽器的機器。Firefox瀏覽器直接實現了WebDriver的通訊協議,而Chrome和IE則是通過ChromeDriver和InternetExplorerDriver實現的。
Session
服務器端需要維護瀏覽器的Session,從客戶端發過來的請求頭中包含了Session信息,服務器端將會執行對應的瀏覽器頁面。
WebElement
這是WebDriverAPI中的對象,代表頁面上的一個DOM元素。
舉個實際的例子,下面代碼的作用是”命令”firefox轉跳到google主頁:
WebDriver driver = new FirefoxDriver(); //實例化一個Driver driver.get("http://www.google.com");
在執行driver.get("http://www.google.com")
這句代碼時,client,也就是我們的測試代碼向remote
server
發送瞭如下的請求:
POSTsession/285b12e4-2b8a-4fe6-90e1-c35cba245956/url
post_data{"url":"http://google.com"}
通過post的方式請求localhost:port/hub/session/session_id/url地址,請求瀏覽器完成跳轉url的操作。
如果上述請求是可接受的,或者說remote server是實現了這個接口,那麼remote server會跳轉到該post data包含的url,並返回如下的response
{"name":"get","sessionId":"285b12e4-2b8a-4fe6-90e1-c35cba245956","status":0,"value":""}
該response中包含如下信息:
-
name:remote server端的實現的方法的名稱,這裏是get,表示跳轉到指定url;
-
sessionId:當前session的id;
-
status:請求執行的狀態碼,非0表示未正確執行,這裏是0,表示一切ok不許擔心;
-
value:請求的返回值,這裏返回值爲空,如果client調用title接口,則該值應該是當前頁面的title;
-
如果client發送的請求是定位某個特定的頁面元素,則response的返回值可能是這樣的:
{"name":"findElement","sessionId":"285b12e4-2b8a-4fe6-90e1-c35cba245956","status":0,"value":{"ELEMENT":"{2192893e-f260-44c4-bdf6-7aad3c919739}"}}
name,sessionId,status
跟上面的例子是差不多的,區別是該請求的返回值是ELEMENT:{2192893e-f260-44c4-bdf6-7aad3c919739},表示定位到元素的id,通過該id,client可以發送如click之類的請求與
server端進行交互。
這個今天就講到這裏。以後有新東西再補充。