Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Write to robot framework console from Python

I am a newbie using python and I wanted to ask for your help in showing me how can I print messages from Python into robot framework console.

like image 990
Marcio125 Avatar asked Mar 20 '13 08:03

Marcio125


People also ask

Can you write Python code Robot Framework?

Yes, you can. This is all documented fairly extensively in the Robot Framework user guide, in the section titled Creating test libraries.

How do I call a robot file from Python?

To import the Python script inside Robot, we use the keyword Library in the Robot file under ***settings*** . To call the function, we use <file_name> <dot> <function name> . To add keywords inside the function, we use the keyword decorator. Here, BuildIn().


1 Answers

There are several ways for a python function to send information to the robot logs or to the console. These are all documented in the Robot framework user's guide, in the section titled Logging information.

The cleanest way is to use the logging API, which gives specialized functions for various kinds of logging. For example, to send information to the console you would use logger.console(message).

Using the logging API

Here is a library file that uses this method:

# ExampleKeywords.py
from robot.api import logger
def write_to_console(s):
    logger.console(s)

You can use this library in the following manner:

*** Settings ***
| Library | ExampleKeywords.py

*** Test Cases ***
| Use a custom keyword to write to the console
| | Write to console | Hello, world

This will appear in the console only, and will not show up in the logs. If you want the information to show up in the logs you can use logger methods info, warn, debug, or trace. To log an error you would simply throw an exception.

Calling built-in keywords

There are other ways for your custom keywords to send information to the logs. For example, you can get a reference to the BuiltIn library, and directly call the log or log to console keywords like this:

from robot.libraries.BuiltIn import BuiltIn
def write_to_console(s):
    BuiltIn().log_to_console("Hello, world")

Using print statements

Finally, you can write information to the logs (but not only to the console) using print statements. You can prefix the string with *<level>* to affect the log level. For example, to print a warning you can do:

print "*WARN* Danger Will Robinson"

Summary

Using the API is arguably the best method to log information from your keywords. However, this is a fairly new API only available since Robot Framework 2.6, so if you are using an older version of Robot you may need to use one of the other techniques.

like image 56
Bryan Oakley Avatar answered Sep 20 '22 17:09

Bryan Oakley