Export test results to excel using Robot Framework and Excellibrary

2.6k views Asked by At

I'm new to Robotframework and I've been trying to export the result of my test to excel, but I couldn't get the correct loop for writing the data to excel. the logic works that every time the element is present in the page, it will be log to the console. But at the same time, I want it to be written in excel.

With the current code, it's only failing, it couldn't recognize ${my_data}

I just put ..... to represent the codes that are not mentioned.

*** Test Cases ***
Check data of the web
    Open browser    ${url}    chrome
    : FOR    ${url}    IN    @{url_list}
    \    Go To    ${url}
    \    ${searched_script} =    Get Source
    \    Run Keyword And Continue On Failure    Should Contain    ${searched_script}    ${sample}
    \    Log to Console    ${url}
    \    @{site_data} =    Get WebElements 
    \    Loop data    @{site_data}
    \   Push all result to excel
    Close Browser

*** Keywords ***
Loop data
    [Arguments]    @{site_data}
    : FOR    ${site_data}    IN    @{site_data}
    \    Log    ${site_data}
    \    ${my_data}=    Get Element Attribute    ${site_data}    my_data_sample
    \    Continue For Loop If    $my_data is None
    \    Run Keyword And Continue on Failure    Should Contain    ${my_data}    hello_world
    \    Log To Console    ${my_data}  

Push all result to excel
    Create excel document doc_id=docname
    Write excel rows    1   0   @{my_data}  sheet    #my_data here is not passing the data from the loop
    Save Excel Document     test.xlsx

1

There are 1 answers

0
Michele Belotti On

the problem in your example is that the variable is local to your test case, not to the test suite.

you need to add the following command to promote the value to test suite

Set Suite Variable ${my_data}