如何找到 Dragonfly 使用 Cucumber/Capybara 生成的图像? [英] How do I find an image generated by Dragonfly with Cucumber/Capybara?

查看:22
本文介绍了如何找到 Dragonfly 使用 Cucumber/Capybara 生成的图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

In the comments to the solution for How do I find an image on a page with Cucumber/Capybara, somebody asked:

I can't seem to figure how to get this to work with URLs generated by Dragonfly. They look like this: /media/BAh_some_long_string_AwIw/12_11_52_810_5x5.jpg?s=7e360000, where 5x5.jpg is my file name. I've tried something like: //img[@src="/media//#{image}?s=*"] but it doesn't work. Got any tips? – Ramon Tayag Feb 25 at 4:18

I have a similar problem, only worse - in my case, the generated image paths don't even include a (jpg|png|gif) file name, they only have these really long ids:

<img src="/media/BAhbB1sHOgZmSSIdNGQ4MTEyOGU3ZjViZmQwZTQ4MDAwMDAyBjoGRVRbCDoGcDoKdGh1bWJJIg0yMDB4MjAwIwY7BlQ" />

(Using dragonfly with mongo/gridfs)

These paths get rendered alright, but I can't figure out how to find them in a Cucumber/Capybara step :P

Any ideas? I looked at the Dragonfly's features, but they only test the rendering of the image itself, without detecting it's existence within an html page.

解决方案

Answering my own question, after talking to Dragonfly's author (he's working on making this easier):

#route
match '/media/:dragonfly/:file_name', :to => Dragonfly[:images]  

#model
class Store
  include MongoMapper::Document
  key :photo_uid, String
  key :photo_name, String
  image_accessor :photo
end

#view
<%= image_tag @store.photo.thumb('274x207#').url(:suffix => "/#{@store.photo_name}") if @store.photo %>

#cucumber
Then I should see the image "my_photo.png"

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

The key is adding an [attachment]_name field to the model, which Dragonfly populates automatically, and then passing it on as a suffix to url(). And the routes needs to allow for a :file_name param besides the generated dragonfly identifier.

这篇关于如何找到 Dragonfly 使用 Cucumber/Capybara 生成的图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆