1

I have to check that a text box accepts only a particular number of characters. If the characters exceed, it doesn't give any error but only turn the text in red. Another text box simply doesn't accept additional characters. How do I verify that text box doesn't accept more characters in both cases?

One option I have, is check if the 'Save' button which is visible on the page is clickable. How do I do this with Selenium WebDriver in Java?

John Smith
  • 7,243
  • 6
  • 49
  • 61
user3190414
  • 113
  • 1
  • 6
  • 13
  • Just for your information, 'clickable' refers to visibility and availability in selenium language. A disabled button would still be clickable, in the sense that it would register a mouse click, even if it would not start any actions. As the answers show, your question is really about if a known existing and visible element is enabled. Your question is still understandable, and valid, these definitions will just help you search for pretty simple answers yourself, in the future :) – jumps4fun Feb 17 '17 at 12:21

3 Answers3

2

You can use isEnabled() to check if an element is enabled or not.

driver.findElement(By.xpath("//path/to/element").isEnabled();

This will return true if the button is clickable.

Santosh Hegde
  • 3,420
  • 10
  • 35
  • 51
2

Just write the below method and call it whenever you want to check whether the element is clickable or not. Pass the required arguments also.

public static boolean isClickable(WebElement el, WebDriver driver) 
    {
        try{
            WebDriverWait wait = new WebDriverWait(driver, 6);
            wait.until(ExpectedConditions.elementToBeClickable(el));
            return true;
        }
        catch (Exception e){
            return false;
        }
    }
sForSujit
  • 987
  • 1
  • 10
  • 24
0

If by clickable you mean not disabled, you can use WebElement.isEnabled() .

About the text input that doesn't accept additional characters, if you want to detect that instead, it depends on how that constraint is enforced. For example if it's done through a maxlength attribute you can try to read that attribute from your input element (WebElement.getAttribute(String)). In this case you would know in advance how many characters you can send to the textbox.

About the text input that turns the text to red, if you want to detect that you should first find out how the text is turned to red; probably it does that by setting a CSS class or style attribute to your input element, in which case you can try to read that attribute from the element.

Community
  • 1
  • 1
SantiBailors
  • 1,596
  • 3
  • 21
  • 44
  • I was able to get the maxlength attribute. Thank you for the suggestion. But how do I verify that the textbox does not accept characters more than maxlength. When I use driver.findelement(By.xpath("xpath_value")).length() it gives me total length of the input string which is larger than maxlength and the test case fails. – user3190414 Feb 17 '17 at 13:55
  • You probably mean `driver.findElement(By.xpath("xpath_value")).getText().length()`. However, the purpose of my suggestion to read the `maxlength` attribute was that once you know that value that's the amount of characters that the textbox will accept. Now I did not test whether the `maxlength` attribute is honored by browsers but I definitely assume it is, which means I would trust that if the `maxlength` I read is 100 then the textbox will not accept more than 100 characters. Do you need to test that the field is set to accept max N characters, or that it actually honors that setting ? – SantiBailors Feb 17 '17 at 14:50