Joe Early Joe Early - 7 months ago 67
Java Question

selenium webdriver java dropdown box

need some help on this. Trying to learn Java and selenium at the same time isn't good but has to be done :(

I am trying to test a drop down spots box which contains:

<select name="sport" id="sport">
<option value="1">Soccer</option>
<option value="2">Basketball</option>
</select>


I want to perform the follow:
1: Assert box is present and its text is Soccer and Basketball
2: Click Basketball and then click Soccer again.
3: Assert the changes in the table below this box. (Have this done I think but need the above)

Code so far:

Select sportDropdown = new Select(webBrowser.findElement(By.id("sport")));
sportDropdown.selectByVisibleText("Soccer");
assertEquals(sportDropdown, "Soccer");


Error received:

java.lang.AssertionError:
Expected :org.openqa.selenium.support.ui.Select@278806c4
Actual :Soccer

I have no idea where this "Expected " value is coming from so any pointers would be great folks.

Note this is all in Java so please refrain from submitting code help in C#. Makes my life harder :(

Thanks
J

Answer

You'll need to get the text of the selected option for your assert statement to work. Currently you are asking whether a WebElement object is equal to a String.

Select sportDropdown = new Select(webBrowser.findElement(By.id("sport")));
sportDropdown.selectByVisibleText("Soccer");
assertEquals(sportDropdown.getFirstSelectedOption().getText(), "Soccer");