Search code examples
browserrobotframework

Sort and compare


I want to check the "sort" feature when user click the column header to sort the list.

I am planning to do it in steps:

  1. Get the current list: below script iterate the table row by row.

        *** Settings ***
        Library  Browser
        Library  String
        Library  Collections
        Resource            ../Resources/BrowserFunctions.robot
        Suite Setup         Start New Browser
        Suite Teardown      Close Browser
        
        *** Test Cases ***
        001-Models
            Open New Page To Server
            Navigate To Models Module
    
         ${elements} =    Get Elements    table tr td:nth-child(2)
         ${modelList}=    Create List
         FOR    ${elem}    IN    @{elements}
         ${text}=    Get Text    ${elem}
         ${new elem}=     Set Variable    ${text}
         Append To List    ${modelList}    ${new elem}
         END
         Log To Console    ${modelList}

  1. How do I sort the list (this will be stored as ${expected} variable).

  2. User click on the column header

  3. Repeat #1 get the list and stored it as ${actual} variable.

  4. ${expected} == ${actual} then PASS


Solution

  • *** Test Cases ***
    Test_Sort_And_Compare_Asc
        [Tags]    compareAsc
        ${expected}    Create List    z  x  y
        ${Actual}      Create List    x  y  z
        Sort List    ${expected}
        # Sort List    ${Actual}
        Lists Should Be Equal    ${expected}    ${Actual}    
    
    Test_Sort_And_Compare_Desc
        [Tags]    compareDesc
        ${expected}    Create List    x  y  z
        ${Actual}      Create List    z  y  x
        Reverse List    ${expected}
        Lists Should Be Equal    ${expected}    ${Actual} 
    
    Test_Compare
        [Tags]    compare
        ${expected}    Create List    z  x  y
        ${Actual}      Create List    x  y  z
        Lists Should Be Equal    ${expected}    ${Actual}    
    

    You can use 3rd case which is suitable for you, As you have sorted list in expected variable and actual list in actual variable. Because you are fetching sorted list from application in expected variable.