Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Check 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!

The post Check image is displayed with selenium appeared first on Testingrepository - Talking about selenium , and our passion.



This post first appeared on Testing Repository - Creating Testing, please read the originial post: here

Share the post

Check image is displayed with selenium

×

Subscribe to Testing Repository - Creating Testing

Get updates delivered right to your inbox!

Thank you for your subscription

×