Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to sort a list and return the value in Robotframework

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'
    
like image 255
Poovin Avatar asked Oct 31 '18 06:10

Poovin


People also ask

How do you access elements in a list in Robot Framework?

If a variable value is a list or list-like, it is also possible to use as a list variable like @{EXAMPLE}. In this case individual list items are passed in as arguments separately. The second problem is that the syntax for accessing array elements only works outside the braces if you have a single index.


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

like image 64
Todor Minakov Avatar answered Sep 24 '22 17:09

Todor Minakov