Postman实现UI自动化测试

科技资讯 投稿 5300 0 评论

Postman实现UI自动化测试

作者:测试蔡坨坨

caituotuo.top/1db4fa44.html


看到这篇文章的标题,是不是有小伙伴会感到惊讶呢?

其实,只要你了解Selenium的运行原理,就可以理解为什么Postman也能实现UI自动化测试了。

Selenium底层原理

所以,本质上是调用http请求的过程,因此也就可以理解为什么可以使用Postman实现UI自动化测试。

Postman实现UI自动化测试

这些接口信息,我们可以通过对源码的分析得到。

运行chromedriver.exe
from selenium import webdriver
driver = webdriver.Chrome(

执行上述代码,程序会打开Chrome浏览器。(前提:已经正确配置了Chrome驱动和对应的版本)

源码分析:

由于我们跳过了代码脚本,因此需要手动启动浏览器驱动。

127.0.0.1:9515

新建浏览器会话

继续查看源码,这里有一行重要的代码:

http://127.0.0.1:9515/session发送了一个post请求,参数是JSON格式,然后返回一个特定的响应信息给程序,主要就是新建了一个sessionId。

url: /session
method: POST
content_type: application/json

请求参数:

{
    "capabilities": {
        "browserName": "chrome"
    }
}

调用接口:

访问目标网站
driver.get("https://www.baidu.com"

执行以上代码,可以访问目标网站。

D:\Python3\Lib\site-packages\selenium\webdriver\remote\remote_connection.py

Command.GET: ("POST", "/session/$sessionId/url"这个地址就是实现访问一个网站的URL。

打开浏览器操作浏览器实现各种动作是通过上一步新建浏览器会话返回的sessionId实现的关联。你也会发现后面操作的各种接口地址中都存在一个$sessionId,以达到能够在同一个浏览器中做操作。

url: /session/$sessionId/url
method: POST
content_type: application/json

请求参数:

{
    "url": "目标网站地址"
}

调用接口:

窗口最大化
driver.maximize_window(

源码分析:

url: /session/$sessionId/window/maximize
method: POST
content_type: application/json

调用接口:

元素定位
driver.find_element(By.XPATH, "//input[@id='kw']"

源码分析:

url: /session/$sessionId/element
method: POST
content_type: application/json

请求参数:

{
    "using": "xpath", // 定位方式
    "value": "//input[@id='kw']" // 值
}

接口调用:

输入文本
driver.find_element(By.XPATH, '//input[@type="text"]'.send_keys("测试蔡坨坨"

源码分析:

url: /session/$sessionId/element/$id/value
method: POST
content_type: application/json

请求参数:

{
    "text": "测试蔡坨坨"
}

接口调用:

点击元素
driver.find_element(By.XPATH, "//input[@id='su']".click(

源码分析:

url: /session/$sessionId/element/$id/click
method: POST
content_type: application/json

接口调用:

关闭浏览器
driver.quit(

源码分析:

url: /session/$sessionId
method: DELETE
content_type: application/json

接口调用:

postman_collection.json

https://pan.baidu.com/s/12lzuy0f-o7aVO0oYgw3OMg
提取码:ctta

编程笔记 » Postman实现UI自动化测试

赞同 (22) or 分享 (0)
游客 发表我的评论   换个身份
取消评论

表情
(0)个小伙伴在吐槽