Selenium1 Selenium2 WebDriver
內(nèi)容摘要:
- WebDriver 組件
- WebDriver 協(xié)議
- Remote End 處理流程
- Commands & Endpoints & 請求路由
- 錯(cuò)誤消息
- WebDriver?配置
?
?
1、Selenium 1 原理
?
?
?
(1).測試用例(Testcase)通過Client Lib的接口向Selenium Server發(fā)送Http請求,要求和Selenium Server建立連接。
為什么要通過發(fā)送Http請求控制Selenium Server而不采用其他方式呢?從上文可以看出,Selenium Server是一個(gè)獨(dú)立的中間服務(wù)器(確切地說是代理服務(wù)器),它可以架設(shè)在其他機(jī)器上!所以測試案例通過發(fā)送HTTP請求去控制Selenium Server是很正常的。
(2).Selenium Server的Launcher啟動(dòng)瀏覽器,把Selenium Core加載入瀏覽器頁面當(dāng)中,并把瀏覽器的代理設(shè)置為Selenium Server的Http Proxy。
(3).測試案例通過Client Lib的接口向Selenium Server發(fā)送Http請求,Selenium Server對請求進(jìn)行解析,然后通過Http Proxy發(fā)送JS命令通知Selenium Core執(zhí)行操作瀏覽器的動(dòng)作。
(4).Selenium Core接收到指令后,執(zhí)行操作。
(5).瀏覽器收到新的頁面請求信息(因?yàn)樵?4)中,Selenium Core的操作可能引發(fā)新的頁面請求),于是發(fā)送Http請求,請求新的Web頁面。
由于Selenium Server在啟動(dòng)瀏覽器時(shí)做了手腳,所以Selenium Server會(huì)接收到所有由它啟動(dòng)的瀏覽器發(fā)送的請求。
(6).Selenium Server接收到瀏覽器的發(fā)送的Http請求后,自己重組Http請求,獲取對應(yīng)的Web頁面。
(7).Selenium Server的Http Proxy把接收的Web頁面返回給瀏覽器。
?
2、WebDriver
WebDriver 提供了一堆用于查找和維護(hù)頁面元素(DOM元素)的接口,以此方式來控制瀏覽器行為。目前已納入W3C管理。
https://www.w3.org/TR/webdriver/
?
2.1 Webdriver 組件
在WebDriver協(xié)議里,包括local,remote,Intermidary,endpoint等概念:
·Local End:代表了WebDriver通信的客戶端,通常情況下,是由一些特定語言基于通信協(xié)議組成的一套Client API。
·Remote End:remote是在通信協(xié)議的服務(wù)端。用于處理Client請求并作出響應(yīng)。它包括兩大類:
????Intermidary node:intermidary 通常是代理,它實(shí)現(xiàn)了local,并代理了remote。
Endpoint :endpoint 才是實(shí)際上的remote 端的處理的地方。和Java Web開發(fā)中的action、Webservice中的endpoint是類似的概念。
?
?
?
?
Webdriver 的通信協(xié)議是基于HTTP的以命令(command)方式組織的各種請求。WebDriver在處理一個(gè)命令時(shí),可能會(huì)使browser執(zhí)行一系列的操作。上面圖中,webdriver server就是remote end,WebDriver Client就是local end。
?
2.2 WebDriver 協(xié)議
?
2.2.1 Remote End 處理流程
在local end與remote end的連接建立后,Remote End會(huì)做如下處理:
1、根據(jù)HTTP協(xié)議讀取一個(gè)完整的HTTP請求,并封裝成request對象。
2、根據(jù)http request method, url對請求進(jìn)行匹配,找到相應(yīng)的endpoint。
3、如果匹配到了error ,就發(fā)送一個(gè)error給local end,然后step 1。
4、使用sessionid進(jìn)行session匹配
5、如果匹配失敗(session 列表里找不到與sessionid相關(guān)聯(lián)的session),響應(yīng)一個(gè)error,然后step 1。
6、如果是post請求,以json方式解析requet body。如果解析成功,但并不是一個(gè)對象,或者解析失敗,響應(yīng)一個(gè)error給Local。然后step 1。
7、等待瀏覽器導(dǎo)航完畢
8、響應(yīng)結(jié)果
?
2.2.2 Commands & Endpoints & 請求路由
?
為了控制browser的行為,提供了一系列的command,大體可以分為幾類:
1)Session管理(remote 與local之間的session)
2)瀏覽器URL導(dǎo)航
3)瀏覽器窗口管理
4)DOM元素管理
5)JS腳本執(zhí)行
6)Cookie管理
7)Alert
8)截屏
9)一些操作例如(鍵盤,鼠標(biāo),批處理等)
?
?
Remote在接收到這些命令時(shí),會(huì)進(jìn)行Endpoint匹配。匹配時(shí)是根據(jù)URL、http method。
他們的對應(yīng)關(guān)系是:
?
| Method | URI Template | Command |
| POST | /session | New Session |
| DELETE | /session/{session id} | Delete Session |
| GET | /status | Status |
| GET | /session/{session id}/timeouts | Get Timeouts |
| POST | /session/{session id}/timeouts | Set Timeouts |
| POST | /session/{session id}/url | Go |
| GET | /session/{session id}/url | Get Current URL |
| POST | /session/{session id}/back | Back |
| POST | /session/{session id}/forward | Forward |
| POST | /session/{session id}/refresh | Refresh |
| GET | /session/{session id}/title | Get Title |
| GET | /session/{session id}/window | Get Window Handle |
| DELETE | /session/{session id}/window | Close Window |
| POST | /session/{session id}/window | Switch To Window |
| GET | /session/{session id}/window/handles | Get Window Handles |
| POST | /session/{session id}/frame | Switch To Frame |
| POST | /session/{session id}/frame/parent | Switch To Parent Frame |
| GET | /session/{session id}/window/rect | Get Window Rect |
| POST | /session/{session id}/window/rect | Set Window Rect |
| POST | /session/{session id}/window/maximize | Maximize Window |
| POST | /session/{session id}/window/minimize | Minimize Window |
| POST | /session/{session id}/window/fullscreen | Fullscreen Window |
| GET | /session/{session id}/element/active | Get Active Element |
| POST | /session/{session id}/element | Find Element |
| POST | /session/{session id}/elements | Find Elements |
| POST | /session/{session id}/element/{element id}/element | Find Element From Element |
| POST | /session/{session id}/element/{element id}/elements | Find Elements From Element |
| GET | /session/{session id}/element/{element id}/selected | Is Element Selected |
| GET | /session/{session id}/element/{element id}/attribute/{name} | Get Element Attribute |
| GET | /session/{session id}/element/{element id}/property/{name} | Get Element Property |
| GET | /session/{session id}/element/{element id}/css/{property name} | Get Element CSS Value |
| GET | /session/{session id}/element/{element id}/text | Get Element Text |
| GET | /session/{session id}/element/{element id}/name | Get Element Tag Name |
| GET | /session/{session id}/element/{element id}/rect | Get Element Rect |
| GET | /session/{session id}/element/{element id}/enabled | Is Element Enabled |
| POST | /session/{session id}/element/{element id}/click | Element Click |
| POST | /session/{session id}/element/{element id}/clear | Element Clear |
| POST | /session/{session id}/element/{element id}/value | Element Send Keys |
| GET | /session/{session id}/source | Get Page Source |
| POST | /session/{session id}/execute/sync | Execute Script |
| POST | /session/{session id}/execute/async | Execute Async Script |
| GET | /session/{session id}/cookie | Get All Cookies |
| GET | /session/{session id}/cookie/{name} | Get Named Cookie |
| POST | /session/{session id}/cookie | Add Cookie |
| DELETE | /session/{session id}/cookie/{name} | Delete Cookie |
| DELETE | /session/{session id)/cookie | Delete All Cookies |
| POST | /session/{session id}/actions | Perform Actions |
| DELETE | /session/{session id}/actions | Release Actions |
| POST | /session/{session id}/alert/dismiss | Dismiss Alert |
| POST | /session/{session id}/alert/accept | Accept Alert |
| GET | /session/{session id}/alert/text | Get Alert Text |
| POST | /session/{session id}/alert/text | Send Alert Text |
| GET | /session/{session id}/screenshot | Take Screenshot |
| GET | /session/{session id}/element/{element id}/screenshot | Take Element Screenshot |
?
?
2.2.3 錯(cuò)誤消息
從上面的流程里,有多次提到發(fā)現(xiàn)error時(shí),回發(fā)一個(gè)error。而實(shí)際上返回的錯(cuò)誤需要包括
| { Error: // 錯(cuò)誤碼 Message://錯(cuò)誤信息 Stacktrace:// 發(fā)生錯(cuò)誤時(shí)的stack } |
?
下面是各種錯(cuò)誤的描述:
| Error Code | HTTP Status | JSON Error Code | Description |
| element click intercepted | 400 | element click intercepted | The?Element Click?command?could not be completed because the?element?receiving the events is?obscuring?the element that was requested clicked. |
| element not selectable | 400 | element not selectable | An attempt was made to select an element that cannot be selected. |
| element not interactable | 400 | element not interactable | A?command?could not be completed because the element is notpointer- or?keyboard?interactable. |
| insecure certificate | 400 | insecure certificate | Navigation?caused the user agent to hit a certificate warning, which is usually the result of an expired or invalid TLS certificate. |
| invalid argument | 400 | invalid argument | The arguments passed to a?command?are either invalid or malformed. |
| invalid cookie domain | 400 | invalid cookie domain | An illegal attempt was made to set a cookie under a different domain than the current page. |
| invalid coordinates | 400 | invalid coordinates | The coordinates provided to an interactions operation are invalid. |
| invalid element state | 400 | invalid element state | A?command?could not be completed because the element is in an invalid state, e.g. attempting to click an element that is no longer attached to the?document. |
| invalid selector | 400 | invalid selector | Argument was an invalid selector. |
| invalid session id | 404 | invalid session id | Occurs if the given?session id?is not in the list of?active sessions, meaning the?session?either does not exist or that it’s not active. |
| javascript error | 500 | javascript error | An error occurred while executing JavaScript supplied by the user. |
| move target out of bounds | 500 | move target out of bounds | The target for mouse interaction is not in the browser’s viewport and cannot be brought into that viewport. |
| no such alert | 400 | no such alert | An attempt was made to operate on a modal dialog when one was not open. |
| no such cookie | 404 | no such cookie | No cookie matching the given path name was found amongst the?associated cookies?of the?current browsing context’sactive document. |
| no such element | 404 | no such element | An element could not be located on the page using the given search parameters. |
| no such frame | 400 | no such frame | A?command?to switch to a frame could not be satisfied because the frame could not be found. |
| no such window | 400 | no such window | A?command?to switch to a window could not be satisfied because the window could not be found. |
| script timeout | 408 | script timeout | A script did not complete before its timeout expired. |
| session not created | 500 | session not created | A new?session?could not be created. |
| stale element reference | 400 | stale element reference | A?command?failed because the referenced?element?is no longer attached to the DOM. |
| timeout | 408 | timeout | An operation did not complete before its timeout expired. |
| unable to set cookie | 500 | unable to set cookie | A?command?to set a cookie’s value could not be satisfied. |
| unable to capture screen | 500 | unable to capture screen | A screen capture was made impossible. |
| unexpected alert open | 500 | unexpected alert open | A modal dialog was open, blocking this operation. |
| unknown command | 404 | unknown command | A?command?could not be executed because the?remote end?is not aware of it. |
| unknown error | 500 | unknown error | An unknown error occurred in the?remote end?while processing the?command. |
| unknown method | 405 | unknown method | The requested?command?matched a known URL but did not match an method for that URL. |
| unsupported operation | 500 | unsupported operation | Indicates that a?command?that should have executed properly cannot be supported for some reason. |
?
?
2.3 WebDriver 配置
為了支持各個(gè)瀏覽器,自然要由各個(gè)瀏覽器廠商來實(shí)現(xiàn)這一套協(xié)議。各個(gè)廠商的瀏覽器畢竟不同,各有各自的一些特性等。為了更好的支持各個(gè)瀏覽器,各個(gè)廠商可以在創(chuàng)建Session時(shí),指定一些自定義的配置。
一些標(biāo)準(zhǔn)的配置有:
?
| Capability | Key | Value Type | Description |
| Browser name | "browserName" | string | Identifies the user agent. |
| Browser version | "browserVersion" | string | Identifies the version of the user agent. |
| Platform name | "platformName" | string | Identifies the operating system of the?endpoint node. |
| Accept insecure TLS certificates | "acceptInsecureCerts" | boolean | Indicates whether untrusted and self-signed TLS certificates are implicitly trusted onnavigation?for the duration of the?session. |
| Page load strategy | "pageLoadStrategy" | string | Defines the?current session’spage load strategy. |
| Proxy configuration | "proxy" | JSONObject | Defines the?current session’sproxy configuration. |
| Window dimensioning/positioning | "setWindowRect" | boolean | Indicates whether the remote end supports all of thecommands?in?Resizing and Positioning Windows. |
| Session timeouts configuration | "timeouts" | JSONObject | Describes the?timeouts?imposed on certain session operations. |
| Unhandled prompt behavior | "unhandledPromptBehavior" | string | Describes the?current session’s?user prompt handler. |
?
除此之外,各個(gè)廠商可以自定義自己的配置。
具體有哪些配置,可以參考:?
https://github.com/SeleniumHQ/selenium/wiki/DesiredCapabilities
?
整理后的配置參見:http://www.cnblogs.com/f1194361820/p/7419522.html
?
3、Selenium 2
?
Selenium 2 = WebDriver Selenium1
只需要將Selenium 1 中的RC Server ,RC Client替換成 WebDriver ,就成了Selenium2的結(jié)構(gòu)。
?
更多專業(yè)前端知識(shí),請上 【猿2048】www.mk2048.com
總結(jié)
以上是生活随笔為你收集整理的Selenium1 Selenium2 WebDriver的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 一段js实现复制文本内容到剪切板
- 下一篇: React 篇 Search Bar a