如何使用自定义参数在页面对象 gem 中查找元素?
How to find element in page-object gem with custom parameter?
我将页面对象 gem 与 RSpec 一起使用,我想创建一个带有自定义参数的元素,例如
link(:derect_link, text: "#{custom_text}"
因为我想把 link 变成文本,每次开始测试时,这个文本都会更改。
具体场景如何使用?
访问器方法在 运行 时不支持自定义参数。您将必须手动创建 link 的方法。 link 访问器创建的方法的等价物是:
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
这将像标准方法一样使用,只是您将指定 link:
的文本
# 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')
我将页面对象 gem 与 RSpec 一起使用,我想创建一个带有自定义参数的元素,例如
link(:derect_link, text: "#{custom_text}"
因为我想把 link 变成文本,每次开始测试时,这个文本都会更改。
具体场景如何使用?
访问器方法在 运行 时不支持自定义参数。您将必须手动创建 link 的方法。 link 访问器创建的方法的等价物是:
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
这将像标准方法一样使用,只是您将指定 link:
的文本# 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')