Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check whether image file exists, Robot-Framework, Selenium2Library

I would like to know, if it is possible and how to check, whether an element, which is supposed to display a picture, does really show the picture.

The picture is in <img src=..> and within the same domain.

like image 437
Qwerty Avatar asked Jun 02 '26 18:06

Qwerty


1 Answers

It's not entirely clear what your goals are. I think it's safe to assume that if your code does everything right (ie: the URL is correct, and css rules don't cause the element to be hidden) that the browser will do what it's supposed to do and show the image. Of course, if you're on the testing team that is actually testing the browser itself, that's a different problem.

So, assuming that the browser is behaving correctly, here are a couple of suggestions:

  1. You can use the Page should contain image attribute to verify the element is in the DOM.

  2. You can use the Get Element Attribute keyword to get the value of the src attribute, and then use the Get keyword of the requests library to verify that the url returns a 200 status code.

  3. You can use the Element should be visible keyword to verify that the css rules allow the element to be displayed.

Here is a complete working example:

*** Settings ***
| Library | Selenium2Library
| Library | RequestsLibrary

*** Variables ***
| ${TEST_PAGE} | http://the-internet.herokuapp.com/hovers
| ${BROWSER}   | chrome

*** Keywords ***
| Assert an image is visible
| | [Arguments] | ${element id}
| | [Documentation]
| | ... | This keyword fails if the given image element is not visible.
| | ... | Three checks are performed: that the element is on the page,
| | ... | that selenium thinks the element is visible, and that the 
| | ... | src attribute of the image points to a resource that is 
| | ... | accessible. 
| | 
| | # Step 1: verify the page contains the given element
| | Page should contain image | ${element id}
| | 
| | # Step 2: verify that the element is visible
| | Element should be visible | ${element id}
| | 
| | # Step 3: verify the src attribute of the image is accessible
| | ${img src}= | Get element attribute | ${element id}@src
| | Create session | img-src | ${img src}
| | ${response} | Get | img-src | / | # URL is relative to the session URL
| | Should be equal as integers | ${response.status_code} | 200
| | ... | image url '${img src}' returned unexpected status code '${response.status_code}'
| | ... | show_values=False

*** Test Cases ***
| Example of test to verify an image is visible
| | [Documentation]
| | ... | Verify that the first image on the test page is visible
| | 
| | [Setup] | Open browser | ${TEST_PAGE} | ${BROWSER}
| | Assert an image is visible | //div[@class='figure']/img
| | [Teardown] | Close All Browsers
like image 133
Bryan Oakley Avatar answered Jun 06 '26 06:06

Bryan Oakley



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!