我正在通过黄瓜测试浏览器数据。我正在查看页面上的链接列表,以确定我应该单击哪个链接。
所以让我说我有一个甜点链接列表,我想找到一个苹果酱。我打电话给一个通过链接的方法,并在我将名称苹果酱传递给它之后找到一个有苹果酱的方法。
字符串“apple sauce”存储在@dessert中,因为@dessert会经常更改,我需要知道是否有办法找出“apple sauce”是否存储在变量@dessert中。
当我做@ dessert.text.include? “#@ dessert”我一直都是假的。我需要这是真的,以便决定点击它。当我自己评估@dessert时,我有“苹果酱”......
如何摆脱引号(“)以便不对它们进行评估?我认为这让我很烦恼!
答案 0 :(得分:1)
测试字符串是否包含另一个字符串的标准方法是使用正则表达式,您可以在其中描述“足够接近”匹配的模式,或者搜索文字子字符串:
@dessert = "apple sauce;pears;walnuts"
@dessert.include?("apple sauce")
# => true
我不是为什么你的问题有"#@dessert"
,因为它的评估结果完全相同。 "#{@dessert}"
可能是你想要的,其中#{...}
内联一个字符串值,但这是多余的,因为你只是在没有其他数据的情况下测试单个变量。如果x
是一个字符串,则"#{x}"
和x
会评估为等效的字符串。
答案 1 :(得分:0)
您似乎在比较@dessert,一个@ dessert.text对象,该对象的属性。此外,您正在使用一种字符串插值形式,我和其他人一起建议避免使用。
尝试
@dessert.text.include? "#{@dessert.text}"
并查看它是否返回true。
答案 2 :(得分:0)
这是一个示例,我进入一个容器并拉出所有链接以查看点击,导航到页面,然后断言页面标题,网址和面包屑都包含在容器内的链接中的文本。您可以添加一个if语句来评估链接是否包含某些文本,如果是,则单击它。
来自lib文件
def pull_text_and_href_from_continent_link
@continents.each do |continent|
find(".continents li.#{continent}").hover
page.should have_selector(".#{continent} .continent-wrapper", :visible => true)
page.all(:css, '.continents .continent-wrapper a').each do |nav_link|
@array.push(:text => nav_link.native.attribute("text"), :href => nav_link[:href])
end
end
end
来自spec文件(这是一个不使用cuke的套件,但代码仍然相似)
it "should have the right page title, url, and breadcrumbs" do
@array.each do |info|
if info[:text].include?('See all')
pp "Skipping the see all link for this container"
else
visit_page_logged_out(info[:href])
assert_correct_page_url info[:href]
assert_country_breadcrumbs remove_specials(info[:text])
assert_page_title info[:text]
end
end