0

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

2 Answers 2

2

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");
Sign up to request clarification or add additional context in comments.

2 Comments

You sir are a star !!!! Thanks very much for the quick response and your code worked perfect first run after a slight tweak: assertEquals(sportDropdown.getFirstSelectedOption().getText(), "Soccer");
Ah Sorry, I took a guess at the Java binding for .getFirstSelectedOption(), in Python we use .firstSelectedOptionso close enough. Glad I could help, I'll edit the answer for future viewers.
0

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"));

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.