I didn’t realize it’s actually a little tricky to check that an image is loaded when using WebDriver. WebDriver will only complain if the image tag you’re looking for isn’t in the DOM, not if the image link is broken and not actually visible.
For example, in watir-webdriver (ruby), this doesn’t really work as I would expect as the image isn’t actually visible on the ‘brokenimage’ page.
require 'watir-webdriver' b = Watir::Browser.new :firefox b.goto 'https://dl.dropbox.com/u/18859962/brokenimage.html' puts b.image(id: 'watermelon').visible? #true but is not visible
b = Watir::Browser.new :firefox b.goto 'https://dl.dropbox.com/u/18859962/brokenimage.html' puts b.execute_script("return (typeof arguments.naturalWidth!=\"undefined\" && arguments.naturalWidth>0)", b.image(id: 'watermelon'))
b = Watir::Browser.new :firefox b.goto 'https://dl.dropbox.com/u/18859962/brokenimage.html' puts b.execute_script("return arguments.complete", b.image(id: 'watermelon'))
In C#, you can wrap this up into a WebDriver extension method so you can this directly from Driver passing in the image element.
If it’s important that images load correctly in your application, you should probably start putting some of these in your WebDriver page objects. It’s simple to write a verify images method on a page that iterates through each image in the DOM and checks that it’s visible using the techniques above. Have fun.
Update: 30 November
I wrote about a slightly more elegant C# approach to do this directly from the element.