How can I put colors in console output with Robot Framework

浪尽此生 提交于 2020-08-23 07:58:05

问题


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

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

(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 :(

I tried:

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

and

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

But none works :'(


回答1:


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

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}



回答2:


can I ask your overall goal here? Is it just to output random text in different colours?

Here's what I do to easily signal PASS/FAIL etc on different test cases to make them stand out:

--monitorcolors ansi

In your command to run your test suite, include the above toward the beginning of the command.



来源:https://stackoverflow.com/questions/35652486/how-can-i-put-colors-in-console-output-with-robot-framework

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!