Chu
Chu

Reputation: 89

watir button.enabled? always returns true value

I am doing @browser.some_button(:id => 'some_id').enabled? but it is always returning me the true value even when the button is disabled.

I tried putting sleep for sometime and went and try to see if in the workflow button accidentally gets enabled but it is not.

What might be have gone wrong?

Upvotes: 5

Views: 2794

Answers (1)

Justin Ko
Justin Ko

Reputation: 46846

Problem

The Element#enabled? method only checks whether the element has the disabled attribute. It does not check if one of the classes includes the word disabled.

For example, given the html:

<html>
  <body>
    <input type="submit"id="disabled_id" value="disabled button" disabled>
    <input type="submit" id="enabled_id" value="enabled button">
    <input type="submit" id="some_id" value="Some_value" class="button is-disabled submit"> 
  </body>
</html>

You can see that only the first button, which has the disabled attribute, is not enabled:

p @browser.button(:id => 'disabled_id').enabled?
#=> false
p @browser.button(:id => 'enabled_id').enabled?
#=> true
p @browser.button(:id => 'some_id').enabled?
#=> true

Solution

To check if the element is disabled based on a class, you will have to check the class attribute.

Assume the page has the following html, which includes the button in the "disabled" and "enabled" state:

<html>
  <body>
    <input type="submit" id="some_id_disabled" value="Some_value" class="button is-disabled submit"> 
    <input type="submit" id="some_id_enabled" value="Some_value" class="button submit"> 
  </body>
</html>

One solution would be to check if the element is present when including the class as a locator:

p @browser.button(:id => 'some_id_disabled', :class => 'is-disabled').present?
#=> true
p @browser.button(:id => 'some_id_enabled', :class => 'is-disabled').present?
#=> false

Alternatively, if you located/stored the element earlier, you could check the class_names instead (rather than re-locating the element):

e = @browser.button(:id => 'some_id_disabled')
p e.class_name.split.include?('is-disabled')
#=> true

e = @browser.button(:id => 'some_id_enabled')
p e.class_name.split.include?('is-disabled')
#=> false

Upvotes: 3

Related Questions