0

Im unable to compare the list box options. I created the list box with options as tea, coffee, milk and softdrinks. I created a string array and stored these options in it. When I tried to compare same with the values retrieved from the selenium code (using select class) every time I get 'They are different'. Can any one please let me know what is the wrong I'm doing in the code?

package madhu;

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 ComparingListValues {

  public static void main(String[] args) {

    System.setProperty("webdriver.chrome.driver","./driver/chromedriver.exe");

    String [] options = {"Tea","Coffee","Milk","SoftDrinks"};



    WebDriver driver = new ChromeDriver();
    driver.get("file:///C:/Users/mdevaraju/Desktop/Selenium%20HTML%20Pages/Listbox.html");
    WebElement listbox = driver.findElement(By.id("s2"));

    Select select = new Select (listbox);
     List<WebElement> allOptions = select.getOptions();

    for (int i=0; i<allOptions.size(); i++) 
    {
        WebElement address = allOptions.get(i);
         String text = address.getText();
         ArrayList <String> soptions = new ArrayList <String>();
         soptions.add(text);
         System.out.println(text);
            if (soptions.equals(options))
            {
                System.out.println("Both values are same");
            }
            else
            {
                System.out.println("They are different");
            }
    }

    driver.close();
  }

}

Output:

Tea 
They are different
Coffee 
They are different
Milk 
They are different
SoftDrinks 
They are different
1
  • 2
    How much programming experience do you have? You are comparing an array of strings with an ArrayList: these have a different object type. You are also comparing the entire array with the array list, which means the objects won't even be the same size until the for loop is complete. You need to compare the text with the appropriate item in the array, not the two objects.
    – Kate Paulk
    Sep 7, 2017 at 19:46

1 Answer 1

2

You can compare the Listbox values with expected values by using following code :

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;

public class TestAbc {

      public static void main(String[] args) {


        String [] options = {"Taea","Coffee","Milk","SoftDrinks"};



        WebDriver driver = new FirefoxDriver();
         driver.get("file:///C:/Users/mdevaraju/Desktop/Selenium%20HTML%20Pages/Listbox.html");
         WebElement listbox = driver.findElement(By.id("s2"));

        Select select = new Select (listbox);

        List<WebElement> allOptions = select.getOptions();

        for(int i=0 ;i<allOptions.size();i++)
        {
            if(allOptions.get(i).getText().equals(options[i]))
            {
                System.out.println("Values Matching : "+" List Value = "+allOptions.get(i).getText()+" And Expected Value = "+options[i]);
            }
            else
            {
                System.out.println("Value Mismatch : "+" List Value = "+allOptions.get(i).getText()+" And Expected Value = "+options[i]);
            }
        }

      }

    }

You only need to store all your Expected values in an array of String as you are doing. Then get all option from Listbox. Loop through all options and get one by one and compare with your expected values as mentioned in code. No need to use ArrayList and all those things to make code more complex.

1
  • Thanks, Narendra. You showed me one of the possibilities. Nov 23, 2017 at 13:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.