Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I put colors in console output with Robot Framework

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

like image 312
Matthieu.P Avatar asked Dec 19 '22 19:12

Matthieu.P


1 Answers

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}
like image 160
Matthieu.P Avatar answered Dec 28 '22 06:12

Matthieu.P