Testing drop down list values in Selenium - Console does not print mis-match

0 votes
asked Jan 19, 2012 by pritum-patel

I'm testing drop down list values in Selenium. I am working in Java.

A drop down list has 40 values. I want my code to get all the values in the DDL and compare it to the ones they should be.

My code as it stands will fail if the values in the DDL do not match the hardcoded values I expect. It prints a message which says values do not match. However it doesnt tell me which value has not matched.

What is the best way to achieve this?

I am using selenium.getSelectOptions to get the DDL values. Then using if statement to check if values are same, if not then fail. But it doesnt tell me which value is not the same.

1 Answer

0 votes
answered Jan 20, 2012 by pavel-janicek

Well without the code is hard to help you without the code. I will try to outline a little help how do I approach this:

 public void chooseProduct(String product){
   List<WebElement> Options = new ArrayList<WebElement>();
   productChoooser = driver.findElement(By.id("id_of_the_selectbox"));
   productChoooser.click();
   Select select = new Select(productChoooser);
   Options = select.getOptions();
   for (WebElement option:Options){
       if(option.getText().equals(product)){
           option.click();
       }
   }

The above code select one item from the dropdown. You can easily modify it bymodyfying the if statement in this, say:

 boolean foundIt = false;
 for (WebElement option:Options){
       if(option.getText().equals(product)){
           System.out.println("The product " + product + "was found in the drop down list");
 foundIt = true;
       }

 }
if (!foundit){
  System.out.println("The product " + product + "was NOT found in the drop down list");
} 
} 

please recheck it after me, I am writing the code directly to the answer window

Welcome to Q&A, where you can ask questions and receive answers from other members of the community.
Website Online Counter

...