[Automated testing] postman realizes ui automation operation

Speaking of postman, the first reaction is that it is not used for interface testing, can it also be used for UI automation?

Yes, ui automation is inseparable from three things, test scripts, drivers, and browsers.

Understand automation as taking a taxi, which has three roles:

  • Passengers: Tell the driver where to go and how to go
  • Driver: Drive according to the requirements of passengers and send passengers to their destinations
  • Car: complete the driving (turning, forward, backward, etc.) according to the driver's manipulation, and reach the destination

In this analogy:

        The test script is equivalent to the passenger, the browser driver is equivalent to the driver, and the browser is the taxi

After understanding this relationship and the working principle of selenium, you can understand how postman can be used for automation.

Please read this article first: How selenium works

Generally, we use the selenium package for automation, combined with python or java to write scripts. This is equivalent to communicating with the driver in Mandarin, which is common throughout the country. Suddenly one day you take a taxi and meet a fellow, so you start talking. If you don't speak the native dialect, you can still communicate and reach your destination.

Just look at the example how to implement

1. Execute start chromedriver under cmd to start a webdriver.exe (provided that the corresponding version driver has been configured)

After startup, the default port is 9515, Only local connections are allowed, localhost:9515, the next step is to get the browser interface address to start a browser

2. Initiate a post request in postman to start the browser, address: 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. Continue to send the post request, enter the jd URL on the browser, open the jd homepage,

Request address: url = 'http://127.0.0.1:9515/session/%s/url' %session_id

 4. The previous three steps have realized the functions of the following code, and other operations are also performed through similar requests. The specific operations corresponding to which interfaces can be seen in the source code. As shown below

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

After understanding these, in fact, it can be realized by using jmeter and request, etc. The principle is the same, send a request to the driver, and let the driver perform the corresponding operation

# 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)

        From my personal point of view, although UI automation can be realized without selenium, it sounds very powerful. After understanding the principle, it is actually nothing. These bells and whistles are not very useful. It is time-consuming and labor-intensive to really do automation. Professional Tools, these are the learning process, practice after mastering the principles.


How to get the interface address? Through the source code analysis of the previous working principle of selenium, we know that the browser is actually started by sending an http request. Selenium starts a browser through start_session(), and start_session() actually sends a request to the driver. The request address can be found in the source code, the path is /session, and the source code of other interface addresses is as follows.

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'),
        }

Guess you like

Origin blog.csdn.net/MrChenLen/article/details/120827380