I have a textbox that displays a placeholder text when the textbox is not in focus. When the focus is shifted to this textbox (by placing the cursor in the textbox), the placeholder text disappears and the cursor appears in the textbox. I am trying to automate a scenario which confirms this behavior. Has anybody ever tried to automate a similar scenario? Any suggestions would be appreciated. Thanks
You can get the placeholder text using the getAttribute
method of webdriver.
The HTML:
<input id="<ur id>" class="<ur class name>" type="password" lang="en" maxlength="30" placeholder="Enter Password" data-label="passwordPlaceholder" tabindex="5">
The Java code:
String password=driver.findElement(By.cssSelector("ur css path")).getAttribute("placeholder");
I've done similar tests. For our fields with placeholder text, the text appears in a placeholder attribute on the element. In Ruby, we use code like this:
element = @driver.find_element(*<locator string>*)
expected_placeholder_text = element.attribute('placeholder')
That gives us a string that we compare just like any other string. You could also shorten the element.attribute call to be
element['placeholder']
as well, but we prefer the former (for non-relevant and completely arbitrary reasons).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With