Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
5.0k views
in Technique[技术] by (71.8m points)

python - How to save data from the response of the web socket after sending a request in the UI test performed in pytest

UI test is performed in pytest

from page_objects import MainPage, LoginPage


def test_locate_best_accept(browser):

    LoginPage(browser) 
        .login_user(login='****_user', password="******")
    MainPage(browser) 
        .select_symbol(symbol='ACY') 
        .select_quantity_symbol(quantity=600) 
        .select_destination_best() 
        .click_request_button() 
        .accept_request()

After performing the step .click_request_button() I need to get and save in the python dictionary data from response WebSockets. How to implement it? I am new to automated testing.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I managed to get the WebSocket response in the terminal using the Puppeteer library

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...