MarkusBernhardt / robotframework-selenium2library-java

Java port of the Selenium 2 (WebDriver) Python library for Robot Framework
Apache License 2.0
46 stars 51 forks source link

Keyword Element Should Be Enabled throws a RuntimeException #53

Closed atmcarmo closed 9 years ago

atmcarmo commented 10 years ago

Hello,

I cannot use the keyword Element Should Be Enabled on my project. The application is built using ZK framework.

The keyword Element Should Be Disabled works fine, but the Enabled one doesn't. I think that what happens is:

I can give you an example for you to try, using the following page: http://www.zkoss.org/zkdemo/input/form_sample (I am using the "Password field")

My test would be:

  Go To  http://www.zkoss.org/zkdemo/input/form_sample
  Wait Until Keyword Succeeds  10 sec  1 sec  Element Should Be Enabled  //input[../../../td[1]//span[contains(text(), 'Password')]]

And it gives an error:

FAIL RuntimeException FAIL Timeout 10 seconds exceeded. The last error was: RuntimeException

Is this a selenium2library problem, or it is a problem of Selenium API itself?

Thank you very much.

MarkusBernhardt commented 10 years ago

The RunTimeException is caused by calling "Go To" without an open browser. Please try:

ZKOSS
    Open Browser    http://www.zkoss.org/zkdemo/input/form_sample    firefox    mainbrowser
    Wait Until Page Contains Element    //input[not(@disabled='disabled') and ../../../td[1]//span[contains(text(), 'Password')]]
atmcarmo commented 10 years ago

Hello Markus, thanks for your reply.

It was a copy-paste mistake. My code had the Open Browser keyword, and it still fails.

The code you pasted, with the disabled attribute on the xpath, is a workaround. And that was how I solved the problem. But it doesn't allow me to use other locators like CSS selector or id's.

I don't understand why this code gives me a RuntimeException.

ZKOSS
    Open Browser    http://www.zkoss.org/zkdemo/input/form_sample    firefox    mainbrowser
    Wait Until Keyword Succeeds  10 sec  1 sec  Element Should Be Enabled  //input[../../../td[1]//span[contains(text(), 'Password')]]
atmcarmo commented 10 years ago

Hello again,

I was checking the code, and I found the issue. You have the following piece of code, on method isEnabled(), starting on line 1564:

if (!element.isEnabled()) {
    return false;
}
String readonly = element.getAttribute("readonly"); //this may return null, which will cause an exception in the next line
if (readonly.equals("readonly") || readonly.equals("true")) {
     return false;
}

When we use the keyword Element Should Be Disabled, the Selenium API will return false on element.isEnabled(), and your method will work properly.

However, if we use the keyword Element Should Be Enabled, the Selenium method element.isEnabled() will return true, and the method execution will procede. Then, on line 1574, element.getAttribute("readonly") will return null if that attribute is not present (please check http://selenium.googlecode.com/svn/trunk/docs/api/java/org/openqa/selenium/WebElement.html#getAttribute(java.lang.String)). That null object will return a NullPointerException in the next line (1575), which is somehow shown as a RuntimeException in the robot log.

So, you have to protect your String readonly variable against null values, or that method won't work properly.

Thank you very much

MarkusBernhardt commented 9 years ago

Fixed by your patch. Thanks!!!