Issue with Robot Framework & Capturing Screenshots

2,335 views
Skip to first unread message

Peter Cosgrave

unread,
Jan 27, 2016, 6:14:22 AM1/27/16
to robotframework-users

Hi,

 

I currently have an issue with “capture_page_screenshot” method in Robot Framework. This keyword captures the entire web page.

 

Objective:

My overall objective is to have a custom keyword “Capture Screenshot Of Element” which I can pass an element too and just return a screenshot of that element, expanding on the Capture Page Screenshot keyword.

It would be called using Robot Framework as follows: Capture Screenshot Of Element    elementName    imagename.jpg

 

Current Work Done:

I have coded this new keyword similar to below:

 

def Capture_Screenshot_Of_Element(self, element, filename):                              

                                location = element.location

                                size = element.size

                                self.capture_page_screenshot(filename) # saves screenshot of element

                                im = Image.open(filename) # uses PIL library to open image in memory

 

                                left = location['x']

                                top = location['y']

                                right = location['x'] + size['width']

                                bottom = location['y'] + size['height']

 

                                im = im.crop((left, top, right, bottom)) # defines crop points

                                im.save(filename) # saves new cropped image

 

The above function takes the screenshot but the screenshot is not a true reflection of what is seen in the browser (see screenshot1.jpg)

 

When I write a small Python script using Selenium as below I get the correct reflection of what is seen on the browser (screenshot.jpg)

 

fox = webdriver.Firefox()

fox.get('URL GOES HERE')

element = fox.find_element_by_xpath(‘XPATH_LOCATION’) # find part of the page you want image of

location = element.location

size = element.size

fox.save_screenshot('screenshot.jpg') # saves screenshot of element

fox.quit()

 

im = Image.open('screenshot.jpg') # uses PIL library to open image in memory

 

left = location['x']

top = location['y']

right = location['x'] + size['width']

bottom = location['y'] + size['height']

 

im = im.crop((left, top, right, bottom)) # defines crop points

im.save('screenshot.jpg') # saves new cropped image

 

Questions:

What is the difference between Robot Framework’s (Selenium2Library) capture_page_screenshot function and Selenium’s save_screenshot function?

Is there a solution to obtaining the correct image from a browser using Robot Framework?

screenshot.jpg
screenshot1.jpg

Tatu Aalto

unread,
Jan 27, 2016, 8:49:40 AM1/27/16
to peter.co...@gmail.com, robotframework-users

Ugh

The difference is very small. There is some logic on the keyword:

1) Determine where the screen shot is saved.
2) Determine the actual file name.
3) And make the screen shot visible in the log file.

But the actually getting the screen shot, it relies on the selenium functionality [1], which you are using. But I can see that there is a difference in the screen shots, but without being able try it out it is, for me, impossible to say anything concrete. As a wild guess, I would assume that this is a timing issue. As a first thing, I would try small sleep and if that solves the problem, I would then perform better polling to make sure that required elements are in place and visible.

-Tatu
Send from my mobile
[1] https://github.com/robotframework/Selenium2Library/blob/master/src/Selenium2Library/keywords/_screenshot.py

--
You received this message because you are subscribed to the Google Groups "robotframework-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to robotframework-u...@googlegroups.com.
To post to this group, send email to robotframe...@googlegroups.com.
Visit this group at https://groups.google.com/group/robotframework-users.
For more options, visit https://groups.google.com/d/optout.

Ed Manlove

unread,
Jan 27, 2016, 2:44:26 PM1/27/16
to robotframe...@googlegroups.com
Peter,

You should check out https://github.com/datakurre/robotframework-selenium2screenshots which has some/much of the functionality you might be looking for. Selenium2Library's capture_page_screenshot calls either Selenium's save_screenshot function or it's get_screenshot_as_file function [1] depending on the parameters passed. Note also that depending on the browser you are using selenium's save_screenshot will either capture the full page or what is in the viewpoint. This is a factor of the browser and not selenium. Just something to keep in mind. But do check out Asko's robotframework-selenium2screenshots library [2].

Ed

[1] https://github.com/robotframework/Selenium2Library/blob/master/src/Selenium2Library/keywords/_screenshot.py#L88
[2] https://github.com/datakurre/robotframework-selenium2screenshots
Reply all
Reply to author
Forward
0 new messages