How to sort 6 or more columns in a grid

I have a table that has 6 columns to sort, sort works when we click on the sort button present at the header of every column

Once we click on sort button, columns will sort the items in such a way that blank item will be at top and then the alphabetical order will start from A-z, for numbers it will be numeric sorting.

in the above screen shot there are only 4 columns, but in few other tables there could be 6 columns as well

@dominique Can you share the HTML which contains the columns?

Hi,

According to your screen, you have a change in the symbol of the sorted columns, so you should verify if it has an attribute in this case (for example sort with a ‘up’ or ‘down’, ‘asc’ or ‘desc’ value).

Then you could get info depending of the columns names and click until the attribute is as expected.
Something like this:

Table Sort
     [Arguments]    ${col_name}    ${sort_type}
     VAR    ${isSorted}    ${EMPTY}
     WHILE    '${isSorted}' != '${sort_type}'
         Click Element    //div[@sort and text()='${col_name}']
         ${isSorted}    Get Element Attribute    //div[@sort and text()='${col_name}']    sort
       END

Of course check the structure of the table and exact xpath you can use.

Regards
Charlie

1 Like

Hi @falcon030 not sure when I’m sending the HTML, it it coming out blank, please find the screen shot in the reply section

Hi Charlie, I also want to check in the first 5 and last 5 rows of the column for the elements to be sorted alphabetically or numerically:

For some reason, I’m not able to post the HTML structure here, so sending a photo of it:

Hi Dominique,

Do these sortamountupalticon change to sortamountdownalticon when you change the sort direction? That would be my guess, if so apply that to @CharlieScene’s example.

sortamountupalticon
sortamountdownalticon

sort amount up alt icon
sort amount down alt icon

Dave.

@dominique Can you also share the HTML code where the names of the columns are visible?

Hi @falcon030 here is the HTML code with the names of columns that visible, I have removes some lines from the middle to capture in screenshot:

Hi @damies13,

I also need to check if elements present in first page for each columns are sorted or not, I need to do sorting with actual elements present in the cells for each element in the first page, when we click on sort the first few sorted elements comes as blank and the rest will start alphabetically from A to Z, can you also help in validating this.

Dominique

@dominique I don’t see the names of the columns in the HTML code.
Is it very different than this?

Hi @falcon030,

In the above snap if you look at attribute “field” which is highlighted that would be the column name for all the columns.

Dominique

@dominique That is not the same as text that is visible before the sort and filter icons. The value of the field attribute isn’t been displayed.

@dominique Yes that’s exactly what I needed :smiley:. Now what do you exactly want to do? Sort a column based on there name ascending or descending?

1 Like

@dominique I think that you can use the following code. The keyword takes 2 embedded keywords: the visible name of the column and the sort order (Ascending / Descending).

sorttest
    New Browser     headless=False
    New Context
    New Page        url=Haven't the faintest idea
    Sort Column "FI Name" By "Descending" Order
    Sleep           5s


*** Keywords ***
Sort Column "${column_name}" By "${sort_order}" Order
    [Arguments]         ${occurrence}=1
    ${sort_order}       Convert To Lowercase    ${sort_order}
    IF  "${sort_order}" == "ascending"
            VAR    ${sort_icon}    sortamountupalticon
    END
    IF  "${sort_order}" == "descending"
            VAR    ${sort_icon}    sortamountdownicon
    END
    ${sorted}           Get Element States
    ...                 :nth-match(th[role="columnheader"]:has-text("${column_name}"):visible,${occurrence}):has(${sort_icon})
    ...                 then
    ...                 bool(value & visible)
    WHILE    ${sorted} == ${False}
        Click           :nth-match(th[role="columnheader"]:has-text("${column_name}"):visible,${occurrence})
        Log To Console  Matching= :nth-match(th[role="columnheader"]:has-text("${column_name}"):visible,${occurrence}):has(${sort_icon})
        ${sorted}       Get Element States
        ...             :nth-match(th[role="columnheader"]:has-text("${column_name}"):visible,${occurrence}):has(${sort_icon})
        ...             then
        ...             bool(value & visible)
    END
1 Like

Yes @falcon030, exactly once user clicks on sort button then I want to sort based on ascending or descending

Hi @falcon030,

When I use the keyword mentioned above, I get “Undefined Variable” for sort_icon, you can see it in the screenshot below,

can I declare the variable in some different way.

@dominique What are the versions of Robot Framework and Browser Library that you are using?