Selenium3筆記-WebDriver源碼初探

來源:互聯網
上載者:User

標籤:

Selenium3 有哪些變化?                                                     

其實相對於與Selenium2,Selenium3沒有做太多的改動。下面給出官方的文檔說明,供參考。

參考文檔:https://seleniumhq.wordpress.com/2013/08/28/the-road-to-selenium-3/

  1. “We aim for Selenium 3 to be “a tool for user-focused automation of mobile and web apps”,Developers from projects such as Appium, ios-driver and selendroidwill be working on the suite of tests to enable this.”
  2. “Selenium 3 will see the removal of the original Selenium Core implementations, and consequently we’ll be deprecating the RC APIs too,the original implementation will be available as a download, but it will no longer be actively developed once we release 3.0.”

所以對於Selenium3來說最大的變動可能就是更加專註於手機和web的測試,尤其是手機的支援,因為你曉得的,現在更多的是移動的時代。

對於Selenium2中對於RemotControl的實現我看了下Selenium3的源碼發現確實不在支援,而更多的轉向了W3C standard,不是獨成一套Selenium自己的WebDriver API.關於這個需要插如一下有關W3C WebDriver的知識。

有關W3C WebDriver                                                                      

參考文檔: https://www.w3.org/TR/webdriver/,https://www.w3.org/testing/Activity,https://github.com/w3c/webdriver

W3C組織制定了一套瀏覽器自動化的規範叫做WebDriver,這套規範規定了所有的瀏覽器生產商都必須遵守這個規範。其實定義了好多的遵循的介面和WebDriver的概念。對於Chrome,Firefox,Opera,Safari.etc他們都需要遵守這個規範並且實現規範裡面的介面,這些實現一般都是伴隨瀏覽器的開發進行的。

所以你應該明白了,Selenium不管是WebDriver還是RemoteWebDriver都是W3C WebDriver的一種實現而已。真正的核心瀏覽器的互動在對應的瀏覽器的WebDriver上,其實你有了對應的瀏覽器的WebDriver,參考W3C的標準介面文檔HTTP-based wire protocol你就可以單獨實現瀏覽器的操作。就是Client-Server的溝通。所有支援的命令列表如下:

 

舉個ChromeDriver的例子。。。

  • 首先我們找到ChromeDriver ,這個自然到chromium項目上去下載就好了。

https://sites.google.com/a/chromium.org/chromedriver/這裡也有很多詳細的介面的說明,這裡的介面說明跟上面的W3C的介面說明差不多。你需要針對不同的瀏覽器下載對應的版本。下面我以下載的一個win版本的為例(:http://chromedriver.storage.googleapis.com/2.23/chromedriver_win32.zip )

WebDriver的使用                                                                  

1.1 查看下chromedriver.exe提供給我們的一些可用的命令。

 

裡面的使用很詳細,這裡我們只需要使用一個參數來啟動ChromeDriver的server, –port ,命令如下:chromedriver.exe –port 9514,或者直接不輸入連接埠直接斷行符號,介面命令如下:

 

啟動後chromedriver會在本地的9514連接埠號碼上進行監聽通訊,根據不同的命令發送到瀏覽器上,瀏覽器進行互動。比如啟動一個chrome瀏覽器對應的命令是session,單獨的ChromeDriver的HTTP通訊URI是:http://localhost:9514/session,對於通過RemoteWebDriver的URL是:http://localhost:9514/wd/hub/session

WebDriver -New Session                                                                                         

看一下這個說明: https://www.w3.org/TR/webdriver/#dfn-new-session,操作流程如下:

The remote end steps are:

  1. If the remote end is an intermediary node, take implementation-defined steps that either result in returning an error with error code session not created, or in returning a success with data that is isomorphic to that returned by remote ends according to the rest of this algorithm.

  2. If the maximum active sessions is equal to the length of the list of active sessions, return error with error code session not created.

  3. If there is a current user prompt, return error with error code session not created.

  4. Let capabilities be the result of getting a property named "capabilities" from the parameters argument.

  5. Let capabilities result be the result of processing capabilities with capabilities as an argument.

  6. If capabilities result is an error, return error with error code session not created.

  7. Let capabilities be capabilities result’s data.

  8. Let session id be the result of generating a UUID.

  9. Let session be a new session with the session ID of session id.

  10. Set the current session to session.

  11. Append session to active sessions.

上面的流程已經在最新的Selenium WebDriver中實現了。所有啟動一個瀏覽器做的session操作可以參考如下Selenium代碼邏輯。

上面說明下WebDriver的通訊是HTTP的協議,因此這裡所有的通訊都是通過JSON Wired進行溝通的RESTFul格式。也就是說所有的溝通都是一次RESTFul的request和response的過程。

參考如下Selenium的說明: https://github.com/SeleniumHQ/selenium/wiki/JsonWireProtocol#command-summary

Selenium3筆記-WebDriver源碼初探

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.