Selenium3笔记-WebDriver源码初探
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:

Selenium3笔记-WebDriver源码初探的更多相关文章
- IOS懒人笔记应用源码
这个源码是懒人笔记应用源码,也是一个已经上线的apple应用商店的应用,懒人笔记iOS客户端源码,支持语音识别,即将语音转化成文本文字,所用语音识别类库为讯飞语音类库. 懒人笔记是一款为懒人设计的笔记 ...
- Catalyst揭秘 Day2 Catalyst源码初探
Catalyst揭秘 Day2 Catalyst源码初探 这节课从源码角度来讲catalyst. 首先有一个观点要澄清,就是技术不是越底层就越是性能和效率更高.因为除了指令执行性能以外,更重要的是架构 ...
- Servlet源码初探
年底,公司的事情告一段落,就来捣鼓一下这个Servlet源码,为下一步的spingmvc源码初探做准备 1.Servlet接口 public interface Servlet { void init ...
- Hadoop学习笔记(9) ——源码初窥
Hadoop学习笔记(9) ——源码初窥 之前我们把Hadoop算是入了门,下载的源码,写了HelloWorld,简要分析了其编程要点,然后也编了个较复杂的示例.接下来其实就有两条路可走了,一条是继续 ...
- [转]OpenTK学习笔记(1)-源码、官网地址
OpenTK源码下载地址:https://github.com/opentk/opentk OpenTK使用Nuget安装命令:OpenTK:Install-Package OpenTK -Versi ...
- 【一起学源码-微服务】Feign 源码一:源码初探,通过Demo Debug Feign源码
前言 前情回顾 上一讲深入的讲解了Ribbon的初始化过程及Ribbon与Eureka的整合代码,与Eureka整合的类就是DiscoveryEnableNIWSServerList,同时在Dynam ...
- 笔记-twisted源码-import reactor解析
笔记-twisted源码-import reactor解析 1. twisted源码解析-1 twisted reactor实现原理: 第一步: from twisted.internet ...
- RequireJS源码初探
前两天跟着叶小钗的博客,看了下RequireJS的源码,大体了解了其中的执行过程.不过在何时进行依赖项的加载,以及具体的代码在何处执行,还没有搞透彻,奈何能力不够,只能先记录一下了. RequireJ ...
- Nginx学习笔记4 源码分析
Nginx学习笔记(四) 源码分析 源码分析 在茫茫的源码中,看到了几个好像挺熟悉的名字(socket/UDP/shmem).那就来看看这个文件吧!从简单的开始~~~ src/os/unix/Ngx_ ...
随机推荐
- [PHP]Yaf + composer 引起大幅性能下降
composer.json 文件可以用命令 composer init 创建,命令是交互式的. 也可以直接编辑一个 json 文件,如下: repositories 中 url 使用中国全量镜像地址. ...
- 本地Mac如何搭建IPv6环境
http://blog.csdn.net/dancen/article/details/51595374 这篇文章写的很详细,借鉴用啦-
- Mac上Homebrew的使用 (Homebrew 使 OS X 更完整)
0 Homebrew是啥? “Homebrew installs the stuff you need that Apple didn’t.——Homebrew 使 OS X 更完整”. Homebr ...
- php中文截取无乱码方法
直接使用PHP函数substr截取中文字符可能会出现乱码,主要是substr可能硬生生的将一个中文字符“锯”成两半.解决办法: 1.使用mbstring扩展库的mb_substr截取就不会出现乱码了. ...
- CSS3 线性渐变(linear-gradient) 兼容IE8,IE9
一.线性渐变在 Mozilla 下的应用 语法: -moz-linear-gradient( [<point> || <angle>,]? <stop>, ...
- hibernate连接查询
Hibernate的HQL语言类似于SQL语言,更适合于Java面向对象的思想. 类与数据库映射好了,不必考虑数据库. 实现Class1的表与Class2的表的联合查询: Class1的class2属 ...
- Web 开发中 20 个很有用的 CSS 库
转自:http://www.oschina.net/translate/css-libraries-for-developers 在过去的几年中,CSS已经成为一大部分开发者和设计者的最爱,因为它提供 ...
- 在sqlServer中把数据导出为insert脚本
有时候为了把数据导出为insert脚本,不得不用一些小工具,或者通过自己写存储过程来完成这一操作.其实SqlServer本身就有这种功能.以下是详细步骤:
- Linux下jvm、tomcat、mysql、log4j优化配置笔记
小菜一直对操作系统心存畏惧,以前也很少接触,这次创业购买了Linux云主机,由于木有人帮忙,只能自己动手优化服务器了.... 小菜的云主机配置大致为:centeos6(32位),4核心cpu,4G内存 ...
- 为什么不能把委托(delegate)放在一个接口(interface)当中?
stackoverflow上有人问,为什么不能把委托放在一个接口当中? 投票最多的第一个答案第一句话说,“A Delegate is just another type, so you don't g ...