0

I use Pyppeteer library because there is a Chrome Dev Tools protocol and I can receive webSocketFrameReceived after sending a request in UI test. I was able to print the socket response to the terminal, but that's not what I want. I need, depending on the status of one of the response parameters (marked on the screenshot https://www.screencast.com/t/4wKSIcPjL9T), to continue my test differently. How can I do this? How to deal with socket response? save answer to Python dictionary? Any idea

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())
  • It is a good question, how to continue with a test using data from websocket response. The problem is that the callback function is synchronous, and you can't evaluate something like `await page.click('a')` from the callback. It will be good to find an answer someday. – goodgrief Jun 11 '23 at 19:31

1 Answers1

-1
 def printResponse(response):
        pprint(type(response))

will display the type of response, these are Python dictionaries