当前位置:   article > 正文

【自动化测试】postman实现ui自动化操作_postman可以测ui吗

postman可以测ui吗

说起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  

  1. {"capabilities": {
  2. "firstMatch": [{}],
  3. "alwaysMatch": {"browserName": "chrome",
  4. "platformName": "any",
  5. "goog:chromeOptions": {"extensions": [], "args": []}
  6. }},
  7. "desiredCapabilities": {"browserName": "chrome",
  8. "version": "",
  9. "platform": "ANY",
  10. "goog:chromeOptions": {"extensions": [], "args": []}}
  11. }

3.继续发送post请求在浏览器上输入jd网址,打开jd首页,

请求地址:url = 'http://127.0.0.1:9515/session/%s/url' %session_id

 4.前面这3步就实现了以下代码的功能,其他操作也是通过类似的请求,具体哪些操作对应哪些接口在源码里可以看到。如下图

  1. from selenium import webdriver
  2. driver = webdriver.Chrome()
  3. driver.get("https://www.jd.com")

明白这些之后,其实利用jmeter和request等都可以来实现 ,原理都是一样的,发请求给驱动,让驱动去执行对应的操作

  1. # 1.启动浏览器获取session_id
  2. import requests
  3. params = {"capabilities": {
  4. "firstMatch": [{}],
  5. "alwaysMatch": {"browserName": "chrome",
  6. "platformName": "any",
  7. "goog:chromeOptions": {"extensions": [], "args": []}
  8. }},
  9. "desiredCapabilities": {"browserName": "chrome",
  10. "version": "",
  11. "platform": "ANY",
  12. "goog:chromeOptions": {"extensions": [], "args": []}}
  13. }
  14. url='http://127.0.0.1:9515/session'
  15. res = requests.post(url=url,json=params) # 注意这里要用json传参数
  16. session_id = (res.json()["value"]["sessionId"]) #获取sessionId
  17. # 2.获取session_id后后续操作带上
  18. url = 'http://127.0.0.1:9515/session/%s/url' % session_id
  19. data = {'url': "http://www.jd.com"}
  20. res = requests.post(url=url, json=data)

        个人观点,虽然说不用selenium也能实现ui自动化,听起来很厉害的样子,了解原理之后其实也没什么,这些花里胡哨的东西没太多用,真去折腾起来费时费力,真的做自动化还是用专业的工具,这些都是学习过程,掌握原理后的实践。


关于如何获取接口地址?通过之前的selenium工作原理的源码分析,我们知道其实是通过发送一个http请求来启动浏览器的。selenium通过start_session()来启动一个浏览器,而start_session()实际上也是发送一个请求给驱动,请求地址可以在源码中找到,路径就是/session,其他接口地址源码中如下。

  1. self._commands = {
  2. Command.NEW_SESSION: ('POST', '/session'),
  3. Command.QUIT: ('DELETE', '/session/$sessionId'),
  4. Command.W3C_GET_CURRENT_WINDOW_HANDLE:
  5. ('GET', '/session/$sessionId/window'),
  6. Command.W3C_GET_WINDOW_HANDLES:
  7. ('GET', '/session/$sessionId/window/handles'),
  8. Command.GET: ('POST', '/session/$sessionId/url'),
  9. Command.GO_FORWARD: ('POST', '/session/$sessionId/forward'),
  10. Command.GO_BACK: ('POST', '/session/$sessionId/back'),
  11. Command.REFRESH: ('POST', '/session/$sessionId/refresh'),
  12. Command.W3C_EXECUTE_SCRIPT:
  13. ('POST', '/session/$sessionId/execute/sync'),
  14. Command.W3C_EXECUTE_SCRIPT_ASYNC:
  15. ('POST', '/session/$sessionId/execute/async'),
  16. Command.GET_CURRENT_URL: ('GET', '/session/$sessionId/url'),
  17. Command.GET_TITLE: ('GET', '/session/$sessionId/title'),
  18. Command.GET_PAGE_SOURCE: ('GET', '/session/$sessionId/source'),
  19. Command.SCREENSHOT: ('GET', '/session/$sessionId/screenshot'),
  20. Command.ELEMENT_SCREENSHOT: ('GET', '/session/$sessionId/element/$id/screenshot'),
  21. Command.FIND_ELEMENT: ('POST', '/session/$sessionId/element'),
  22. Command.FIND_ELEMENTS: ('POST', '/session/$sessionId/elements'),
  23. Command.W3C_GET_ACTIVE_ELEMENT: ('GET', '/session/$sessionId/element/active'),
  24. Command.FIND_CHILD_ELEMENT:
  25. ('POST', '/session/$sessionId/element/$id/element'),
  26. Command.FIND_CHILD_ELEMENTS:
  27. ('POST', '/session/$sessionId/element/$id/elements'),
  28. Command.CLICK_ELEMENT: ('POST', '/session/$sessionId/element/$id/click'),
  29. Command.CLEAR_ELEMENT: ('POST', '/session/$sessionId/element/$id/clear'),
  30. Command.GET_ELEMENT_TEXT: ('GET', '/session/$sessionId/element/$id/text'),
  31. Command.SEND_KEYS_TO_ELEMENT:
  32. ('POST', '/session/$sessionId/element/$id/value'),
  33. Command.UPLOAD_FILE: ('POST', "/session/$sessionId/se/file"),
  34. Command.GET_ELEMENT_TAG_NAME:
  35. ('GET', '/session/$sessionId/element/$id/name'),
  36. Command.IS_ELEMENT_SELECTED:
  37. ('GET', '/session/$sessionId/element/$id/selected'),
  38. Command.IS_ELEMENT_ENABLED:
  39. ('GET', '/session/$sessionId/element/$id/enabled'),
  40. Command.GET_ELEMENT_RECT:
  41. ('GET', '/session/$sessionId/element/$id/rect'),
  42. Command.GET_ELEMENT_ATTRIBUTE:
  43. ('GET', '/session/$sessionId/element/$id/attribute/$name'),
  44. Command.GET_ELEMENT_PROPERTY:
  45. ('GET', '/session/$sessionId/element/$id/property/$name'),
  46. Command.GET_ELEMENT_ARIA_ROLE:
  47. ('GET', '/session/$sessionId/element/$id/computedrole'),
  48. Command.GET_ELEMENT_ARIA_LABEL:
  49. ('GET', '/session/$sessionId/element/$id/computedlabel'),
  50. Command.GET_SHADOW_ROOT:
  51. ('GET', '/session/$sessionId/element/$id/shadow'),
  52. Command.FIND_ELEMENT_FROM_SHADOW_ROOT:
  53. ('POST', '/session/$sessionId/shadow/$shadowId/element'),
  54. Command.FIND_ELEMENTS_FROM_SHADOW_ROOT:
  55. ('POST', '/session/$sessionId/shadow/$shadowId/elements'),
  56. Command.GET_ALL_COOKIES: ('GET', '/session/$sessionId/cookie'),
  57. Command.ADD_COOKIE: ('POST', '/session/$sessionId/cookie'),
  58. Command.GET_COOKIE: ('GET', '/session/$sessionId/cookie/$name'),
  59. Command.DELETE_ALL_COOKIES:
  60. ('DELETE', '/session/$sessionId/cookie'),
  61. Command.DELETE_COOKIE:
  62. ('DELETE', '/session/$sessionId/cookie/$name'),
  63. Command.SWITCH_TO_FRAME: ('POST', '/session/$sessionId/frame'),
  64. Command.SWITCH_TO_PARENT_FRAME: ('POST', '/session/$sessionId/frame/parent'),
  65. Command.SWITCH_TO_WINDOW: ('POST', '/session/$sessionId/window'),
  66. Command.NEW_WINDOW: ('POST', '/session/$sessionId/window/new'),
  67. Command.CLOSE: ('DELETE', '/session/$sessionId/window'),
  68. Command.GET_ELEMENT_VALUE_OF_CSS_PROPERTY:
  69. ('GET', '/session/$sessionId/element/$id/css/$propertyName'),
  70. Command.EXECUTE_ASYNC_SCRIPT: ('POST', '/session/$sessionId/execute_async'),
  71. Command.SET_TIMEOUTS:
  72. ('POST', '/session/$sessionId/timeouts'),
  73. Command.GET_TIMEOUTS:
  74. ('GET', '/session/$sessionId/timeouts'),
  75. Command.W3C_DISMISS_ALERT:
  76. ('POST', '/session/$sessionId/alert/dismiss'),
  77. Command.W3C_ACCEPT_ALERT:
  78. ('POST', '/session/$sessionId/alert/accept'),
  79. Command.W3C_SET_ALERT_VALUE:
  80. ('POST', '/session/$sessionId/alert/text'),
  81. Command.W3C_GET_ALERT_TEXT:
  82. ('GET', '/session/$sessionId/alert/text'),
  83. Command.W3C_ACTIONS:
  84. ('POST', '/session/$sessionId/actions'),
  85. Command.W3C_CLEAR_ACTIONS:
  86. ('DELETE', '/session/$sessionId/actions'),
  87. Command.SET_WINDOW_RECT:
  88. ('POST', '/session/$sessionId/window/rect'),
  89. Command.GET_WINDOW_RECT:
  90. ('GET', '/session/$sessionId/window/rect'),
  91. Command.W3C_MAXIMIZE_WINDOW:
  92. ('POST', '/session/$sessionId/window/maximize'),
  93. Command.SET_SCREEN_ORIENTATION:
  94. ('POST', '/session/$sessionId/orientation'),
  95. Command.GET_SCREEN_ORIENTATION:
  96. ('GET', '/session/$sessionId/orientation'),
  97. Command.GET_NETWORK_CONNECTION:
  98. ('GET', '/session/$sessionId/network_connection'),
  99. Command.SET_NETWORK_CONNECTION:
  100. ('POST', '/session/$sessionId/network_connection'),
  101. Command.GET_LOG:
  102. ('POST', '/session/$sessionId/se/log'),
  103. Command.GET_AVAILABLE_LOG_TYPES:
  104. ('GET', '/session/$sessionId/se/log/types'),
  105. Command.CURRENT_CONTEXT_HANDLE:
  106. ('GET', '/session/$sessionId/context'),
  107. Command.CONTEXT_HANDLES:
  108. ('GET', '/session/$sessionId/contexts'),
  109. Command.SWITCH_TO_CONTEXT:
  110. ('POST', '/session/$sessionId/context'),
  111. Command.FULLSCREEN_WINDOW:
  112. ('POST', '/session/$sessionId/window/fullscreen'),
  113. Command.MINIMIZE_WINDOW:
  114. ('POST', '/session/$sessionId/window/minimize'),
  115. Command.PRINT_PAGE:
  116. ('POST', '/session/$sessionId/print'),
  117. Command.ADD_VIRTUAL_AUTHENTICATOR:
  118. ('POST', '/session/$sessionId/webauthn/authenticator'),
  119. Command.REMOVE_VIRTUAL_AUTHENTICATOR:
  120. ('DELETE', '/session/$sessionId/webauthn/authenticator/$authenticatorId'),
  121. Command.ADD_CREDENTIAL:
  122. ('POST', '/session/$sessionId/webauthn/authenticator/$authenticatorId/credential'),
  123. Command.GET_CREDENTIALS:
  124. ('GET', '/session/$sessionId/webauthn/authenticator/$authenticatorId/credentials'),
  125. Command.REMOVE_CREDENTIAL:
  126. ('DELETE', '/session/$sessionId/webauthn/authenticator/$authenticatorId/credentials/$credentialId'),
  127. Command.REMOVE_ALL_CREDENTIALS:
  128. ('DELETE', '/session/$sessionId/webauthn/authenticator/$authenticatorId/credentials'),
  129. Command.SET_USER_VERIFIED:
  130. ('POST', '/session/$sessionId/webauthn/authenticator/$authenticatorId/uv'),
  131. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/600153
推荐阅读
相关标签
  

闽ICP备14008679号