简体   繁体   English

如何在我的Cucumber / Capybara步骤定义(Rails 3)中进行xpath正则表达式搜索?

[英]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.

相关问题 如何在Rails 3中使用Cucumber / Capybara在页面上找到图像 - How do I find an image on a page with Cucumber / Capybara in Rails 3 如何从使用 Capybara 的 Cucumber Step 获取整个页面的响应 HTML? - How do i get the response HTML of an entire Page from a Cucumber Step that uses Capybara? 在Rails 3中使用Cucumber / Capybara,如何设置自定义User-Agent字符串? - Using Cucumber/Capybara in Rails 3, how do I set a custom User-Agent string? 为什么我为Cucumber :: Rails :: World获取未定义的方法`click'? 在Cucumber / Capybara的步骤 - Why do I get undefined method `click' for Cucumber::Rails::World ? in Cucumber / Capybara steps 我如何找到由蜻蜓用黄瓜/水豚生成的图像? - How do I find an image generated by Dragonfly with Cucumber/Capybara? 如何模拟黄瓜/水豚的IP地址? - How do I mock an IP address in cucumber/capybara? 如何在Cucumber / Capybara中设置浏览器语言? - How do I set the browser language in Cucumber / Capybara? 使用Cucumber和Capybara的Factory Girl,我如何填充表格? - Using Factory Girl with Cucumber and Capybara, how do I populate forms? 我如何才能看到黄瓜失败的黄瓜步骤中发现了什么? - How can I see what capybara found in a failing cucumber step? 如何在“黄瓜”步骤中从Rack返回响应? - How do I get the response returned from Rack in a Cucumber step?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM