pythonselenium-webdriverautomationrobotframeworkui-automation

How to read responses from request in robot selenium


Looking similar to cypress.intercept() in robot framework where we read api responses for get and post requests already happening for api testing from UI without additional calls. Not found any suitable docs, hence posting this to understand if its doable from robot or any helper library will do this.

cy.intercept('POST', '**/login').as('login-request');

cy.wait('@login-request', { responseTimeout: TIME_OUT.pageLoad }).then(
    (intercept) => {
      const { statusCode, body } = intercept.response;
      expect(statusCode).to.eq(200);
      expect(body).property('idToken').to.not.be.oneOf([null, undefined]);
      Cypress.env('idToken', body.idToken);
    }
  );

Solution

  • To achieve something similar to Cypress's cy.intercept() in the Robot Framework, you can use the Browser library which has more advanced capabilities, or you can work with a combination of the SeleniumLibrary and Python's requests library to handle API requests.

    If you want to capture network traffic, Robot Framework itself does not have a built-in feature to intercept network calls like Cypress. However, you can use the Browser library with the Playwright backend to intercept and modify HTTP requests and responses.

    Try something like this:

    pip install robotframework-browser
    rfbrowser init
    
    *** Settings ***
    Library    Browser
    
    *** Variables ***
    ${URL}    https://example.com/login
    
    *** Test Cases ***
    Intercept Login Request
        New Browser    headless=false
        New Context
        New Page    ${URL}
        Intercept Network    POST    **/login    CaptureLoginResponse
        Click    //button[@id='login']
        Wait For Condition    'response_captured'
    
    *** Keywords ***
    CaptureLoginResponse
        [Arguments]    ${request}
        IF    '${request.method}' == 'POST'
            Set Test Variable    ${response_captured}    ${request.response.body}
        END
    

    This example opens a browser and sets up a network interceptor for the login request. The keyword CaptureLoginResponse is used to capture and process the response of the intercepted network call.

    If you are limited to using SeleniumLibrary and cannot use the Browser library, you would have to rely on using browser developer tools or external proxies like mitmproxy to capture the requests, but this approach would require more setup and isn't as straightforward as using a built-in solution like Cypress's intercept.