Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selenium Webdriver - verify text box write-protected?

How can we verify whether a field is write-protected (that is, readonly) in Selenium using Java code?

Best regards

like image 646
ABCDEFG Avatar asked Mar 19 '12 10:03

ABCDEFG


4 Answers

  1. isEnabled() does not have any common things to readonly.
  2. String attribute = element.getAttribute("readonly"); will not fail your test even "readonly" is absent. In this case it returns null, but we need exception.

Use like this:

    WebElement some_element = driver.findElement(By.id("some_id"));
    String readonly = some_element.getAttribute("readonly");
    Assert.assertNotNull(readonly);

Do NOT verify getAttribute("readonly").equals("true") or similar, in different browsers it can be different as well. (readonly="readonly" in IE, readonly="" in FF, etc.)

like image 185
Anatoliy Avatar answered Nov 10 '22 00:11

Anatoliy


Since I don't know for what you need that check i'll post some examples that might be usefull.

driver.findElements(By.cssSelector("input:not([readonly='readonly'])[type='text']"));

=> returns all text input fields that are editable

WebElement element = driver.findElement(By.id("username");
//can fail if the attribute is not there
String attribute  = element.getAttribute("readonly"); 

=> might need a try catch block

like image 40
Tarken Avatar answered Oct 10 '22 05:10

Tarken


You can try to write something via sendkeys() and check that value attribute of textbox has not been changed.

like image 5
Pazonec Avatar answered Nov 10 '22 00:11

Pazonec


The WebElement interface has a function called isEnabled.

like image 3
Ken Brittain Avatar answered Nov 09 '22 23:11

Ken Brittain