查找元素并稍后重新使用它 capybara cucumber

Find element and re-use it later capybara cucumber

我有一个问题,可以很直接地解决。

我想编写一个测试,该测试将在页面上的元素内查找,将值或文本存储在该元素内,以便以后使用。

示例:

在此 css 路径中“#clickable-rows > tbody > tr:nth-child(1) > td:nth-child(1)`”是我的值我想提取以便以后使用

这可能吗?

是的,您正在寻找 #text 对吗?

element_css_locator = "#clickable-rows > tbody > tr:nth-child(1) > td:nth-child(1)"

# save text of element
element_text = page.find(element_css_locator).text

# later on assert:
page.find(element_css_locator).should have_content element_text 
# or
page.should have_selector(element_css_locator, :text => element_text)

通常最好两次都找到元素,而不是挂在水豚元素实例上。