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