2

I am trying to read the console output of a webpage, especially I need the POST-GET-PUT ajax calls, with RF and Selenium. I have found some help online but I cannot seem to make it work. the python script I have is:

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(driver.current_url)
    a = driver.get_log('browser')

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

I call this script from RF, after having done some operations on the webpage. So I need to pass to this function the page exactly how it is after the actions I took. To do that I do:

    ${seleniumlib}=    Get Library Instance    SeleniumLibrary
    Log    ${seleniumlib._drivers.active_drivers}[0]
    ${message} =    Get Logs2   ${seleniumlib._drivers.active_drivers}[0]

I get as a result and empty message, but I know console is not empty. Can you help? Thanks.

Anna Ramolini
  • 55
  • 2
  • 8
  • Does this answer your question? [Robot Frameworkget background call with Robot Framework/selenium](https://stackoverflow.com/questions/66155774/robot-frameworkget-background-call-with-robot-framework-selenium) You code suspiciously looks the same, which is not a problem but it simply makes your question a duplicate. – Bence Kaulics Feb 17 '21 at 18:22
  • Yes, I actually had my friend Sven ask that question because I did not have an account. I think it is good to close that question and follow up on this one. I tried with robot.api logger but since the string is empty it won't print anything. – Anna Ramolini Feb 18 '21 at 08:59
  • I have updated my answer at the other question. The other question cannot be deleted because I have already answered it, this one does not have answers so you can delete this one. – Bence Kaulics Feb 18 '21 at 11:37
  • Great, thank you, the solution worked but now I have a new problem. the console only logs the ajax calls when the "Log XMLHttpRequests" is checked in the developer tools settings. by default it is not, so I do not see the calls in the log. Do you know how to have that box clicked in the "settings" from RF? Thank you. – Anna Ramolini Feb 18 '21 at 15:47

1 Answers1

3

Here is a solution using entirely Robot Framework, no additional user library. The logic is the same.

  1. Set the correct browser capabilities to enable logging.
  2. Then use the Get Library Instance keyword to retrieve the webdriver instance.
  3. Call the get_log('browser') on the webdriver instance.

*** Settings ***
Library    SeleniumLibrary

*** Variables ***
&{browser logging capability}    browser=ALL
&{capabilities}    browserName=chrome    version=${EMPTY}    platform=ANY    goog:loggingPrefs=${browser logging capability}

*** Test Cases ***
Browser Log Cases
    Open Browser    https://stackoverflow.com    Chrome    desired_capabilities=${capabilities}
    ${log entries}=    Get Browser Console Log Entries    
    Log    ${log entries}
    [Teardown]    Close All Browsers
    
    
*** Keywords ***
Get Browser Console Log Entries
    ${selenium}=    Get Library Instance    SeleniumLibrary
    ${webdriver}=    Set Variable     ${selenium._drivers.active_drivers}[0]
    ${log entries}=    Evaluate    $webdriver.get_log('browser')
    [Return]    ${log entries}
Bence Kaulics
  • 7,066
  • 7
  • 33
  • 63