Reputation: 323
I'm testing a widget where I have to enter a from(van) and To(naar) field in order to get travel directions. The To and From field are javascript and autocomplete after typing 3 characters. After having selected a location I want to be assert that the selected location is indeed in the To or From field. However, if I try to assert this with an assertEquals script it tells me there is no data in the field. Here is some page code and code I use to test it.
This is the html From input field:
<input id="van" class="textfield" type="text" data-reactid=".0.1.1.0.1.0.0.1.0" value="" tabindex="0" required="" placeholder="Place" autocomplete="off" aria-label="Van"></input>
This is how I enter text into the input field and select the second available option:
driver.findElement(By.id("van")).clear();
driver.findElement(By.id("van")).sendKeys(Ams);
It now shows me a few options containing Amsterdam and I select the second option:
driver.findElement(By.xpath("//div[@class='autocomplete-dropdown']/ul/li2")).click();
If I look at the screen it now shows me the following
I would now like to assert the From field contains what I just selected (Amsterdam, Noord-Holland). I do this with the following:
assertEquals("Amsterdam, Noord-Holland", driver.findElement(By.id("van")).getText());
This however results in:
org.junit.ComparisonFailure: expected:<[Amsterdam, Noord-Holland]> but was:<[]>
and if I inspect the element it shows me:
<input id="van" class="textfield" type="text" data-reactid=".0.1.1.0.1.0.0.1.0" value="" tabindex="0" required="" placeholder="Place" autocomplete="off" aria-label="Van"></input>
So it seems that the ComparisonFailure is correct but I can't find out why I cannot assert what I see on screen.
I'm testing this using Java, Selenium and Eclipse.
Upvotes: 0
Views: 353
Reputation: 2938
Hi please do it like below
in assert do this way
driver.findElement(By.id("van")).getAttribute("value");
// please note when you want
to check the value entered by you inside the input box then do not use getText()
as it returns inner visible html of the tag
So to get the value there is a hidden attribute for every input box known as "value"
which keeps value entered by you.
Hope this helps you Thanks
Upvotes: 1