Is “enabled?” method available on Watir and/or Pag

2019-06-14 16:49发布

问题:

Yesterday i was working on determining is this link was or not enabled, waiting until enabled in order to click it. I'm using Cucumber + Ruby + Watir + Page-Object gem. The link is very similar to:

<a id="the_link"  href="#" disabled="disabled">Proceed with your order</a>

Once you fill some fields, the link is enabled and the source changes to:

<a id="the_link"  href="#">Proceed with your order</a>

The idea is to wait until the link is enabled in this way, which works with buttons:

def click_on_link
  Watir::Wait.until { self.the_link_element.element.enabled? }
  self.the_link
end

...but does not work with the link. I made it work determining if the attribute exists, this way:

def click_on_proceed_form
  Watir::Wait.until { !self.the_link_element.element.attribute_value('disabled') }
  self.proceed_form_submit
end

Looking for the "enabled?" method at the Watir documentation here or at the Page-Object gem here, it seems that is available for all elements. But looking for the same method in the Watir documentation here, seems it's only available for Buttons, Selects and Inputs.

So, i wonder is there is an "enabled?" method for links (anchors) and, if it exists, how to use it. Can you help clarify this issue, please? Thank you very much!

回答1:

Watir-webdriver does not support the enabled? method for links. I believe this is because the disabled attribute is not a standard attribute for links.

On the other hand, Watir-classic does support the enabled? method for links. However, it will always return false (again because links cannot be disabled).

Therefore, I think your approach is correct (unless you want to monkey patch the link elements to support the enabled?).

However, you should try to avoid using Watir-webdriver directly where possible. The page-object gem has its own methods for waiting and getting attribute values:

def click_on_proceed_form
  wait_until{ !the_link_element.attribute('disabled') }
  proceed_form_submit
end