如何在Cucumber / Capybara步骤定义(Rails 3)中执行xpath regex搜索?

时间:2021-09-03 19:45:14

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. How can I use regex here to match image to only the filename instead of the entire url?

下面的方法非常有用,除了我有一个基于listing_id的可变文件夹结构。如何使用regex将图像匹配到文件名而不是整个url?

Then /^I should see the image "(.+)"$/ do |image|
    page.should have_xpath("//img[@src=\"/public/images/#{image}\"]")
end

2 个解决方案

#1


15  

Try page.should have_xpath("//img[contains(@src, \"#{image}\")]"), which will match any img whose src contains your filename.

尝试页面。应该有_xpath(“//img”[contains(@src, \“#{image}”)),它将匹配任何src包含您的文件名的img。

#2


0  

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$”的末尾,表示字符串末尾的匹配。

#1


15  

Try page.should have_xpath("//img[contains(@src, \"#{image}\")]"), which will match any img whose src contains your filename.

尝试页面。应该有_xpath(“//img”[contains(@src, \“#{image}”)),它将匹配任何src包含您的文件名的img。

#2


0  

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$”的末尾,表示字符串末尾的匹配。