Check image is displayed with selenium


638
638 points
image is displayed with selenium
image is displayed with selenium

Check image is displayed with selenium

You want to check if image is displayed with selenium but selenium API doesn’t really gives you any helpers to do that , so we can implement a small helper to check image is displayed with selenium.

We can actually check if the service returns the proper mime types in the Content-Type HTTP header. Here you can find a list with mime types.

To check the Content-Type header for image in ruby using Net::HTTP, you should use the following code:

 def image_exist?(url)
      url = URI.parse(url)
        http = Net::HTTP.new(url.host, url.port)
        http.use_ssl = (url.scheme == "https")
        http.start do |http|
          return http.head(url.request_uri)['Content-Type'].start_with? 'image'
       end
end

This piece of code will return true of false if the content type it’s an image so you can check it using mini test assertions :

# should be present
assert_true(image_exist?(url)

# should not be present
assert_false(image_exist?(url)

There are also alternatives using xpath to check :

assert_true(find(:xpath,"//header[@src='logo.png']").visible?)

Happy Testing!


Like it? Share with your friends!

638
638 points
Test engineer

0 Comments

Your email address will not be published. Required fields are marked *

13 − thirteen =