capybarasite-prism

Can't seem to use SitePrism's element.visible? if the element doesn't exist


I have the following codes:

element :header_upgrade_subscription, :xpath, "//a[text()='Upgrade subscription']"
element :header_change_subscription, :xpath, "//a[text()='Change subscription']"

if header_upgrade_subscription.visible?
  change_subscription = header_upgrade_subscription 
else
  change_subscription = header_change_subscription 
end

The problem is if header_upgrade_subscription doesn't exist, it just fails with:

Capybara::ElementNotFound Exception: Unable to find xpath "//a[text()='Upgrade subscription']"

I know that in Capybara, you can do:

(rdb:1) first(:xpath, "//a[text()='Upgrade subscription']")
nil

and it would return nil if it doesn't exist. How would I use "first" method against the SitePrism element? This is what I get:

(rdb:1) first(header_upgrade_subscription)
Capybara::ElementNotFound Exception: Unable to find xpath "//a[text()='Upgrade subscription']"

I like using the "first" method as it has no wait time if the element doesn't exist.

Thanks for your help!


Solution

  • if has_header_upgrade_subscription? && header_upgrade_subscription.visible?