Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking for placeholder text using Selenium Webdriver

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

like image 685
T D Avatar asked Feb 29 '12 14:02

T D


2 Answers

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");
like image 99
Mohan Avatar answered Oct 03 '22 11:10

Mohan


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).

like image 23
Andy Tinkham Avatar answered Oct 03 '22 10:10

Andy Tinkham