如何将 Network.webSocketFrameReceived 保存到 Python 字典。使用 Pyppeteer 库进行 UI 测试

How to save Network.webSocketFrameReceived to Python dictionary. Using Pyppeteer library for UI test

我使用 Pyppeteer 库是因为有一个 Chrome 开发工具协议,我可以在 UI 测试中发送请求后收到 webSocketFrameReceived。我能够将套接字响应打印到终端,但这不是我想要的。我需要根据其中一个响应参数的状态(在屏幕截图 https://www.screencast.com/t/4wKSIcPjL9T 上标记),以不同的方式继续我的测试。我怎样才能做到这一点?如何处理套接字响应?将答案保存到 Python 词典?任何想法

import asyncio
from pyppeteer import launch
import time
import pprint
import json


async def test():
    args = ['--start-maximized']
    browser = await launch(
        args=args,
        headless=False,
        # args=['--no-sandbox'],
        # autoClose=False
        )

    page = await browser.newPage()
    await page.goto('https://example.com/')
    await page.setViewport({'width': 1920, 'height': 1080})

    # create CDP Session
    cdp = await page.target.createCDPSession()
    await cdp.send('Network.enable')

    #  Login
    await page.type('input[name=login]', 'login')
    await page.type('input[name=password]', 'password')
    await page.click('button[type=submit]')
    time.sleep(2)

    # Fill in the request
    await page.type('input[name=symbol]', 'ACY')
    await page.type('input[name=quantity]', '500')
    await page.click('selector')
    await page.type('selector', 'Text')
    await page.keyboard.press('Enter')
    await page.click('button[type=submit]')

    # Waiting for the status of "Pending"
    await page.waitForSelector('selector, {'timeout': 5000})

    def printResponse(response):
        pprint.pprint(response)

    # Calls printResponse when a websocket is received
    cdp.on('Network.webSocketFrameReceived', printResponse) 

    # Calls printResponse when a websocket is sent 
    # cdp.on('Network.webSocketFrameSent', printResponse)  

    await asyncio.sleep(20)
    # await page.close()
    # await browser.close()


asyncio.get_event_loop().run_until_complete(test_best_location_accept())
 def printResponse(response):
        pprint(type(response))

将显示响应类型,这些是 Python 词典