虽说UI自动化成本高,维护难度大,但应用广泛,在一定场景下还是非常强有力的工具,所以说还是有必要了解一下的
文章标题为 selenium webdriver 但实际上讲的都是webdriver,历史就不讲了,直接介绍原理
先做一个实验
- 安装好python2.7以及selenium和requests
pip install selenium
pip install requests
- 下载好chromedriver,放到环境变量里,注意要和chrome浏览器版本对上,然后执行chromedriver
sun@sun:~ » chromedriver
Starting ChromeDriver 2.31.488763 (092de99f48a300323ecf8c2a4e2e7cab51de5ba8) on port 9515
Only local connections are allowed.
可以看出,上面的chromedriver启动后在本地启动了一个WEB服务,端口是9515,但只能用于本地链接
- 执行代码
# coding=utf-8
import requests
import time
capabilities = {
"capabilities": {
"alwaysMatch": {
"browserName": "chrome"
},
"firstMatch": [
{}
]
},
"desiredCapabilities": {
"platform": "ANY",
"browserName": "chrome",
"version": "",
"chromeOptions": {
"args": [],
"extensions": []
}
}
}
# 打开浏览器 http://127.0.0.1:9515/session
res = requests.post('http://127.0.0.1:9515/session', json=capabilities).json()
session_id = res['sessionId']
# 打开百度
requests.post('http://127.0.0.1:9515/session/%s/url' % session_id,
json={"url": "http://www.baidu.com", "sessionId": session_id})
time.sleep(3)
# 关闭浏览器,删除session
requests.delete('http://127.0.0.1:9515/session/%s' % session_id, json={"sessionId": session_id})
什么是webdriver
The primary new feature in Selenium 2.0 is the integration of the WebDriver API. WebDriver is designed to provide a simpler, more concise programming interface in addition to addressing some limitations in the Selenium-RC API. Selenium-WebDriver was developed to better support dynamic web pages where elements of a page may change without the page itself being reloaded. WebDriver’s goal is to supply a well-designed object-oriented API that provides improved support for modern advanced web-app testing problems.
上面是官网的介绍,我来转义一下:
- selenium 2.0最重要的特性就是集成了WebDrvier API,WebDriver除了解决一些Selenium-RC API的不足外,旨在提供更简单,更简洁的编程接口
- Selenium-WebDrivers为更好的支持动态页面(也就是ajax,不刷新页面改变DOM)而开发
- 目标是提供一套精心设计的面向对象的API,为现代WEB应用自动化测试提供更好的支持
总结一下,就是说它是为了更好的支持动态页面,更简单易用的编程接口,如果你感觉觉得有点抽像,我们接着往下看
webdriver是怎么运行的
Selenium-WebDriver makes direct calls to the browser using each browser’s native support for automation. How these direct calls are made, and the features they support depends on the browser you are using.
webdriver直接调用了浏览器对自动化测试的原生接口,具体怎么调用,取决于你使用的浏览器(chrome使用chromedriver,IE使用iedriver),但重要的是最终提供出来的接口是一样的
再简化下这个概念:
- 每个浏览器都有自己自动化测试接口,如打开网页,点击等
- 每个浏览器自己的webdriver实现,如chromedriver iedriver都封装了这些自动化测试接口,然后把这些操作以一个标准web restfull api暴露出来
大家看下面的图: