简体   繁体   English

从Python写入机器人框架控制台

[英]Write to robot framework console from Python

我是一个使用python的新手,我想请求你的帮助,告诉我如何将消息从Python打印到机器人框架控制台。

There are several ways for a python function to send information to the robot logs or to the console. python函数有几种方法可以将信息发送到机器人日志或控制台。 These are all documented in the Robot framework user's guide, in the section titled Logging information . 这些都记录在Robot框架用户指南的标题为Logging information的部分中。

The cleanest way is to use the logging API , which gives specialized functions for various kinds of logging. 最干净的方法是使用日志记录API ,它为各种日志记录提供专门的功能。 For example, to send information to the console you would use logger.console(message) . 例如,要将信息发送到控制台,您将使用logger.console(message)

Using the logging API 使用日志记录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 . 如果您希望信息显示在日志中,您可以使用记录器方法infowarndebugtrace 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: 例如,您可以获取对BuiltIn库的引用,并直接调用日志记录到控制台关键字,如下所示:

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

Using print statements 使用print语句

Finally, you can write information to the logs (but not only to the console) using print statements. 最后,您可以使用print语句将信息写入日志(但不仅仅是控制台)。 You can prefix the string with *<level>* to affect the log level. 您可以在字符串前加上*<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. 使用API​​可以说是记录关键字信息的最佳方法。 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. 但是,这是一个相当新的API,自Robot Framework 2.6起可用,因此如果您使用旧版本的Robot,则可能需要使用其他技术之一。

What it sounds like you're asking for is a way to write a library such that your testcase can print to the console during test execution. 你要求的是一种编写库的方法,这样你的测试用例可以在测试执行期间打印到控制台。

This is pretty easy, you can find it in the RF Manual under Logging Information . 这非常简单,您可以在“ 记录信息 ”下的“RF手册”中找到它。 The short version is you can log a "WARN" that will appear in both the log and on screen with a print statement, like so: 简短版本是您可以使用print语句记录将出现在日志和屏幕上的“WARN”,如下所示:

print "*WARN* This text will show up on the console."

Just type 只需输入

Log ${Variable_name}

This will print the variable content in html report file which is generated. 这将在生成的html报告文件中打印变量内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM