2

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!

4

2 回答 2

6
if has_header_upgrade_subscription? && header_upgrade_subscription.visible?
于 2014-01-22T23:09:02.577 回答
0

这种方法最适合我,因为它灵活且一致。

假设您有页面类:

class SomePage < SitePrism::Page

    element :some_message_banner, :css, "[data-component-property='message']"

    # Within this class you can use it like this
    def some_method
      ...
      has_some_message_banner?(visible: false, text: "some text")
      # or
      has_some_message_banner?(visible: true)
    end

end

在测试类之外,您可以像这样使用它:

@some_page = SomePage.new
expect(@some_page.has_some_message_banner?(visible: true, text: "some text")).to be_truthy
# or this combination 
@some_page.has_some_message_banner?(visible: false, text: "some text")

将返回truefalse取决于您要断言的内容

于 2017-06-30T07:04:03.147 回答