Search code examples
automationrobotframework

Robot Framework: How do I direct my test results to an external file?


I have a functioning Robot Framework test that searches for identified elements in a list and then logs their presence or absence to the console. This has been working just fine for me so far. But now I need that console output directed to a file.

I have used the LOG keyword, I've used LOG MANY keyword and I've tried to also use APPEND TO FILE to get this done. I'm wondering at this point if my issue is the list/search logic itself. I can have individual commands logged or appended no problem. Even looking at the generated log.html file, only those basic log commands show up. Not the console output. Here's the keyword in question. And just to note, the search logic is sound. My problem is how to LOG what normally shows in the console to a file.

***Test Keyword***
Log            "TEST MENU ----"
${StaList}=    Create List   test1  test2  test3  test4  test5  test6
FOR    ${a}    IN    @{StaList}
    ${p}=  Run Keyword And Return Status   Page Should Contain Element   xpath=//* 
         [contains(text(), "${a}")]
    Run Keyword If   ${p}  Log  "(${a}) X"  ELSE  Log  "(${a}) "
END

When I run this with "Log To Console", this is what I get. A running list showing me if an element is present (with X) or absent (without X).

"TEST MENU ---- "(test1) X" "(test2) " "(test3) X"

This works fine if its just me running it. But I need this output sent to a text file to deliver to my team. I've been at this for a while now and need some help. Anybody have any ideas? Thanks so much!


Solution

  • Log to Console, does just that. It shows in Console but not in log.html. What you want is to duplicate your step to APPEND TO FILE, so you have in Console and in the file.

    You need to add \n or ${ENTER} to your output strings.

    EDIT: Here is a fully working example:

    *** Settings ***
    Suite Setup       Create and Open Page
    Suite Teardown    Close All Browsers
    Library           SeleniumLibrary
    Library           OperatingSystem
    
    *** Test Cases ***
    Example Test
        Verify Elements
    
    *** Keywords ***
    Verify Elements
        Log To Console    "TEST MENU ----"\n
        Create File    test_menu.txt    TEST MENU ----\n
        ${StaList}=    Create List    test1    test2    test3    test4    test5    test6
        FOR    ${a}    IN    @{StaList}
            ${p}=    Run Keyword And Return Status    Page Should Contain Element    xpath=//*[contains(text(), "${a}")]
            Run Keyword If    ${p}    Log To Console    "(${a}) X"\n
            ...    ELSE    Log To Console    "(${a}) "\n
            Run Keyword If    ${p}    Append To File    test_menu.txt    (${a}) X\n
            ...    ELSE    Append To File    test_menu.txt    (${a}) \n
        END
        Log    <a href="file://${CURDIR}/test_menu.txt">The result is test_menu.txt</a>    html=True
    
    Create and Open Page
        Create File    my_mock.html
        Append To File    my_mock.html    <HTML>\n \ \ \ <BODY>\n \ \ \ \ \ \ \ <p>test2</p>\n \ \ \ \ \ \ \ <p>test4</p>\n \ \ \ \ \ \ \ <p>test5</p>\n \ \ \ </BODY>\n</HTML>
        Open Browser    file://${CURDIR}/my_mock.html
    

    You should replace Run Keyword If by IF/ELSE/END blocks.