Sven van den Boogaart
Sven van den Boogaart

Reputation: 12331

Robot Framework get background call with Robot Framework/selenium

I'm testing an web application with selenium, what I want to check is if there are calls done in the background (post,get). e.g. I load google.com and in the developer options I can see that it does some requests.

I looked into the documentation for the selenium library in Robot Framework but cant find an option. Is it possible to get the requests done? I also found selenium wire that does exactly what I want, but in all the examples they use a driver object, Is it possible to get the driver object used by Robot Framework/selenium?

e.g.

*** Settings ***
Documentation     Simple example using SeleniumLibrary.
Library           SeleniumLibrary

*** Variables ***
${LOGIN URL}      http://localhost:7272
${BROWSER}        Chrome

*** Test Cases ***
Valid Login
    Open Browser To Login Page
    Input Username    demo
    Input Password    mode
    Submit Credentials
    Welcome Page Should Be Open
    Get call to some-site should be done <---- what i want.
    [Teardown]    Close Browser

*** Keywords ***
Open Browser To Login Page
    Open Browser    ${LOGIN URL}    ${BROWSER}
    Title Should Be    Login Page

Input Username
    [Arguments]    ${username}
    Input Text    username_field    ${username}

Input Password
    [Arguments]    ${password}
    Input Text    password_field    ${password}

Submit Credentials
    Click Button    login_button

Welcome Page Should Be Open
    Title Should Be    Welcome Page

I tried the following based on Bence Kaulics's answwer

from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

def get_logs2(driver):
# enable browser logging
    #d = DesiredCapabilities.CHROME
    #d['goog:loggingPrefs'] = { 'browser':'ALL' }
    #driver = webdriver.Chrome(desired_capabilities=d)

    # load the desired webpage
    #driver.get('http://34.90.50.21/')
    #driver.get(driver.current_url)
    a = driver.get_log('browser')

    # print messages
    for entry in driver.get_log('browser'):
        print(entry)
    print("finished")
    return a

the robot part I do is:

*** Keywords ***
   Get Logs2
        [Arguments]     ${arg1}
        ${seleniumlib}=    Get Library Instance    SeleniumLibrary
        Log    ${seleniumlib._drivers.active_drivers}[0]
        Get Logs2   ${seleniumlib._drivers.active_drivers}[0]

But to no avail

Upvotes: 1

Views: 1647

Answers (1)

Bence Kaulics
Bence Kaulics

Reputation: 7291

In the SeleniumLibrary/init.py line 487, you can see that SeleniumLibrary has a member called _drivers which is an instance of class WebDriverCache. If you browse further you can see that this WebDriverCache has an active_drivers property. This is what you need.

To retrieve it you can use the Get Library Instance keyword that will return you the object of your SeleniumLibrary instance. The rest is accessible by the extended variable syntax.

You also have to set the appropriate browser capabilities to enable logging in the web driver.

from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

def get_chrome_browser_logging_capability():
    d = DesiredCapabilities.CHROME
    d['goog:loggingPrefs'] = { 'browser':'ALL' }
    return d

def get_logs2(driver):
    return driver.get_log('browser')

Here is a quick example:

*** Settings ***
Library    SeleniumLibrary
Library    driver.py

*** Test Cases ***
Browser Log Cases
    ${capability}=    Get Chrome Browser Logging Capability
    Open Browser    https://stackoverflow.com    Chrome    desired_capabilities=${capability}
    Go To    https://stackoverflow.com/q/66155774/3820025
    ${seleniumlib}=    Get Library Instance    SeleniumLibrary
    ${message} =    Get Logs2   ${seleniumlib._drivers.active_drivers}[0]
    [Teardown]    Close All Browsers

With these I can see log entries returned.

enter image description here

Upvotes: 1

Related Questions