Robot Framework as a Python Library

Introduction

Quite a typical situation is when existing robot libraries are added to their own Python libraries

But do not forget that Robot Framework is also a Python library and its keywords can be called from Python code.

In this article you can study examples of such calls

Screenshot

Let's look at how to make robotic screenshots using an example similar to unstable_url from the article about custom Python libraries

It is clear that with the help of Python you can make screenshots in different ways, but we are interested in the screenshot that will be integrated into test results.

The Screenshot library requires the installation of an additional library. In Windows this is usually Pillow.

Supported tools and modules for taking screenshots How screenshots are taken depends on the operating system. On OSX, screenshots are taken using the built-in screen capture utility. On other operating systems, you will need to install one of the following Python tools or modules. You can specify a tool/module to use when importing the library. If no tool or module is specified, the first one found will be used. wxPython :: http://wxpython.org :: General-purpose Python GUI toolkit. PyGTK :: http://pygtk.org :: This module is available by default in most Linux distributions. Pillow :: http://python-pillow.github.io :: Only works on Windows. The original PIL package is also supported. Scrot :: http://en.wikipedia.org/wiki/Scrot :: Not used on Windows. Install with apt-get install scrot or similar.

python -m pip install pillow

Project structure

screenshot_demo/ |-- Libraries | `-- unstable_url.py `-- Tests `-- demo.robot

*** Settings *** Documentation Example that uses external python library Library ../libraries/unstable_url.py *** Test Cases *** Check URL [Tags] title ${url}= Get Unstable Url Should Be Equal As Strings ${url} https://www.aredel.com

from robot.libraries.Screenshot import Screenshot from random import randrange _screenshot = Screenshot() def get_unstable_url() -> str: _screenshot.take_screenshot("my_screen", "900px") if randrange(2) == 1: return "https://www.aredel.com" else: return "https://www.heihei.ru"

ТNow, every time unstable_url() is called, a screenshot will be taken by the robot.

It will be visible in the file log.html

Physically, it will be located in the directory from which the test was called.

By default, the first screenshot is called my_screen_1.jpg . If you do not delete it and run the test again, the second file will appear my_screen_2.jpg and so on.

Article author: Andrei Olegovich

Related articles
Robot Framework
Basics
Login Test
Empty Fields
reStructuredText
QuickStart
Robot from Python

Search this site

Subscribe to Telegram channel @aofeed to follow new articles and updates to old ones

Go to channel

@aofeed

Ask a question in the Telegram group

@aofeedchat

Contacts and cooperation:
I recommend our hosting beget.ru
Write to info@urn.su, info@aredel.com if you:
1. Want to write an article for our site or translate an article into your native language.
2. Want to place thematically relevant ads on the site.
3. Ads on my site pass maximum censorship. If you see an ad block unsuitable for school-age children, shocking or misleading - please contact us by e-mail
4. Found a mistake, inaccuracy, bug, etc. on the site. ... .......
5. Articles can be shared on social media by clicking a network icon: