Logs from Python in Robot Framework

Introduction

In this article, you can learn how to output messages from external Python libraries to the Robot Framework log.

It is assumed that you know the material of the article «Robot Framework Basics»

Let's consider a simple connection of an external library from the article «Architecture of tests on Robot Framework»

What should be done so that the function from unstable_url.py outputs something to the log file or to the robot console? The usual print() will not be visible anywhere.

from random import randrange def get_unstable_url() -> str: if randrange(2) == 1: return "https://www.urn.su" else: return "https://www.heihei.ru"

*** Settings *** Documentation Example that opens single page Library Browser ... enable_playwright_debug=${True} ... auto_closing_level=TEST ... retry_assertions_for=0:00:03 Library ../libraries/unstable_url.py Force Tags ui *** Variables *** *** Keywords *** Test Setup Tasks Start Chromium Browser Test Teardown Tasks Close Browser Start Chromium Browser New Browser browser=chromium headless=True New Context viewport={'width': 1920, 'height': 1080} ignoreHTTPSErrors=True *** Test Cases *** Starting a browser with a page [Tags] title ${url} = Get Unstable Url New Page ${url} Get Title == URN.SU Close Browser

BuitIn

from random import randrange from robot.libraries.BuiltIn import BuiltIn def write_to_console(s: str): BuiltIn().log_to_console(s) def get_unstable_url() -> str: if randrange(2) == 1: url = "https://www.urn.su" else: url = "https://www.heihei.ru" write_to_console(f"\nunstable url is: {url}\n") return url

============================================================================== Tests ============================================================================== Tests.Test Title :: Example that opens single page ============================================================================== Starting a browser with a page unstable url is: https://www.heihei.ru Starting a browser with a page | FAIL | Title 'HeiHei.ru' (str) should be 'URN.SU' (str) …

API

from random import randrange from robot.api import logger def write_to_console(s: str): logger.console(s) …

Не советую это делать в продакшен, но можно перегрузить функцию print()

from random import randrange from robot.api import logger # mode = "python" mode = "robot" if mode == "robot": def print(s: str): logger.console(s) def get_unstable_url() -> str: if randrange(2) == 1: url = "https://www.urn.su" else: url = "https://www.heihei.ru" print(f"\nunstable url is: {url}\n") return url

If you have many files in which you overload print(), I recommend using config

To send a message not to the console, but to the log, you need to replace console with the desired log level (info, warn, debug, or trace).

For example, to info

from random import randrange from robot.api import logger def write_to_console(s: str): logger.info(s) …

Robot www.aredel.com
INFO
лог

Article author: Andrei Olegovich

Related articles
Robot Framework
Architecture
Logs
__init__.robot
Create custom .py libs
Path to libs and resources
Keyword as decorator
Template
Parametrize
Demo with pywinauto

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: