Selenium1 Selenium2 WebDriver

内容摘要:

  1. Selenium 1 原理
  2. WebDriver
    • WebDriver 组件
    • WebDriver 协议
      • Remote End 处理流程
      • Commands & Endpoints & 请求路由
      • 错误消息
    • WebDriver 配置
  3. Selenium 2

 

 

1、Selenium 1 原理

 

 

 

(1).测试用例Testcase)通过Client Lib的接口向Selenium Server发送Http请求,要求和Selenium Server建立连接。

为什么要通过发送Http请求控制Selenium Server而不采用其他方式呢?从上文可以看出,Selenium Server是一个独立的中间服务器(确切地说是代理服务器),它可以架设在其他机器上!所以测试案例通过发送HTTP请求去控制Selenium Server是很正常的。

(2).Selenium Server的Launcher启动浏览器,把Selenium Core加载入浏览器页面当中,并把浏览器的代理设置为Selenium Server的Http Proxy。

(3).测试案例通过Client Lib的接口向Selenium Server发送Http请求,Selenium Server对请求进行解析,然后通过Http Proxy发送JS命令通知Selenium Core执行操作浏览器的动作。

(4).Selenium Core接收到指令后,执行操作。

(5).浏览器收到新的页面请求信息(因为在(4)中,Selenium Core的操作可能引发新的页面请求),于是发送Http请求,请求新的Web页面。
由于Selenium Server在启动浏览器时做了手脚,所以Selenium Server会接收到所有由它启动的浏览器发送的请求。

(6).Selenium Server接收到浏览器的发送的Http请求后,自己重组Http请求,获取对应的Web页面。

(7).Selenium Server的Http Proxy把接收的Web页面返回给浏览器。

 

2WebDriver

WebDriver 提供了一堆用于查找和维护页面元素(DOM元素)的接口,以此方式来控制浏览器行为。目前已纳入W3C管理。

https://www.w3.org/TR/webdriver/

 

2.1 Webdriver 组件

在WebDriver协议里,包括local,remote,Intermidary,endpoint等概念:

·Local End:代表了WebDriver通信的客户端,通常情况下,是由一些特定语言基于通信协议组成的一套Client API

·Remote Endremote是在通信协议的服务端。用于处理Client请求并作出响应。它包括两大类:

    Intermidary nodeintermidary 通常是代理,它实现了local,并代理了remote

Endpoint endpoint 才是实际上的remote 端的处理的地方。和Java Web开发中的actionWebservice中的endpoint是类似的概念。

 

 

 

 

Webdriver 的通信协议是基于HTTP的以命令(command)方式组织的各种请求。WebDriver在处理一个命令时,可能会使browser执行一系列的操作。上面图中,webdriver server就是remote endWebDriver Client就是local end

 

2.2 WebDriver 协议

 

2.2.1 Remote End 处理流程

在local endremote end的连接建立后,Remote End会做如下处理:

1、根据HTTP协议读取一个完整的HTTP请求,并封装成request对象。

2、根据http request method, url对请求进行匹配,找到相应的endpoint

3、如果匹配到了error ,就发送一个errorlocal end,然后step 1

4、使用sessionid进行session匹配

5、如果匹配失败(session 列表里找不到与sessionid相关联的session),响应一个error,然后step 1

6、如果是post请求,以json方式解析requet body。如果解析成功,但并不是一个对象,或者解析失败,响应一个errorLocal。然后step 1

7、等待浏览器导航完毕

8、响应结果

 

2.2.2 Commands & Endpoints & 请求路由

 

为了控制browser的行为,提供了一系列的command,大体可以分为几类:

1)Session管理(remote local之间的session

2)浏览器URL导航

3)浏览器窗口管理

4)DOM元素管理

5)JS脚本执行

6)Cookie管理

7)Alert

8)截屏

9)一些操作例如(键盘,鼠标,批处理等)

 

 

Remote在接收到这些命令时,会进行Endpoint匹配。匹配时是根据URLhttp method

他们的对应关系是:

 

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 错误消息

从上面的流程里,有多次提到发现error时,回发一个error。而实际上返回的错误需要包括

{

Error: // 错误码

Message://错误信息

Stacktrace:// 发生错误时的stack

}

 

下面是各种错误的描述:

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 配置

为了支持各个浏览器,自然要由各个浏览器厂商来实现这一套协议。各个厂商的浏览器毕竟不同,各有各自的一些特性等。为了更好的支持各个浏览器,各个厂商可以在创建Session时,指定一些自定义的配置。

一些标准的配置有:

 

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.

 

除此之外,各个厂商可以自定义自己的配置。

具体有哪些配置,可以参考: 

https://github.com/SeleniumHQ/selenium/wiki/DesiredCapabilities

 

整理后的配置参见:http://www.cnblogs.com/f1194361820/p/7419522.html

 

3Selenium 2

 

Selenium 2 = WebDriver Selenium1

只需要将Selenium 1 中的RC Server RC Client替换成 WebDriver ,就成了Selenium2的结构。

 


更多专业前端知识,请上 【猿2048】www.mk2048.com

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/361137.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Fork / Join框架vs并行流vs.ExecutorService:最终的Fork / Join基准

Fork / Join框架在不同配置下如何工作? 就像即将上映的《星球大战》一样,围绕Java 8并行性的批评也充满了兴奋。 并行流的语法糖带来了一些炒作,就像我们在预告片中看到的新型光剑一样。 现在,有了许多使用Java进行并行处理的方法…

CSRF攻击与防御

CSRF是什么 CSRF在百度百科中是这么说的:“CSRF(Cross-site request forgery跨站请求伪造,也被称为“one click attack”或者session riding,通常缩写为CSRF或者XSRF,是一种对网站的恶意利用。尽管听起来像跨站脚本&a…

Silverlight学习笔记(九)-----RenderTransform特效【五种基本变换】及【矩阵变换MatrixTransform】...

RenderTransform特效: 变形(RenderTransform)类是为了达到直接去改变某个Silverlight对象的形状(比如缩放、旋转一个元素)的目的而设计的,RenderTransform包含的变形属性成员就是专门用来改变Silverlight对…

React 篇 Search Bar and content Table

我们要构建一个模块,其中包含一个内容显示的表格,然后上面有一个提供Search的栏位,并对Search中输入栏进行监听,当有改变的时候,触发Search然后对内容表中的内容进行过滤。 Demo Link:http://czrmodel.mybluemix.net/…

PrimeFaces:在动态生成的对话框中打开外部页面

我已经在即将出版的PrimeFaces Cookbook 2版中写了一篇关于食谱的博客。 在这篇文章中,我想发表第二篇关于一个名为Dialog Framework的小型框架的文章。 我个人喜欢它,因为我记得我为使用Struts框架付出同样的努力而付出的代价。 当您想将外部页面加载到…

vue-router之 beforeRouteEnter

beforeRouteEnter在每次路由切换都执行 ,而项目优化后,切换路由mounted只在最开始执行一次beforeRouteEnter的具体用法可参考官方文档 https://cn.vuejs.org/v2/guide/migration-vue-router.html#activate-替换 需要注意的是:在这期间路由跳转携带的数据发生改变会影…

突破极限–如何将AeroGear Unified Push用于Java EE和Node.js

在2014年底的AeroGear队宣布红帽的JBoss统一推送服务器的可用性xPaaS 。 让我们仔细看看! 总览 统一推送服务器允许开发人员将本地推送消息发送到Apple的推送通知服务(APNS)和Google的云消息传递(GCM)。 它具有一个内…

js 获取json数组里面数组的长度

作为一个前端页面开发者第一次处理json数据,遇到了‘js 获取json数组里面数组的长度’?竟然不知道 json没有.length属性(真是要嘲讽下自己),少壮不努力老大徒伤悲啊!以前都是去寻求男朋友帮助,但…

针对WildFly和EAP运行Java Mission Control和Flight Recorder

Java Mission Control (JMC)使您可以监视和管理Java应用程序,而无需引入通常与这些类型的工具相关的性能开销。 它使用为正常的JVM动态优化而收集的数据,从而形成了一种非常轻量级的方法来观察和分析应用程序代码中的问题。 JMC由…

C#堆栈和堆的讲解

OS和CLR通常将用于容纳数据的内存划分为两个独立的区域,每个区域都采用截然不同的方式来管理:堆栈(Stack)和堆(heap)。(1) 调用一个方法时,它的参数以及它的局部变…

jQuery中的常用内容总结(一)

jQuery中的常用内容总结(一) 前言 不好意思(✿◠‿◠),由于回家看病以及处理一些其它事情耽搁了,不然这篇博客本该上上周或者上周写的;同时闲谈几句:在这里建议各位开发的童鞋,如果有疾病尽快治疗,不要拖&a…

线程魔术技巧:Java线程可以做的5件事

Java线程最鲜为人知的事实和用例是什么? 有些人喜欢爬山,有些人喜欢跳伞。 我,我喜欢Java。 我喜欢它的一件事是,您永不停止学习。 您每天使用的工具通常可以向您展示全新的方面,以及您还没有机会看到的方法和有趣的用…

强制删除tfs未迁入项的两个方法。

方法1: 打开Vs2008的命令提示: 查看用户的工作区: 输入: Tf workspaces /owner:所有者或* /server:服务器名称或IP ex: tf workspaces /owner:stcct(所有者) /server:203.156.1.100(Ip地址) 删除用户的未迁入项: 输…

点击左侧跳到右侧

效果图 JS部分 function moveOption(e1, e2){ try{ for(var i0;i<e1.options.length;i ){ if(e1.options[i].selected){ var e e1.options[i]; e2.options.add(new Option(e.text, e.value)); e1.remove(i); iii-1 } } } catch(e){}} HTML以及CSS部分 <for…

IDC关于使用JBoss Fuse的商业价值的报告(与Apache Camel一起使用)

这只是一篇博客文章&#xff0c;具有更多的商业性质&#xff0c;但是您不能一无所有。 实际上&#xff0c;这也是使Apache Camel保持活力并保持良好状态的原因&#xff0c;这还归功于其商业上的成功。 希望从JBoss Fuse之类的产品中寻找有关在商业上使用Apache Camel的附加值的…

Spring-Quartz (一)

摘自&#xff1a; http://www.blogjava.net/Jay2009/archive/2009/03/25/259176.htmlSpring为创建Quartz的Scheduler、Trigger和JobDetail提供了便利的FactoryBean类&#xff0c;以便能够在Spring 容器中享受注入的好处。此外Spring还提供了一些便利工具类直接将Spring中的Bean…

在Java EE 7上骑骆驼–带有Swagger文档的REST服务

骆驼开箱即用。 Swagger集成就是其中之一。 不幸的是&#xff0c;大多数已经存在的功能都严重依赖于Spring。 但这并不能阻止我们在普通的Java EE 7应用程序中使用它们&#xff0c;因为有时它只是服务器的轻量级变体。 但我不想再对此进行讨论。 相反&#xff0c;我认为在所有情…

怎么隐藏滚动条又能滚动

1 <!DOCTYPE html>2 <html lang"en">3 <head>4 <meta charset"UTF-8">5 <title>滚动条隐藏</title>6 <style>7 body, ul, li {8 margin: 0;9 padding: 0; 10 …

Eclipse to android

JDK Eclipse Android SDK ADT 1 必须软件 Java JDK SE 1.6 (jdk-7u9-windows-i586.exe) Eclipse (Eclipse IDE for Java Developers) Google Android SDK (android-sdk_r15-windows.zip) ADT (ADT-15.0.0.zip) 如果找不到可参考&#xff1a; http://blog.csdn.net/zhenyong…

canvas画饼图

<style> body { background: black; text-align: center; } #cans { background: white; } </style> <script> function disToRad(n){//将度数表示弧度计算的方法 return n*Math.PI/180;//π用PI表示&#xff0c;π180&#xff0c;所以1PI/180 } w…