mra419
mra419

Reputation: 413

verifying drop down values using WebDriver

I need to verify drop down values using WebDriver. i have expected values in a String array

String[] exp = {"--Title--","Mr","Mrs","Miss","Ms","Dr","Prof"};

I need to write a function that return all the values from drop down and i need to assert with expected values, Below is the code that i have written to print the values from drop down, but i need to assert those values with expected ones:-

WebElement dropdown = driver.findElement(By.id("ddlNights"));  
Select select = new Select(dropdown);  
List<WebElement> options = select.getOptions();  
for(WebElement we:options)  
{  
 System.out.println(we.getText());  
}  

Can anyone help me in writing a method that returns String array of drop down values, so that we can reuse the method for validating values in every drop down using
Assert.assertTrue(Arrays.equals(Expected,Actual))

Thanks in Advance!!!

Upvotes: 0

Views: 38335

Answers (2)

subash chandrabose
subash chandrabose

Reputation: 1

WebElement element= driver.findElement(By.xpath("//select[@class='ui-selectonemenu']"));
 
ArrayList<String> array = new ArrayList();
        array.add("Select Tool");
        array.add("Selenium");
        array.add("Playwright");
        
        Select dropdown = new Select(element);
        System.out.println("Before dropdwon selection");
         List<WebElement> options = dropdown.getOptions();  
         for(WebElement we:options)  
         {  
             int i;
             boolean flag=false;
             System.out.println("getting text"+we.getText());
             for (i=0; i<array.size(); i++){
                  if (we.getText().equals(array.get(i))){
                    
                       //System.out.println("string text is available in dropdown"+array.get(i)+"\n");
                    array.remove(i);
                    flag = true;
                  }
                 }
               System.out.println("flag value"+flag);
               if(flag==true)
               {
                   System.out.println("flag is true"+we.getText()+"\n");
               }
               else
               {
                   System.out.println("flag is false"+we.getText()+"\n");
               }
        }

Upvotes: 0

Pavel Janicek
Pavel Janicek

Reputation: 14738

Try this

 String[] exp = {"--Title--","Mr","Mrs","Miss","Ms","Dr","Prof"};
 WebElement dropdown = driver.findElement(By.id("ddlNights"));  
 Select select = new Select(dropdown);  

 List<WebElement> options = select.getOptions();  
 for(WebElement we:options)  
 {  
  boolean match = false;
  for (int i=0; i<exp.length(); i++){
      if (we.getText().equals(exp[i]){
        match = true;
      }
    }
  Assert.assertTrue(match);
 }  

It should compare each element with every possibility in the expected Strings. The Match will be true only in "found" state. You can play around with the message with the Assert, because it can fail anytime. So you can do something like

Assert.assertTrue(match, we.getText());

Which should write you on which webElement it did not find any match - I am not 100% sure with that line - i dont have any IDE running to verify it.

Upvotes: 3

Related Questions