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

What's Your Reaction?

cute cute
0
cute
scary scary
0
scary
wtf wtf
0
wtf
geek geek
0
geek
WIN WIN
0
WIN
Love Love
0
Love
OMG OMG
0
OMG
Angry Angry
0
Angry
Lol Lol
0
Lol
Test engineer

0 Comments

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

Choose A Format
Trivia quiz
Story