Find missing images selenium -
i need automate (using selenium) missing images on webpage. image can loaded in 3 ways: 1)
<img src="http://something.com/google.png">
2)
<img src="/path/to/file/image.jpg">
3)
<div class="someicon" autoid="some-icon"></div>
the above info can come css (for example
.someicon { height: 97px; width: 93px; background-image: url("../assets/images/some_icon.png"); background-size: 93px 97px; }
now these different way of loading images on html page, how can write automation identify whether image missing or not?
problem 1) can solved using
list<webelement> imageslist = _driver.findelements(by.tagname("img")); (webelement image : imageslist) { httpresponse response = new defaulthttpclient().execute(new httpget(image.getattribute("src");)); if (response.getstatusline().getstatuscode() != 200) // whatever want broken images }
how 2) & 3) ?
thanks, mateen
in second case value of src resolved full url, can use same method first case.
for third case need value of background-image
calling getcssvalue
. return value in form of url("http://<server>/assets/images/some_icon.png")
need parse it:
webelement div = driver.findelement(by.classname("someicon")); string bgimage = div.getcssvalue("background-image"); matcher m = pattern.compile("\\\".*\\\"").matcher(bgimage); if (m.find()) { string imgurl = m.group().replace('"', ' ').trim(); // verify imgurl }
Comments
Post a Comment