Tuesday, 25 August 2015

How to verify elements sorting order


Task: To verify that sorting works:
List view:  ASC and DESC order
Grid view: ASC and DESC order

Object: Shop page with products for sorting (demo).
http://live.guru99.com/index.php/mobile.html?

This site allows use parameters in url to perform sorting ( baseUrl?dir=desc&mode=list&order=name) so I don`t pay attention to clicking all this sorting buttons and just open url with necessary parameters.

public class SortingTest
{

    @Test
    public void userCanSortProductsByNameinList()
    {
     // go to page showing products in list mode
     open("http://live.guru99.com/index.php/mobile.html?mode=list");
     
     // get list with product names
     ArrayList expectedNames = getList(".//*[@class='product-name']/a");
     
     // sort() will return this list sorted in ascending order
     Collections.sort(expectedNames);
     
     // go to page showing products in list mode and sorted by name in ascending order (you can click buttons to get the same result)                      
     open("http://live.guru99.com/index.php/mobile.html?dir=asc&mode=list&order=name");
     
     // get another list with product names
     ArrayList actualNames = getList(".//*[@class='product-name']/a");
     
     // compare lists
     assertEquals(actualNames, expectedNames, "Not sorted by name.");
     
    }

    @Test
    public void userCanSortProductsByNameDescInList()
    {
     // go to page showing products in list mode
     open("http://live.guru99.com/index.php/mobile.html?mode=list");
     
     // get list with product names
     ArrayList expectedNames = getList(".//*[@class='product-name']/a");
     
     // sort() will return this list sorted in ascending order
     Collections.sort(expectedNames);
     
     // reverse() will return this list sorted in reverse => descending order
     Collections.reverse(expectedNames);     
        
     // go to page showing products in list mode and sorted by name in descending order (you can click buttons to get the same result)
     open("http://live.guru99.com/index.php/mobile.html?dir=desc&mode=list&order=name");
     
     // get another list with product names
     ArrayList actualNames = getList(".//*[@class='product-name']/a");
     
     // compare lists
     assertEquals(actualNames, expectedNames, "Not sorted by name in DESC order.");
     
    }

 public ArrayList getList(String xpath){

        /*
        * Find elements by Xpath locator and return ArrayList with elements text attributes.
        */  

     ArrayList list = new ArrayList();
     ElementsCollection elements = $$(By.xpath(".//*[@class='product-name']/a"));
 
     for (SelenideElement el : elements) {
          list.add(el.text());
     }
     return list;
 }

}

No comments:

Post a Comment