Friday 29 May 2020

How to compare displayed drop down select values(actual values displayed) are matching with user expected drop down values for web data tables in java-selenium ? | How to automate page length options for data tables in selenium ?

Hi, in this post, we will learn about how to auto compare the displayed drop down select values to the user expected drop down values.

i.e , compare "actual values displayed" to the "user expected values"  for the drop down select for a web data table.

Zoom-In by tapping the image:

The approach is as follows: 
1) Declare a list for user expected values of an integer(later in the code convert to sting)
     or String type.
2) Locate the drop down select element on the web using xpath locator technique.
3) Create an object of "Select" class with the argument of  drop down select element. (from #2)
4) Store the values of actual values displayed in another list of type WebElement
5) Compare the two lists  and confirm the equality.

user expected values
List<Integer> expectedDropDownValues = new ArrayList<Integer>()
{ 
     { 
          add(10); 
          add(25); 
          add(50); 
          add(100);
      } 
};

actual values displayed
WebElement entriesDropDownLocator = driver.findElement(By.xpath("//select[@name='example_length']"));
  
Select entriesDropDown = new Select(entriesDropDownLocator);

List<WebElement> actualDropDownValues = entriesDropDown.getOptions();

Compare the two lists
for(int i=0;i<actualDropDownValues.size();i++) {
   
     if(actualDropDownValues.get(i).getText().equals(expectedDropDownValues.get(i).toString())) {
      
      System.out.println("Value Matching :"+"Actual List Value="+actualDropDownValues.get(i).getText()+" And Expected Value="+expectedDropDownValues.get(i));
     }else {
      System.out.println("Value Not Matching :"+"Actual List Value="+actualDropDownValues.get(i).getText()+" And Expected Value="+expectedDropDownValues.get(i));
     }
  }

CompareDisplayedDropdownSelectValuesWithActualValues.java
package selenium.datatables;

import java.util.ArrayList;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.Select;


public class CompareDisplayedDropdownSelectValuesWithActualValues {

 public static WebDriver driver;

 public void compareDispalyedRowCountToActualRowCount() throws Exception {
 
  @SuppressWarnings("serial")
  List<Integer> expectedDropDownValues = new ArrayList<Integer>()
         { 
            { 
                add(10); 
                add(25); 
                add(50); 
                add(100);
            } 
         };
         
  System.out.println("Expected dropdown values for accounts table");
         
  for (Integer expectedOptions : expectedDropDownValues) {
          System.out.println(expectedOptions.toString());
   }
            
  WebElement entriesDropDownLocator = driver.findElement(By.xpath("//select[@name='example_length']"));
  
  Select entriesDropDown = new Select(entriesDropDownLocator);
  List<WebElement> actualDropDownValues = entriesDropDown.getOptions();
  
  System.out.println("Actual dropdown values from accounts table");
  for (WebElement actualValues : actualDropDownValues) {
   System.out.println(actualValues.getText());
  }
  System.out.println("Compare the actual values with the expected values for dropdown");
    
  for(int i=0;i<actualDropDownValues.size();i++) {
   
     if(actualDropDownValues.get(i).getText().equals(expectedDropDownValues.get(i).toString())) {
      
      System.out.println("Value Matching :"+"Actual List Value="+actualDropDownValues.get(i).getText()+" And Expected Value="+expectedDropDownValues.get(i));
     }else {
      System.out.println("Value Not Matching :"+"Actual List Value="+actualDropDownValues.get(i).getText()+" And Expected Value="+expectedDropDownValues.get(i));
     }
  }
 }
 
 public void closeDriver() {
  driver.close();
 }
 public void quitDriver() {
  driver.quit();
 }
 
 public static void main(String[] args) throws Exception {
  
   CompareDisplayedDropdownSelectValuesWithActualValues c = new CompareDisplayedDropdownSelectValuesWithActualValues();
   
   System.setProperty("webdriver.chrome.driver", "D:\\006_trainings\\chromedriver.exe");
   System.setProperty("webdriver.chrome.silentOutput","true" );
  
   driver = new ChromeDriver();
   driver.get("https://datatables.net/examples/basic_init/zero_configuration.html");
   driver.manage().window().maximize();
   c.compareDispalyedRowCountToActualRowCount();
   
   c.closeDriver();
   c.quitDriver();
 }
}

Console Log:
Expected dropdown values for accounts table
10
25
50
100
Actual dropdown values from accounts table
10
25
50
100
Compare the actual values with the expected values for dropdown
Value Matching :Actual List Value=10 And Expected Value=10
Value Matching :Actual List Value=25 And Expected Value=25
Value Matching :Actual List Value=50 And Expected Value=50
Value Matching :Actual List Value=100 And Expected Value=100

I hope you find this post is useful, stay tuned for more automation.!

No comments:

Post a Comment