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/Activityhttps://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代码逻辑。

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源码初探的更多相关文章

  1. IOS懒人笔记应用源码

    这个源码是懒人笔记应用源码,也是一个已经上线的apple应用商店的应用,懒人笔记iOS客户端源码,支持语音识别,即将语音转化成文本文字,所用语音识别类库为讯飞语音类库. 懒人笔记是一款为懒人设计的笔记 ...

  2. Catalyst揭秘 Day2 Catalyst源码初探

    Catalyst揭秘 Day2 Catalyst源码初探 这节课从源码角度来讲catalyst. 首先有一个观点要澄清,就是技术不是越底层就越是性能和效率更高.因为除了指令执行性能以外,更重要的是架构 ...

  3. Servlet源码初探

    年底,公司的事情告一段落,就来捣鼓一下这个Servlet源码,为下一步的spingmvc源码初探做准备 1.Servlet接口 public interface Servlet { void init ...

  4. Hadoop学习笔记(9) ——源码初窥

    Hadoop学习笔记(9) ——源码初窥 之前我们把Hadoop算是入了门,下载的源码,写了HelloWorld,简要分析了其编程要点,然后也编了个较复杂的示例.接下来其实就有两条路可走了,一条是继续 ...

  5. [转]OpenTK学习笔记(1)-源码、官网地址

    OpenTK源码下载地址:https://github.com/opentk/opentk OpenTK使用Nuget安装命令:OpenTK:Install-Package OpenTK -Versi ...

  6. 【一起学源码-微服务】Feign 源码一:源码初探,通过Demo Debug Feign源码

    前言 前情回顾 上一讲深入的讲解了Ribbon的初始化过程及Ribbon与Eureka的整合代码,与Eureka整合的类就是DiscoveryEnableNIWSServerList,同时在Dynam ...

  7. 笔记-twisted源码-import reactor解析

    笔记-twisted源码-import reactor解析 1.      twisted源码解析-1 twisted reactor实现原理: 第一步: from twisted.internet ...

  8. RequireJS源码初探

    前两天跟着叶小钗的博客,看了下RequireJS的源码,大体了解了其中的执行过程.不过在何时进行依赖项的加载,以及具体的代码在何处执行,还没有搞透彻,奈何能力不够,只能先记录一下了. RequireJ ...

  9. Nginx学习笔记4 源码分析

    Nginx学习笔记(四) 源码分析 源码分析 在茫茫的源码中,看到了几个好像挺熟悉的名字(socket/UDP/shmem).那就来看看这个文件吧!从简单的开始~~~ src/os/unix/Ngx_ ...

随机推荐

  1. Multi-line NSAttributedString with truncated text

    http://stackoverflow.com/questions/7611816/multi-line-nsattributedstring-with-truncated-text/1017279 ...

  2. 《深入理解Spark:核心思想与源码分析》一书正式出版上市

    自己牺牲了7个月的周末和下班空闲时间,通过研究Spark源码和原理,总结整理的<深入理解Spark:核心思想与源码分析>一书现在已经正式出版上市,目前亚马逊.京东.当当.天猫等网站均有销售 ...

  3. Eclipse-修改工程名

    Eclipse-修改工程名 来自:http://southking.iteye.com/blog/1821754   直接修改工程可能会产生一些莫名其妙的问题,需遵循以下四步: 1. 右键工程:Ref ...

  4. c# 验证码类

    using System; using System.Drawing; using System.Drawing.Drawing2D; using System.Drawing.Imaging; us ...

  5. submit(提交)按钮

    为form添加一个submit(提交)按钮,点击这个按钮,表单中的数据将会被发送到通过action属性指定的地址上. 下面是submit按钮的例子: <button type="sub ...

  6. Memcache的使用基本介绍

    Memcache学习总结2-Memcache的使用基本介绍 上一次总结中我们已经安装部署好了Memcached,并且把PHP扩展Memcache也安装好了,这一节我们详细学习一下PHP扩展Memcac ...

  7. ASP.NET MVC学习之视图篇(2)

    继ASP.NET MVC学习之视图(1)学习 4.HTML辅助器 虽然在ASP.NET MVC中我们已经摆脱了ASP.NET的控件,但是对于页面中需要循环标签的情况依然还是存在,可能很多人认为用for ...

  8. solr与.net系列课程(六)solr定时增量索引与安全

     solr与.net系列课程(六)solr定时增量索引与安全 solr增量索引的方式,就是一个Http请求,但是这样的请求显然不能满足要求,我们需要的是一个自动的增量索引,solr官方提供了一个定时器 ...

  9. Salesforce 生成测试可用 Id

    在写 Test Class 的时候,有时候需要一批有 Id 的数据或者把 Id 作为参数等情况,在数据关系比较复杂的情况下去造真实数据有些麻烦,于是找到这样一个可以生成 Id 的方法可以用来辅助测试! ...

  10. 转载:开发者眼中最好的 22 款 GUI 测试工具

    对于很多同学来说gui程序的测试是一个难点,所以我从网上转载了一篇关于gui测试的一篇文章,里面罗列的很多工具,大家可以尝试一下学习学习. 英文原文:22 best GUI testing tools ...