如何使用Robot Framework在控制台输出中放置颜色 [英] How can I put colors in console output with Robot Framework

查看:369
本文介绍了如何使用Robot Framework在控制台输出中放置颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在RobotFramework的控制台输出中添加一些颜色。

I would like to put some colors in the console output of RobotFramework.

我尝试使用控制台颜色代码,例如:

I tried to use the console color codes like:

${message}=  Set Variable  hello world!
Log To Console  \\e[0;36;49m${message}\\e[0;39;49m

(在Linux控制台中, echo -e \e [0; 36; 49mHello world!\e [0; 39; 49m 打印 Hello world!青色)

(In a linux console, echo -e "\e[0;36;49mHello world!\e[0;39;49m" print Hello world! in cyan)

(我也尝试过使用一个 \ 以及 \033 [31m,\033 [0m,... 代码)

(I also tried with one \ and also \033[31m, \033[0m, ... codes)

但是它不起作用...

But it doesn't work...

因此,是否有可能执行以下操作:

So, is it possible to do something like:

${message}=  Set Variable  hello world!
Log To Console ${message.red}

我发现此模块,但我没有找到有关使用方法的任何信息它:(

I found this module but I didn't found anything on how to use it :(

我尝试过:

Log To Console ${message.red}  robot.output.console.highlighting

${message}=  Evaluate  ${message}.red  robot.output.console.highlighting
Log To Console ${message}

但没有效果:'(

推荐答案

由于机器人框架对 \的解释,登录控制台\\033 [31mRed文本\\033 [0m ]为输出着色。

Because of Robot Framework interpretation of '\', Log To Console \\033[31mRed Text\\033[0m doesn't color the output.

要解决此问题,必须登录变量 Evaluate 才能登录控制台:

To solve this problem, you have to Evaluate the variable before to log in to console:

${message}=  Evaluate  "\\033[31mRed Text\\033[0m"
Log To Console  ${message}






我最终获得了以下内容解决方案,我觉得它很干净:


I ended up with the below solution, which I find quite "clean":

*** Variables ***
${BLACK}  "\\033[30m"
${RED}    "\\033[31m"
# More colors ANSI codes...

*** Keywords ***
Initialize Colors
  ${black}=  Evaluate  ${BLACK}
  Set Test Variable  ${black}
  ${red}=  Evaluate  ${RED}
  Set Test Variable  ${red}
  # More colors...

然后,您只需在套件/测试用例设置中使用上一个关键字,您就可以为输出着色,如下所示:

Then, you just have to use the previous keyword in a Suite/Test Case Setup and you can colorize your outputs like below:

Log To Console  ${cyan}Some Text in cyan and a ${red}${variable}${cyan} in red${default}

这篇关于如何使用Robot Framework在控制台输出中放置颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆