赞
踩
说起postman,第一反应就是不是拿来做接口测试的吗,还能做ui自动化?
是的,要做ui自动化离不开三样东西,测试脚本,驱动,浏览器。
把自动化理解为打车,打车有三个角色:
这个类比中:
测试脚本相当于乘客,浏览器驱动相当于司机,浏览器就是出租车
理解了这层关系,明白selenium的工作原理后,你就能明白postman该如何拿来做自动化了。
请先看这篇文章:selenium工作原理
一般做自动化我们都是用selenium包,结合python或者java等语言去写脚本,这就相当于你跟司机交流是用普通话,全国通用,突然有一天你打车遇到个老乡,于是你们就说起了家乡话,照样能交流和到达目的地。
直接看例子如何实现
1.cmd下执行 start chromedriver启动一个webdriver.exe(前提是已经配置好对应版本驱动)
启动后默认端口为9515,Only local connections are allowed,localhost:9515,下一步是要获取启动浏览器接口地址来启动一个浏览器
2.postman中发起post请求启动浏览器 ,地址:http://127.0.0.1:9515/session
- {"capabilities": {
- "firstMatch": [{}],
- "alwaysMatch": {"browserName": "chrome",
- "platformName": "any",
- "goog:chromeOptions": {"extensions": [], "args": []}
- }},
- "desiredCapabilities": {"browserName": "chrome",
- "version": "",
- "platform": "ANY",
- "goog:chromeOptions": {"extensions": [], "args": []}}
- }
3.继续发送post请求在浏览器上输入jd网址,打开jd首页,
请求地址:url = 'http://127.0.0.1:9515/session/%s/url' %session_id
4.前面这3步就实现了以下代码的功能,其他操作也是通过类似的请求,具体哪些操作对应哪些接口在源码里可以看到。如下图
- from selenium import webdriver
- driver = webdriver.Chrome()
- driver.get("https://www.jd.com")
明白这些之后,其实利用jmeter和request等都可以来实现 ,原理都是一样的,发请求给驱动,让驱动去执行对应的操作
- # 1.启动浏览器获取session_id
- import requests
- params = {"capabilities": {
- "firstMatch": [{}],
- "alwaysMatch": {"browserName": "chrome",
- "platformName": "any",
- "goog:chromeOptions": {"extensions": [], "args": []}
- }},
- "desiredCapabilities": {"browserName": "chrome",
- "version": "",
- "platform": "ANY",
- "goog:chromeOptions": {"extensions": [], "args": []}}
- }
- url='http://127.0.0.1:9515/session'
- res = requests.post(url=url,json=params) # 注意这里要用json传参数
- session_id = (res.json()["value"]["sessionId"]) #获取sessionId
-
- # 2.获取session_id后后续操作带上
- url = 'http://127.0.0.1:9515/session/%s/url' % session_id
- data = {'url': "http://www.jd.com"}
- res = requests.post(url=url, json=data)
个人观点,虽然说不用selenium也能实现ui自动化,听起来很厉害的样子,了解原理之后其实也没什么,这些花里胡哨的东西没太多用,真去折腾起来费时费力,真的做自动化还是用专业的工具,这些都是学习过程,掌握原理后的实践。
关于如何获取接口地址?通过之前的selenium工作原理的源码分析,我们知道其实是通过发送一个http请求来启动浏览器的。selenium通过start_session()来启动一个浏览器,而start_session()实际上也是发送一个请求给驱动,请求地址可以在源码中找到,路径就是/session,其他接口地址源码中如下。
- self._commands = {
- Command.NEW_SESSION: ('POST', '/session'),
- Command.QUIT: ('DELETE', '/session/$sessionId'),
- Command.W3C_GET_CURRENT_WINDOW_HANDLE:
- ('GET', '/session/$sessionId/window'),
- Command.W3C_GET_WINDOW_HANDLES:
- ('GET', '/session/$sessionId/window/handles'),
- Command.GET: ('POST', '/session/$sessionId/url'),
- Command.GO_FORWARD: ('POST', '/session/$sessionId/forward'),
- Command.GO_BACK: ('POST', '/session/$sessionId/back'),
- Command.REFRESH: ('POST', '/session/$sessionId/refresh'),
- Command.W3C_EXECUTE_SCRIPT:
- ('POST', '/session/$sessionId/execute/sync'),
- Command.W3C_EXECUTE_SCRIPT_ASYNC:
- ('POST', '/session/$sessionId/execute/async'),
- Command.GET_CURRENT_URL: ('GET', '/session/$sessionId/url'),
- Command.GET_TITLE: ('GET', '/session/$sessionId/title'),
- Command.GET_PAGE_SOURCE: ('GET', '/session/$sessionId/source'),
- Command.SCREENSHOT: ('GET', '/session/$sessionId/screenshot'),
- Command.ELEMENT_SCREENSHOT: ('GET', '/session/$sessionId/element/$id/screenshot'),
- Command.FIND_ELEMENT: ('POST', '/session/$sessionId/element'),
- Command.FIND_ELEMENTS: ('POST', '/session/$sessionId/elements'),
- Command.W3C_GET_ACTIVE_ELEMENT: ('GET', '/session/$sessionId/element/active'),
- Command.FIND_CHILD_ELEMENT:
- ('POST', '/session/$sessionId/element/$id/element'),
- Command.FIND_CHILD_ELEMENTS:
- ('POST', '/session/$sessionId/element/$id/elements'),
- Command.CLICK_ELEMENT: ('POST', '/session/$sessionId/element/$id/click'),
- Command.CLEAR_ELEMENT: ('POST', '/session/$sessionId/element/$id/clear'),
- Command.GET_ELEMENT_TEXT: ('GET', '/session/$sessionId/element/$id/text'),
- Command.SEND_KEYS_TO_ELEMENT:
- ('POST', '/session/$sessionId/element/$id/value'),
- Command.UPLOAD_FILE: ('POST', "/session/$sessionId/se/file"),
- Command.GET_ELEMENT_TAG_NAME:
- ('GET', '/session/$sessionId/element/$id/name'),
- Command.IS_ELEMENT_SELECTED:
- ('GET', '/session/$sessionId/element/$id/selected'),
- Command.IS_ELEMENT_ENABLED:
- ('GET', '/session/$sessionId/element/$id/enabled'),
- Command.GET_ELEMENT_RECT:
- ('GET', '/session/$sessionId/element/$id/rect'),
- Command.GET_ELEMENT_ATTRIBUTE:
- ('GET', '/session/$sessionId/element/$id/attribute/$name'),
- Command.GET_ELEMENT_PROPERTY:
- ('GET', '/session/$sessionId/element/$id/property/$name'),
- Command.GET_ELEMENT_ARIA_ROLE:
- ('GET', '/session/$sessionId/element/$id/computedrole'),
- Command.GET_ELEMENT_ARIA_LABEL:
- ('GET', '/session/$sessionId/element/$id/computedlabel'),
- Command.GET_SHADOW_ROOT:
- ('GET', '/session/$sessionId/element/$id/shadow'),
- Command.FIND_ELEMENT_FROM_SHADOW_ROOT:
- ('POST', '/session/$sessionId/shadow/$shadowId/element'),
- Command.FIND_ELEMENTS_FROM_SHADOW_ROOT:
- ('POST', '/session/$sessionId/shadow/$shadowId/elements'),
- Command.GET_ALL_COOKIES: ('GET', '/session/$sessionId/cookie'),
- Command.ADD_COOKIE: ('POST', '/session/$sessionId/cookie'),
- Command.GET_COOKIE: ('GET', '/session/$sessionId/cookie/$name'),
- Command.DELETE_ALL_COOKIES:
- ('DELETE', '/session/$sessionId/cookie'),
- Command.DELETE_COOKIE:
- ('DELETE', '/session/$sessionId/cookie/$name'),
- Command.SWITCH_TO_FRAME: ('POST', '/session/$sessionId/frame'),
- Command.SWITCH_TO_PARENT_FRAME: ('POST', '/session/$sessionId/frame/parent'),
- Command.SWITCH_TO_WINDOW: ('POST', '/session/$sessionId/window'),
- Command.NEW_WINDOW: ('POST', '/session/$sessionId/window/new'),
- Command.CLOSE: ('DELETE', '/session/$sessionId/window'),
- Command.GET_ELEMENT_VALUE_OF_CSS_PROPERTY:
- ('GET', '/session/$sessionId/element/$id/css/$propertyName'),
- Command.EXECUTE_ASYNC_SCRIPT: ('POST', '/session/$sessionId/execute_async'),
- Command.SET_TIMEOUTS:
- ('POST', '/session/$sessionId/timeouts'),
- Command.GET_TIMEOUTS:
- ('GET', '/session/$sessionId/timeouts'),
- Command.W3C_DISMISS_ALERT:
- ('POST', '/session/$sessionId/alert/dismiss'),
- Command.W3C_ACCEPT_ALERT:
- ('POST', '/session/$sessionId/alert/accept'),
- Command.W3C_SET_ALERT_VALUE:
- ('POST', '/session/$sessionId/alert/text'),
- Command.W3C_GET_ALERT_TEXT:
- ('GET', '/session/$sessionId/alert/text'),
- Command.W3C_ACTIONS:
- ('POST', '/session/$sessionId/actions'),
- Command.W3C_CLEAR_ACTIONS:
- ('DELETE', '/session/$sessionId/actions'),
- Command.SET_WINDOW_RECT:
- ('POST', '/session/$sessionId/window/rect'),
- Command.GET_WINDOW_RECT:
- ('GET', '/session/$sessionId/window/rect'),
- Command.W3C_MAXIMIZE_WINDOW:
- ('POST', '/session/$sessionId/window/maximize'),
- Command.SET_SCREEN_ORIENTATION:
- ('POST', '/session/$sessionId/orientation'),
- Command.GET_SCREEN_ORIENTATION:
- ('GET', '/session/$sessionId/orientation'),
- Command.GET_NETWORK_CONNECTION:
- ('GET', '/session/$sessionId/network_connection'),
- Command.SET_NETWORK_CONNECTION:
- ('POST', '/session/$sessionId/network_connection'),
- Command.GET_LOG:
- ('POST', '/session/$sessionId/se/log'),
- Command.GET_AVAILABLE_LOG_TYPES:
- ('GET', '/session/$sessionId/se/log/types'),
- Command.CURRENT_CONTEXT_HANDLE:
- ('GET', '/session/$sessionId/context'),
- Command.CONTEXT_HANDLES:
- ('GET', '/session/$sessionId/contexts'),
- Command.SWITCH_TO_CONTEXT:
- ('POST', '/session/$sessionId/context'),
- Command.FULLSCREEN_WINDOW:
- ('POST', '/session/$sessionId/window/fullscreen'),
- Command.MINIMIZE_WINDOW:
- ('POST', '/session/$sessionId/window/minimize'),
- Command.PRINT_PAGE:
- ('POST', '/session/$sessionId/print'),
- Command.ADD_VIRTUAL_AUTHENTICATOR:
- ('POST', '/session/$sessionId/webauthn/authenticator'),
- Command.REMOVE_VIRTUAL_AUTHENTICATOR:
- ('DELETE', '/session/$sessionId/webauthn/authenticator/$authenticatorId'),
- Command.ADD_CREDENTIAL:
- ('POST', '/session/$sessionId/webauthn/authenticator/$authenticatorId/credential'),
- Command.GET_CREDENTIALS:
- ('GET', '/session/$sessionId/webauthn/authenticator/$authenticatorId/credentials'),
- Command.REMOVE_CREDENTIAL:
- ('DELETE', '/session/$sessionId/webauthn/authenticator/$authenticatorId/credentials/$credentialId'),
- Command.REMOVE_ALL_CREDENTIALS:
- ('DELETE', '/session/$sessionId/webauthn/authenticator/$authenticatorId/credentials'),
- Command.SET_USER_VERIFIED:
- ('POST', '/session/$sessionId/webauthn/authenticator/$authenticatorId/uv'),
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。