Search code examples
seleniumautomationrobotframework

Can i verify a list of xpaths at once?


I'm new using Robot Framework, so I created a Test Case to verify that a banch of elements exists on the target web page, so my question is instead of doing :

Page Should Contain Element    ${text_field1}
Page Should Contain Element    ${text_field2}
Page Should Contain Element    ${text_field3}
....

can I do a list with these xpaths and verify if this list exists on the web page at once ? is there any robotframework function to do that ? thanks


Solution

  • No, we don't have a keyword for multiple checking elements in pages. But we can create that logic in an user keyword. See below a fully working example.

    *** Settings ***
    Library           SeleniumLibrary    15    2
    Library           Collections
    
    *** Variables ***
    ${BROWSER}        Chrome
    @{elements}       xpath=//div[@id="error"]    id=searchInput    xpath=//button[@type="submit"]
    
    *** Test Cases ***
    Simple Test
        Open Browser    http://www.wikipedia.org    ${BROWSER}
        Comment    Wait Until Element Is Visible    id=searchInput    # search input field
        ${result}=    Page Should Contain Elements    ${elements}
        IF    not ${result}
            Fail    Page did not contained expected elements
        END
        Input Text    ${elements[0]}    Robot Framework
        Wait Until Element Is Visible    ${elements[1]}
        Click Button    ${elements[1]}
        Wait Until Page Contains    Robot Framework
        Capture Page Screenshot
        [Teardown]    Close All Browsers
    
    *** Keywords ***
    Page Should Contain Elements
        [Arguments]    ${elements}
        @{results}=    Create List
        FOR    ${element}    IN    @{elements}
            ${result}=    Run Keyword And Ignore Error    Page Should Contain Element    ${element}
            IF    "${result[0]}" == "FAIL"
                Append To List    ${results}    ${element}
            END
        END
        ${ret}=    Evaluate    len(${results}) == 0
        Return From Keyword If    ${ret}    True
        FOR    ${element}    IN    @{results}
            Log    Fail: Page should have contained element ${element} but it did not    WARN
        END
        Return From Keyword    False