Selenium3 有哪些變化?
其實相對於與Selenium2,Selenium3沒有做太多的改動。下面給出官方的文檔說明,供參考。
參考文檔:https://seleniumhq.wordpress.com/2013/08/28/the-road-to-selenium-3/
- “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.”
- “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:
-
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.
-
If the maximum active sessions is equal to the length of the list of active sessions, return error with error code session not created.
-
If there is a current user prompt, return error with error code session not created.
-
Let capabilities be the result of getting a property named "
capabilities
" from the parameters argument. -
Let capabilities result be the result of processing capabilities with capabilities as an argument.
-
If capabilities result is an error, return error with error code session not created.
-
Let capabilities be capabilities result’s data.
-
Let session id be the result of generating a UUID.
-
Let session be a new session with the session ID of session id.
-
Set the current session to session.
-
Append session to active sessions.
上面的流程已經在最新的Selenium WebDriver中實現了。所有啟動一個瀏覽器做的session操作可以參考如下核心Selenium代碼邏輯。
1. 第一步設置chromeDriver的路徑后面代碼用到:System.setProperty("webdriver.chrome.driver", "chromedriver.exe");
2. 第二步構建一個命令行對象用於執行chromedriver.exe的命令:
org.openqa.selenium.remote.service.DriverService.Builder.build()
public DS build() {
if (port == 0) {
port = PortProber.findFreePort(); //可用的端口號,例如232323,那么后面用到的命令就是:chromedriver.exe –port 232323
}
if (exe == null) {
exe = findDefaultExecutable();
}
ImmutableList<String> args = createArgs();
return createDriverService(exe, port, args, environment);
}
1. 核心selenium命令執行類:org.openqa.selenium.remote.RemoteWebDriver.RemoteWebDriver(CommandExecutor, Capabilities, Capabilities)
public RemoteWebDriver(CommandExecutor executor, Capabilities desiredCapabilities,
Capabilities requiredCapabilities) {
this.executor = executor;
init(desiredCapabilities, requiredCapabilities);
if (executor instanceof NeedsLocalLogs) {
((NeedsLocalLogs)executor).setLocalLogs(localLogs);
}
try {
startClient(desiredCapabilities, requiredCapabilities);
} catch (RuntimeException e) {
try {
stopClient(desiredCapabilities, requiredCapabilities);
} catch (Exception ignored) {
// Ignore the clean-up exception. We'll propagate the original failure.
}
throw e;
}
try {
startSession(desiredCapabilities, requiredCapabilities);
} catch (RuntimeException e) {
try {
quit();
} catch (Exception ignored) {
// Ignore the clean-up exception. We'll propagate the original failure.
}
throw e;
}
}
以上的代碼完成了如下的操作:
1. 初始化desiredCapabilities對象,這是發送到客戶端的JSON 數據,
2. 啟動一個session,這里包含一個判斷,如果這是一個NEW_SESSION,那么會在上面構建的chromedriver上啟動chromedriver然后在發送session命令。后台操作HTTP請求用到的是Apache HttpClient的API.
上面說明下WebDriver的通信是HTTP的協議,因此這里所有的通信都是通過JSON Wired進行溝通的RESTFul格式。也就是說所有的溝通都是一次RESTFul的request和response的過程。
參考如下Selenium的說明: https://github.com/SeleniumHQ/selenium/wiki/JsonWireProtocol#command-summary
JSON Request:
JSON Response: