[英]How do I do an xpath regex search in my Cucumber / Capybara step definition (Rails 3)?
I have a Scenario that validates that the image I just uploaded exists on the next page. 我有一个方案验证我刚刚上传的图像存在于下一页。
This below works great, except that I have a variable folder structure based on the listing_id. 下面这个很好用,除了我有一个基于listing_id的变量文件夹结构。 How can I use regex here to match image to only the filename instead of the entire url?
我如何在这里使用正则表达式将图像匹配到文件名而不是整个网址?
Then /^I should see the image "(.+)"$/ do |image|
page.should have_xpath("//img[@src=\"/public/images/#{image}\"]")
end
试试page.should have_xpath("//img[contains(@src, \\"#{image}\\")]")
,它将匹配任何src
包含你的文件名的img
。
If you have the option then using a css selector is more concise in this case: 如果您有选项,那么在这种情况下使用css选择器更简洁:
page.should have_selector( :css, "a[href$='#{image}']")
Note the ' $
' on then end of ' href$
' to denote matching against the end of the string. 注意“
$
上再结”的“ href$
”来表示对字符串的结尾匹配。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.