4

I have a clickable image and it is not visible in FireFox. I mean, there is no image, but is there is an element (and it is clickable). FindElement(by).Displayed returns true but there is still no image. The question is how can I check, is this image visible?

Also, I found an error in html headers (the reason, why image is not visible), maybe there is a way to check image presence using html headers?

public bool IsFileIconDisplayed()
        {
            return IsElementDisplayed(By.XPath("//*[@class='SomeClass']/img"));

        }

protected bool IsElementDisplayed(By by)
        {
            return FindElement(by).Displayed;
        }
Vlad Titov
  • 686
  • 1
  • 6
  • 15
  • Well what makes it hidden/disappear/show? Does it have any CSS applied to it? When does it become hidden? (i.e on page load, when you hover over it, what?) – Arran Jul 24 '12 at 16:04
  • The answer for my question is below gave by Ashwin Prabhu, thank you. – Vlad Titov Jul 25 '12 at 09:09

2 Answers2

4

Does the image have a style setting its width and height to preset values?

If not you can query the image elements' client width and height property in Javascript.

document.getElementById(<webElement>).getClientRects()[0].width
document.getElementById(<webElement>).getClientRects()[0].height

or just

document.getElementById(<webElement>).naturalWidth

With JavascriptExecutor you should be able to draw these values into your Java/C# code and conclude image loaded/not loaded based on its width/height.

Ashwin Prabhu
  • 9,285
  • 5
  • 49
  • 82
  • No good, width and height > 0, so test thinks that image is displayed. – Vlad Titov Jul 24 '12 at 12:03
  • That's expected. don't compare against 0. Browsers have a fall back image which takes a fixed width and height usually less than 100 x 100. Rule out this case. It should be easy enough. – Ashwin Prabhu Jul 24 '12 at 13:40
  • I tried **document.getElementsByClassName("Class")[1].children[0].width** in chrome (where image is shown) and in FireFox (where image isn't shown), result is the same - 60px. What am I doing wrong? – Vlad Titov Jul 24 '12 at 13:55
  • Have you tried document.getElementById().naturalWidth? (I have updated my answer.) – Ashwin Prabhu Jul 25 '12 at 05:55
0

This thread contains valuable information on how to check visibility of an image (pay most attention to answer provided by Dave Hunt)

How to check URL for 404 using Selenium WebDriver?

Community
  • 1
  • 1
JacekM
  • 4,041
  • 1
  • 27
  • 34