What will be a good test strategy to test sorting?

I have a scenario where when clicking on a sort, i.e. sort the make/model from Z to A, then the listing should be sorted alphabetically from Z to A.

How can I test this? I use TestNG and Selenium using page object model.

When I do click on, sort make/model from Z to A, then the page reappears with one make, e.g a bunch of Volvo's because there are so many of them and no other make appears on the page.

4 Answers 4


My rule of thumb when designing UI tests is to do a basic check that the UI works, but coverage of the exact logic really should be shifted further down the testing pyramid (this will save execution time, plus lower level tests are better suited to it). A simple check of the first and last elements should suffice. That said, if you don't have good coverage on a lower level, some coverage is better than none.

First, let's say you can retrieve a list of elements in your page object:

By stuffSelector = By.id("stuff-");

public List<WebElement> getStuffList(){
   return getDriver().findElements(stuffSelector);

// alternative java8 solution which makes it easier dealing with strings instead of webelements
public List<String> getStuffStringList() {
    return getDriver().findElements(stuffSelector).stream()

If you have pagination, you'll have to loop whilst there are more pages and add it to the list as you go:

public List<WebElement> getPaginatedStuffList() {

    List<WebElement> stuff = getStuffList();
    boolean morePages = areThereMorePages(); // implement this yourself

    while(morePages) {
        morePages = areThereMorePages(); 

    return stuff;

What you can then do is get an unsorted list before it's sorted, hit your UI sort, and grab a second sorted list.

Then sort your first list through code, and make sure that it's the same as your second list.

List<WebElement> list1 = somePage.getStuffList();
List<WebElement> list2 = somePage.getStuffList();

// sorts list1 according to a comparator - I've simply compared the texts of the elements but you may wish to do something more complex..
list1.sort((e1, e2) -> e1.getText().compareTo(e2.getText()));

// feel free to iterate over the whole list if you really want

Honestly, this is pretty verbose, though. If you have access to the excellent AssertJ libraries, you can assert a list is sorted using a comparator directly.

List<WebElement> stuff = somePage.getStuffList();    
assertThat(stuff).isSortedAccordingTo((e1, e2) -> e1.getText().compareTo(e2.getText()));

Strategy is automation. Could be:

  • Brutal force: read all values on all pages to a field variable and compare each with it's neighbor. Simple task for test automation.
  • Optimised: sequentional read of the parameter and compare with the previous and next value, regardless of independently switching to the next page, if you reach end of the list.
  • Avoidance: You will not test it at all as there are SQL statements with ORDER BY and you will test the database then. You will test the sorting manually.
  • First vs last element: You will test just comparison of the first and last element in the list, so you are testing, if the sorting buttons are working, not if the sorting is right for each element.

You will need to think about the very same values in the whole list (no order then) so use >=, <= intead of >, <

You will make use of following:

  • selenium.storeXpathCount(xpath, variableName)
  • some cycle
  • I like the avoidance strategy - if the sorting and pagination is done on the server, you'll need a test there anyway. Then you only really need a test for the display if you want assurance that it displays it in the order it said it would.
    – corsiKa
    Sep 17, 2015 at 18:49
  • @corsiKa: yes, but with the risk, that GUI elements which are doing sorting are not tested. Again, if this is a boxed software element or standard enough, you do not need to do it, because you are testing the boxed software or standard element then. Then is better to test it just once manually.
    – Dee
    Sep 18, 2015 at 14:37

If all the results are on the same page, it should be pretty straightforward. You'll need to fetch the cell you've sorted, probably require a comparator of some kind, and just verify that each cell is ascending (or descending, as the case may be) from the previous cell.

If you're using pagination it's going to be harder. You would need to go through each page sequentially and verify not only that each page is consistent, but also that the first element of each page is consistent with the last element of the previous page. This isn't an impossible task, clearly, but it does complicate the test.

But like any sort verification, your basic strategy will be to start at the second cell and compare to the first cell, then move to the third and compare to the second, and so on.


You said you're using the Page object model to separate your Web code from your orchestration and assertions. Let's not lose that good design principal when designing this test.

If you haven't already, abstract a table on your Web page into a collection (eg string[]) on your page object. Find out how to check a string[] is alphabetically ordered, which your assertion code will have to do.

The complex part is that the data lives on multiple pages. You're going to have to move through each page, appending string[]'s, until you have all the data and can make assertion. It's a close run thing, but I'm leaning towards saying this navigate-append goes in the page object, as pageanation of this data seems more Web related than an interesting characteristic of the info on the page.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.