WDA原理分析

WDA原理分析1、什么是WDAWebDriverAgent是Facebook在17年的SeleniumConf大会上推出了一款新的iOS移动测试框架。下面摘录一段官方对于WebDriverAgent的介绍字段:(官方文档:https://github.com/facebook/WebDriverAgent)WebDriverAgent在iOS端实现…

大家好,又见面了,我是你们的朋友全栈君。

1、什么是WDA

WebDriverAgent是Facebook 在17年的 SeleniumConf 大会上推出了一款新的iOS移动测试框架。

下面摘录一段官方对于WebDriverAgent的介绍字段:(官方文档:https://github.com/facebook/WebDriverAgent

WebDriverAgent 在 iOS 端实现了一个 WebDriver server ,借助这个 server 我们可以远程控制 iOS 设备。你可以启动、杀死应用,点击、滚动视图,或者确定页面展示是否正确。This makes it a perfect tool for application end-to-end testing or general purpose device automation.(它说它是iOS上一个完美的e2e的自动化解决方案) It works by linking XCTest.framework and calling Apple’s API to execute commands directly on a device.(链接XCTest.framework调用苹果的API直接在设备上执行命令) WebDriverAgent is developed and used at Facebook for end-to-end testing and is successfully adopted by Appium. (Appium封装工作正在进行中,如果一旦封装好,那么以后就可以直接用Appium提供的binding了。)It is currently maintained by Marek Cirkos and Mehdi Mulani。

image2018-10-12%2017%3A1%3A25.png?versio

2、简单原理图

WebDriver之所以能够实现与浏览器进行交互,是因为浏览器实现了Mobile JSON Wire Protocol Specification协议,这个协议是使用JOSN通过HTTP进行传输。

它的实现使用了经典的Server-Client架构(C/S),客户端发送一个requset,服务器端返回一个response。

image2018-10-12%2017%3A50%3A50.png?versi
在开始下面的内部实现细节的讲解前,我们下明确几个概念:

1、WDAClient

WDAClient是基于WebDriverAgent实现的WDA的客户端。facebook-wda 就是 WDA 的 Python 客户端库,通过直接构造HTTP请求直接跟WebDriverAgent通信。

2、WDAServer

运行WDA App的机器,实现了WebDriver的通讯协议

3、Session

服务器端需要维护客户端的Session,客户端首次发送请求的字符串是’/session/sessionId/urlurlurlsessionId/url′。服务器端根据url打开对应的url地址,同时将sessionId解析成真实的值。然后返回给客户端。以后客户端再向浏览器发送请求时,将会携带session值一起发送。

1
2
3
4
5
6
7
8
[debug] [BaseDriver] Creating session with W3C capabilities: {

"alwaysMatch"
:{"platformNa...
[BaseDriver] Session created with session 
id
: 
7a1d8eca
-
8c48
-
4a94
-
8256
-
ab283e2af4c3
 
[Appium] New AndroidUiautomator2Driver session created successfully, session 
7a1d8eca
-
8c48
-
4a94
-
8256
-
ab283e2af4c3 added to master session 
list
[debug] [BaseDriver] Event 
'newSessionStarted'
 
logged at 
1540198593998
 
(
16
:
56
:
33
 
GMT
+
0800
 
(中国标准时间))
[debug] [W3C] Cached the protocol value 
'W3C'
 
for
 
the new session 
7a1d8eca
-
8c48
-
4a94
-
8256
-
ab283e2af4c3
[debug] [W3C] Responding to client with driver.createSession() result: {

"capabilities"
:{

"platform"
:
"LINUX"
,
"webStorageEnabled"
:false,
"takesScreenshot"
:true,
"javascriptEnabled"
:true,
"databaseEnabled"
:false,
"networkConnectionEnabled"
:true,
"locationContextEnabled"
:false,
"warnings"
:{},
"desired"
:{

"platformName"
:
"Android"
,
"unicodeKeyboard"
:true,
"command_executor"
:
"http://127.0.0.1:4723/wd/hub"
,
"noReset"
:true,
"appActivity"
:
"com.didi.sdk.app.launch.DidiLoadDexActivity"
,
"automationName"
:
"uiautomator2"
,
"newCommandTimeout"
:
300
,
"deviceName"
:
"68de2f65"
,
"recreateChromeDriverSessions"
:
"true"
,
"platformVersion"
:
"7.0"
,
"appPackage"
:
"com.sdu.didi.psnger"
},
"platformName"
:
"Android"
,
"unicodeKeyboard"
:true,
"command_executor"
:
"http://127.0.0.1:4723/wd/hub"
,
"noReset"
:true,
"appActivity"
:
"com.didi.sdk.app.launch.DidiLoadDexActivity"
,
"automationName"
:
"uiautomator2"
,
"newCommandTimeout"
:
300
,
"deviceName"
:
"68de2f65"
,
"recreateChromeDriverSessions"
:
"true"
,
"platformVersion"
:
"7.0"
,
"appPackage"
:
"com.sdu.didi.psnger"
,
"deviceUDID"
:
"68de2f65"
,
"deviceScreenSize"
:
"1440x2560"
,
"deviceScreenDensity"
:
640
,
"deviceModel"
:"SM...
[HTTP] <
-
-
 
POST 
/
wd
/
hub
/
session 
200
 
27897
 
ms 
-
 
1238

1
2
3
[debug] [JSONWP Proxy] Matched 
'/session'
 
to command name 
'createSession'
[debug] [JSONWP Proxy] Proxying [POST 
/
session] to [POST http:
/
/
localhost:
8200
/
wd
/
hub
/
session] with body: {

"desiredCapabilities"
:{

"platform"
:
"LINUX"
,
"webStorageEnabled"
:false,
"takesScreenshot"
:true,
"javascriptEnabled"
:true,
"databaseEnabled"
:false,
"networkConnectionEnabled"
:true,
"locationContextEnabled"
:false,
"warnings"
:{},
"desired"
:{

"platformName"
:
"Android"
,
"unicodeKeyboard"
:true,
"command_executor"
:
"http://127.0.0.1:4723/wd/hub"
,
"noReset"
:true,
"appActivity"
:
"com.didi.sdk.app.launch.DidiLoadDexActivity"
,
"automationName"
:
"uiautomator2"
,
"newCommandTimeout"
:
300
,
"deviceName"
:
"68de2f65"
,
"recreateChromeDriverSessions"
:
"true"
,
"platformVersion"
:
"7.0"
,
"appPackage"
:
"com.sdu.didi.psnger"
},
"platformName"
:
"Android"
,
"unicodeKeyboard"
:true,
"command_executor"
:
"http://127.0.0.1:4723/wd/hub"
,
"noReset"
:true,
"appActivity"
:
"com.didi.sdk.app.launch.DidiLoadDexActivity"
,
"automationName"
:
"uiautomator2"
,
"newCommandTimeout"
:
300
,
"deviceName"
:
"68de2f65"
,
"recreateChromeDriverSessions"
:
"true"
,
"platformVersion"
:
"7.0"
,
"appPackage"
:
"com.sdu.didi.psnger"
,
"deviceUDID"
:
"68de2f65"
,
"deviceScreenSize"
:
"1440x2560"
,
"deviceScreenDensity"
:
640
,"deviceMod...
[debug] [JSONWP Proxy] Got response with status 
200
: {

"sessionId"
:
"eef20bb5-f3ed-4cbc-9977-b32f8be4eea9"
,
"status"
:
0
,
"value"
:
"Created Session"
}

4、WebElement

WebDriverAPI中的对象,代表页面上的一个DOM元素。

5、JsonWireProtocol

JsonWireProtocol(以下简称JWP)是通过使用webdriver与remote server进行通信的
web service 协议
。通过http请求,完成和remote server的交互。

6、Mobile JSON Wire Protocol Specification

移动端自动化协议

7、iOS Accessibility

3、执行流程

1.  启动webdriveragent

image2018-10-12%2018%3A26%3A11.png?versi

2.  启动App

向WebdriverAgent发送post请求 ,请求参考WDA项目中 FBSessionCommands.m

请求地址:url=http://#{ip}:8100/session,WevDriverAgent会响应启动app,并返回session数据;

3. 启动app后,定位元素以及操作元素

定位元素 post请求:url+/session/element, 请求参数是定位元素标签以及值 参考 FBFindElementCommands.m;响应会返回elementId

操作*元素post请求:url+/session/element/id/* 参考项目中文件:/Commands/FBElementCommands.m 里面介绍了很多元素操作的方法 进行相应的转换即可。
image2018-10-12%2018%3A32%3A49.png?versi

4、测试代码与Webdriver的交互

接下来我会以获取界面元素这个基本的操作为例来分析两者之间的关系。
在测试代码中,我们第一步要做的是新建一个webdriver类的对象:

image2018-10-12%2018%3A36%3A22.png?versi

这里新建的driver对象是一个webdriver.Remote()类的对象,而webdriver.Remote()类的本质是

image2018-10-12%2018%3A42%3A42.png?versi

也就是一个来自Remote的WebDriver类。这个.remote.webdriver是继承了selenium.webdriver.remote.command

image2018-10-12%2018%3A45%3A20.png?versi

以python为例,在selenium库中,通过ID获取界面元素的方法是这样的:

image2018-10-12%2019%3A0%3A4.png?version

DATest对其进行二次封装后是这样的:

image2018-10-12%2019%3A0%3A39.png?versio

find_elements_by_id是selenium.webdriver.remote.webdriver.WebDriver类的实例方法。在代码中,我们直接使用的其实不是selenium.webdriver.remote.webdriver.WebDriver这个类,而是针对各个浏览器的webdriver类,例如webdriver.Chrome()、webdriver.Remote()。
所以说在测试代码中执行各种浏览器操作的方法其实都是selenium.webdriver.remote.webdriver.WebDriver类的实例方法。


接下来我们再深入selenium.webdriver.remote.webdriver.WebDriver类来看看具体是如何实现例如find_element_by_id()的实例方法的。

通过Source code可以看到:

image2018-10-15%2017%3A56%3A57.png?versi

这个方法最后call了一个execute方法,方法的定义如下:
image2018-10-15%2010%3A33%3A41.png?versi

如注释中提到的,其中的关键在于一个名为command_executor的对象执行了execute方法。

response = self.command_executor.execute(driver_command, params)

名为command_executor的对象是RemoteConnection类的对象,并且这个对象是在新建selenium.webdriver.remote.webdriver.WebDriver类对象的时候就完成赋值的

self.command_executor = RemoteConnection(command_executor, keep_alive=keep_alive)



结合selenium.webdriver.remote.webdriver.WebDriver类的类注释来看:
image2018-10-15%2017%3A5%3A11.png?versio

WebDriver类的功能是通过给一个remote server发送指令来控制浏览器。而这个remote server是一个运行WebDriver wire protocol的server。而RemoteConnection类就是负责与Remote WebDriver server的连接的类。

可以注意到有这么一个新建WebDriver类的对象时候的参数command_executor,默认值=’http://127.0.0.1:4444/wd/hub‘。这个值表示的是访问remote server的URL。因此这个值作为了RemoteConnection类的构造方法的参数,因为要连接remote server,URL是必须的。

现在再来看RemoteConnection类的实例方法execute。

image2018-10-15%2017%3A7%3A21.png?versio

这个方法有两个参数:

  • command
  • params

command表示期望执行的指令的名字。通过观察self._commands这个dict可以看到,self._commands存储了selenium.webdriver.remote.command.Command类里的常量指令和WebDriver wire protocol中定义的指令的对应关系。


image2018-10-15%2017%3A8%3A39.png?versio

以FIND_ELEMENT为例可以看到,指令的URL部分包含了几个组成部分:

  • HTTP请求方法。WebDriver wire protocol中定义的指令是符合RESTful规范的,通过不同请求方法对应不同的指令操作。

  • sessionId。Session的概念是这么定义的:

    The server should maintain one browser per session. Commands sent to a session will be directed to the corresponding browser.

    也就是说sessionId表示了remote server和浏览器的一个会话,指令通过这个会话变成对于浏览器的一个操作。

  • element。这一部分用来表示具体的指令。

selenium.webdriver.remote.command.Command类里的常量指令又在各个具体的类似find_elements的实例方法中作为execute方法的参数来使用,这样就实现了selenium.webdriver.remote.webdriver.WebDriver类中实现各种操作的实例方法与WebDriver wire protocol中定义的指令的一一对应。
selenium.webdriver.rmote.webelement.WebElement中各种在WebElement上的操作也是用类似的原理实现的。

实例方法execute的另一个参数params则是用来保存指令的参数的,这个参数将转化为JSON格式,作为HTTP请求的body发送到remote server。
remote server在执行完对浏览器的操作后得到的数据将作为HTTP Response的body返回给测试代码,测试代码经过解析处理后得到想要的数据。



image2018-10-23%2011%3A7%3A17.png?versio


通过对python selenium库的分析,希望能够帮助大家对selenium和webdriver的实现原理有更进一步的了解,在日常的自动化脚本开发中更加快捷的定位问题和解决问题。


附录:
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/158136.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 下载scrapy失败_手机安装包无法安装怎么办

    下载scrapy失败_手机安装包无法安装怎么办Scrapy安装有问题的,按照这个路径配置下anaconda的环境变量然后进入pycharm里的工作目录输入pipinstall-ihttps://pypi.tuna.tsinghua.edu.cn/simplescrapy安装完成后在cmd中输入scrapy,显示下图内容则证明安装成功:…

    2022年9月17日
    6
  • 【产品面试】经典的一些面试题回顾[通俗易懂]

    【产品面试】经典的一些面试题回顾[通俗易懂]转自:https://www.cnblogs.com/lianghong/p/9339594.html产品经理面试习题大汇总(一)1、介绍一下你自己。介绍一下自己的姓名,年龄、毕业院校,工作经历。简单的介绍,保持在三分钟以内,给面试官问问题的时间。工作经历主要讲一些你牛逼的工作经历,例如:你加入XX公司以后,销售额增加了多少、用户翻了多少倍…这样一些。有些人工作经历比较多…

    2022年6月21日
    22
  • mybatis的逆向工程怎么实现_mybatisinsert

    mybatis的逆向工程怎么实现_mybatisinsert1.什么是逆向工程mybatis官方提供逆向工程,可以针对单表自动生成mybatis执行所需要的代码(mapper.java、mapper.xml、pojo…)企业实际开发中,常用的逆向工程方式:由数据库的表生成java代码。注意:只能对单表进行操作2.逆向工程的作用myBatis逆向工程可以方便的从数据库中将表自动映射到JAVAPOJO类,并同时生成Mapper.xml和Mapper接…

    2022年8月21日
    4
  • Dingo Api请求接口404?

    Dingo Api请求接口404?Dingo Api请求接口404?

    2022年4月24日
    35
  • 使用ARP命令[通俗易懂]

    使用ARP命令[通俗易懂]讲解ARPCache更新过程,并分析对应请求报文以及响应报文的内容。1.建立网络拓扑2.填写4个主机的ip地址和子网掩码3.分别对主机pc0和pc1的命令行中输入“arp-a”命令查看其ARP高速缓存,发现无任何缓存信息。4.切换到simulation模式,pc0和pc1的ip分别为源地址和目的地址5.创建一个简单的PDU6.可以发现pc0和pc1之间成功进行了一次传输,即pc0得到了pc1的ip地址和MAC地址7.可以看出pc0发送了一个广播报文8.pc1得到响应

    2025年6月19日
    1
  • JavaScript-ECMAScript5-JS基础语法「建议收藏」

    JavaScript-ECMAScript5-JS基础语法「建议收藏」JavaScript-ECMAScript5-基础语法

    2022年8月11日
    7

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号