In my last post, I explained how can we validate sorting functionality in a web table using selenium web driver. Today I will discussing about testing filter functionality. To validate filter functionality you can follow the below steps:


     a) Make sure you have enough test data to test filter functionality
     b) Select the filter option from the drop down and click on filter button or link
     c) Read all the values from the column to be tested from the web table in a data structure like list from the first page of the result
     c) Click on the next page link and once next page is loaded do the same operation as in step b)
     d) Continue until you reach the last page
     e) Now check if the filtered expected value is present in the final list. If the final list contains any other value we can fail the test otherwise we will pass our test case.

You can use the same function to read the table column data into a list as explained in Part 1 of this series.

LinkedList<String> getTableColumnData(int column_no, String tableName)
{
        LinkedList<String> values = new LinkedList<String>();
        WebElement table = getWebDriver().findElement(By.className(tableName));
List<WebElement> rows = table.findElements(By.tagName("tr"));
Iterator<WebElement> i = rows.iterator();
while(i.hasNext())
{
                 WebElement row=i.next();
                 List<WebElement> columns= row.findElements(By.tagName("td"));
                 Iterator<WebElement> j = columns.iterator();
                 int count =0;
                 while(j.hasNext())
                {
                            WebElement column = j.next();
                            if(count == column_no)
                          {
                                    values.add( column.getText());
                                  break;
                          }
                           count ++;
               }

      }
        return values;
      } 


      You have to call this function in each page of your application until you reach the last page.
 
       Add a method to check  the size of this actual list. If list size is zero, you return that test data is not sufficient for validating filter functionality.

       Otherwise, following method can be used if actual list size is greater than 0 for checking if the column data is displayed as per the filter criteria:

       boolean isFilterStatusCorrect(LinkedList<String> actual,String expected)
      {
     Iterator<String> itr = actual.iterator();
     boolean result = true;
     if(actual.size()>0)
    {
         while(itr.hasNext())
        {
             if(!expected.equals( itr.next()))
                {
              result = false;
                   break;
                }
         }
     }
    return result;
     }
     
     Hope now you can validate both sorting and filtering functionality in a web page. Please share your questions and comments with me.