Reputation: 118
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
Upvotes: 0
Views: 2425
Reputation: 64
For anyone whose value equals the text value use the get attribute. It worked for me. May be work for someone else.
Select sportDropdown = new Select(driver.findElement(By.id("id"))); sportDropdown.selectByValue("Value"); Thread.sleep(3000);//This was needed since the page is refreshed whenever the value is selected. System.out.println(driver.findElement(By.id("id")).getAttribute("value"));
Upvotes: 0
Reputation: 5453
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");
Upvotes: 2