Skip to content

Colored Text in Terminal [Enhancement] #4878

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
Alpha-Centauri-00 opened this issue Sep 21, 2023 · 1 comment
Open

Colored Text in Terminal [Enhancement] #4878

Alpha-Centauri-00 opened this issue Sep 21, 2023 · 1 comment

Comments

@Alpha-Centauri-00
Copy link

Alpha-Centauri-00 commented Sep 21, 2023


"Log To Console" Keyword is useful to address some values or debugging, but if the text in Terminal is colored, it would be great!


image

@BarbeRousseLibre
Copy link

BarbeRousseLibre commented May 15, 2025

Hello,

EDIT: This is made for UNIX like system, specifically here on Gentoo Linux. Don’t know how to achieve the same thing for Windows.

I do agree with you, I would like to see something Built-In for such.

Currently I am working on it and it is actually possible to drop colors in your output, please see my screenshot (Robotframework + SeleniumLibrary + DataDriver).

Image

To do that, it’s not very difficult but do makes your code looks like spaghetti… This is how I do it, for now:

1/ First, I declare the BASH’s color into variable in my resources.robot file:

# Logging colors and variables used for improving readability
${FOREGROUND_COLOR_BLACK}=           \\33[30m
${FOREGROUND_COLOR_RED}=             \\33[31m
${FOREGROUND_COLOR_GREEN}=           \\33[32m
${FOREGROUND_COLOR_YELLOW}=          \\33[33m
${FOREGROUND_COLOR_BLUE}=            \\33[34m
${FOREGROUND_COLOR_MAGENTA}=         \\33[35m
${FOREGROUND_COLOR_CYAN}=            \\33[36m
${FOREGROUND_COLOR_LIGHT_GRAY}=      \\33[37m
${FOREGROUND_COLOR_GRAY}=            \\33[90m
${FOREGROUND_COLOR_LIGHT_RED}=       \\33[91m
${FOREGROUND_COLOR_LIGHT_GREEN}=     \\33[92m
${FOREGROUND_COLOR_LIGHT_YELLOW}=    \\33[93m
${FOREGROUND_COLOR_LIGHT_BLUE}=      \\33[94m
${FOREGROUND_COLOR_LIGHT_MAGENTA}=   \\33[95m
${FOREGROUND_COLOR_LIGHT_CYAN}=      \\33[96m
${FOREGROUND_COLOR_WHITE}=           \\33[97m

${BACKGROUND_COLOR_GRAY}=            \\33[40m
${BACKGROUND_COLOR_RED}=             \\33[41m
${BACKGROUND_COLOR_GREEN}=           \\33[42m
${BACKGROUND_COLOR_YELLOW}=          \\33[43m
${BACKGROUND_COLOR_BLUE}=            \\33[44m
${BACKGROUND_COLOR_CYAN}=            \\33[45m
${BACKGROUND_COLOR_WHITE}=           \\33[46m
${BACKGROUND_COLOR_GRAY}=            \\33[47m
${BACKGROUND_COLOR_GRAY}=            \\33[48m

${STYLE_FONT_RESET}=                 \\33[0m
${STYLE_FONT_BOLD}=                  \\33[1m
${STYLE_FONT_FAINT}=                 \\33[2m
${STYLE_FONT_ITALIC}=                \\33[3m
${STYLE_FONT_UNDERLINED}=            \\33[4m

Then I also define pre-colored new variables:

${CLR_NORMAL}=                       ${FOREGROUND_COLOR_WHITE}
${CLR_INFO}=                         ${FOREGROUND_COLOR_LIGHT_YELLOW}${STYLE_FONT_FAINT}
${CLR_ON}=                           ${FOREGROUND_COLOR_MAGENTA}${STYLE_FONT_BOLD}
${CLR_OFF}=                          ${FOREGROUND_COLOR_LIGHT_MAGENTA}${STYLE_FONT_FAINT}
${CLR_OK}=                           ${FOREGROUND_COLOR_GREEN}${STYLE_FONT_BOLD}
${CLR_WRN}=                          ${FOREGROUND_COLOR_YELLOW}${STYLE_FONT_BOLD}
${CLR_CRI}=                          ${FOREGROUND_COLOR_RED}${STYLE_FONT_BOLD}
${CLR_TAG}=                          ${FOREGROUND_COLOR_LIGHT_CYAN}${STYLE_FONT_FAINT}
${CLR_VAL}=                          ${FOREGROUND_COLOR_LIGHT_GREEN}${STYLE_FONTFAINT}

Once it is made, in my actual test cases .robot’s file I can use them like this:

${msg_required_field}=                      Evaluate        "\\n${CLR_OK}All required fields are present before initiating this test case.${STYLE_FONT_RESET}"
    Log To Console                              ${msg_required_field}

As you can see on my screenshot, it works very well. BUT.

Let me shows now how unreadable could your code be after you started using this…:

    Input Text                                  ${target_1_id}   ${target_1_data}
    ${target_1_message}=                        Evaluate         "\\n${CLR_INFO}Filling:${STYLE_FONT_RESET} ${CLR_TAG}${target_1_id}${STYLE_FONT_RESET} ${CLR_INFO}with:${STYLE_FONT_RESET} ${CLR_VAL}${target_1_data}${STYLE_FONT_RESET}${CLR_INFO}.${STYLE_FONT_RESET}"
    Log To Console                              ${target_1_message}

And in my actual example, I’ve more than a dozen of these, it’s awful to read (another example):

    ${next_test_initialisation_message}=        Evaluate  "${CLR_INFO}Page was reloaded from:${STYLE_FONT_RESET} ${CLR_VAL}${current_location}${STYLE_FONT_RESET} ${CLR_INFO}to${STYLE_FONT_RESET} ${CLR_VAL}${URL_FORM}${STYLE_FONT_RESET}${CLR_INFO}.${STYLE_FONT_RESET}\\n\\n${CLR_OK}Location and Title are correct as expected.${STYLE_FONT_RESET}\\n\\n${CLR_WRN}Next test, if any, will be initiated now.${STYLE_FONT_RESET}"
    Log To Console                              ${next_test_initialisation_message}

It works, but there is several cons to this method:

1/ This is absolutely not easy to reproduce for any other message you want to log to console.
2/ Very easy to add mistake by accident.
3/ Poor maintainability.
4/ Now trying to "copy/paste" this way in my other 7 testing cases… is a pure hell :).
5/ Each modification (you want to change anything beside the colors defined for any ${CLR_*} ask you to repeat it each time you use it in each file. It’s hell. Not good.

I’m currently trying to write a keywords doing this, but I struggle with Evaluate and Log To Console behavior once I try to set this method out of my actual test file.

So yeah, you can already do it (at least on UNIX alike), but it’s painful.

I hope this is helping anyone wondering.

Regards,
GASPARD DE RENEFORT Kévin

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants