Testing that an image has actually been loaded

391 views
Skip to first unread message

Ian Dickinson

unread,
May 21, 2013, 12:50:40 PM5/21/13
to capybar...@googlegroups.com
I'm using capybara-webkit to write some acceptance tests for a Javascript-heavy page. Going fine so far, but I have a problem that I want to write a test that checks that an image has actually been loaded successfully. Testing that the img tag appears in the dom is fine, but I'm not sure how to test that, for example, the image path was computed correctly so that the image loads.

So far I have:

  it "should have a help page with the appropriate images" do
    visit "#{ROOT}help-en.html?site=ukk2206-20900"
    page.find( :xpath, "/html/body/div[2]/div[2]/ul[2]/li[2]/img" )
  end

This finds me a Capybara::Node::Element object, but I can't see from there what to test to prove that the img src has turned into some actual bits :)

Any suggestions?

Many thanks,
Ian

Message has been deleted

Joe Ferris

unread,
May 23, 2013, 11:14:16 AM5/23/13
to capybar...@googlegroups.com
I haven't tried either of these, but:

* You could use evaluate_script to check the `complete` and `naturalWidth` properties: http://stackoverflow.com/questions/1977871/check-if-an-image-is-loaded-no-errors-in-javascript (second solution).
* You could check the image's `src` attribute to see if it corresponds to an image on disk.

-Joe
--
You received this message because you are subscribed to the Google Groups "capybara-webkit" group.
To unsubscribe from this group and stop receiving emails from it, send an email to capybara-webk...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Ian Dickinson

unread,
May 24, 2013, 3:22:33 AM5/24/13
to capybar...@googlegroups.com
Thanks Joe. I'd already reached the conclusion that the second of your suggestions, doing a second HTTP GET on the img src attribute was the most expedient way forward for getting the test working. I guess the main thrust of my question (which wouldn't have been apparent from the way  asked the question!) was to do with the underlying object graph that the webkit browser was building up. My reasoning was that it would have built a complete DOM, just like a non-headless browser would visiting the same page, and that I'd ultimately be able to get hold of an object with, say, a width attribute that would only be set by pulling in the actual image. However, despite spending some time with the source of both capybara and capybara-webkit, and using pry to inspect the object graph, I couldn't find a way to do that.

Tldr: I've solved the presenting problem to get the test working, but I'm still wondering quite how much of the rendered page is accessible to other tests I might think of writing in the future.

Thanks again,
Ian
Reply all
Reply to author
Forward
0 new messages