How do I find an image generated by Dragonfly with

2019-04-02 17:03发布

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.

1条回答
\"骚年 ilove
2楼-- · 2019-04-02 17:43

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.

查看更多
登录 后发表回答