Jymiko
Jymiko

Reputation: 1

How do I get Chrome console logs with Robot Framework?

How can I get chrome dev_tool console logs with robot framework? I should check console for errors when I open new page. I think I have some logs with this code. This give only "Severe" level lines. Is there a way to get all chrome console logs (all levels)?

Resource           ../../Resources/LoginKeywordsAndVariables.robot
Library    SeleniumLibrary
Library    OperatingSystem
Library    Collections
Suite Setup    Valid Login
Library    json

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

*** Test Cases ***
Get logs
    Write logs

*** Keywords ***
Valid Login
    Open Browser    ${LOGIN URL}   ${BROWSER}    desired_capabilities=${capabilities}
    Input Username    xxx
    Input Login Password    xx
    Submit Credentials
    Element Text Should Be     xpath://a[@id="first_user_info_area"]    xxx

Get Browser Console Logs
    ${selenium}=    Get Library Instance    SeleniumLibrary
    ${webdriver}=    Set Variable     ${selenium._drivers.active_drivers}[0]
    ${logs}=    Evaluate    $webdriver.get_log('browser')
    RETURN   ${logs}

Write logs
    ${logs}=    Get Browser Console Logs
    #Log    ${logs}
    FOR    ${log}    IN    ${logs}
        Log    ${log}\n
    END

    # Set the desired file path
    ${file_path} =    Set Variable    consolelogs.txt
    # Create an empty file first if it doesn't exist
    Create File    ${file_path}
        FOR    ${log}    IN    ${logs}
        # Convert to JSON string with indentation
        ${log_json} =    Evaluate    json.dumps($log, indent=4)
        # Write the JSON string + newline
        Append To File    ${file_path}    ${log_json}\n
    END
    Log    Logs written to ${file_path}

Upvotes: 0

Views: 51

Answers (0)

Related Questions