【问题标题】:How to find element in page-object gem with custom parameter?如何使用自定义参数在页面对象 gem 中查找元素?
【发布时间】:2016-07-14 03:54:26
【问题描述】:

我将页面对象 gem 与 RSpec 一起使用,我想创建一个带有自定义参数的元素,例如

link(:derect_link, text: "#{custom_text}"

因为我想获取文本的链接,并且每次开始测试时都会更改此文本。

如何在规范场景中使用它?

【问题讨论】:

    标签: ruby automated-tests page-object-gem


    【解决方案1】:

    访问器方法在运行时不支持自定义参数。您必须手动创建链接的方法。与链接访问器创建的方法等效的是:

    class MyPage
      include PageObject
    
      def derect_link_element(text)
        link_element(text: text)
      end
    
      def derect_link(text)
        derect_link_element(text).click
      end
    
      def derect_link?(text)
        derect_link_element(text).exists?
      end
    end
    

    这将像标准方法一样使用,除了您要指定链接的文本:

    # Click the link
    page.derect_link('custom_text')
    
    # Check if the link exists
    page.derect_link?('custom_text')
    
    # Get the link element to perform other actions (eg inspect attribute values)
    link = page.derect_link_element('custom_text')
    link.attribute('href')
    

    【讨论】:

    • NoMethodError: undefined method 'link_element' for #<Watir::Browser:0x..fd1e305c5547af8de closed=true> 我收到了这个错误
    • 抱歉,嵌套元素方法不应该针对browser 调用。该示例已更正。
    猜你喜欢
    • 1970-01-01
    • 2017-01-23
    • 1970-01-01
    • 2013-10-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多