How to sort a list and return the value in Robotframework

Viewed 6797

I have a use case for which I have to automate the following steps:

  1. Create an empty list
  2. Push data into the empty list
  3. Keep/save the original order in a variable
  4. Sort the original order
  5. Save the sorted list
  6. Reverse the sorted list and return value

    *** Settings ***
    Library  SeleniumLibrary
    Library  Collections
    
    *** Keywords ***
    Sort order verification
        @{username_list}=   Create List                        //creates an empty list
        @{get_name}=   Get WebElements     css=#userTable > tbody > tr> td:nth-child(1)
        :FOR    ${each}     IN      @{get_name}
        \  ${get_username}=  Get Text    ${each}
        \  Append To List  ${username_list}  ${get_username}   //pushes data into list in iteration
        ${original_order}=  Copy list  ${username_list}        //returns original order
        ${sorted_list}=  Sort List  ${original_order}          //sorts the list but returns none(nothing is saved in the variable
        ${reverse_sorted_list}=  Reverse List  ${sorted_list}  //returns AttributeError: 'NoneType' object has no attribute 'reverse'
    
1 Answers

The Sort List and Reverse List keywords modify the list in-place, e.g. they change the value of the target variable.

They also don't return anything - thus on the lines you've used them, you have assigned the value None to the variables, which led to the error.

You can read about this behavior in the Collections library documentation

Related