Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove EOL in variable for comparing strings

I use Run Keyword Unless comparing a variable and a string:

Run Keyword Unless  '${text}' == 'HelloWorld'      My Keyword     ${text}

Sometimes ${text} consists of two lines separated by "\n" (eg. "One line\ntwo lines"). If so, the tests fails with an error:

Evaluating expression ''One line  
two lines' == 'HelloWorld'' failed: SyntaxError: EOL while scanning string literal (<string>, line 1)

I solved the problem removing '\n' with String.Replace String as follows:

${one_line_text}=     String.Replace String    ${text}     \n     ${SPACE}
Run Keyword Unless  '${one_line_text}' == 'HelloWorld'      My Keyword    ${text}   

Is there a way to do it without explicit removing of EOL in a separate keyword?

like image 473
Psytho Avatar asked Jan 26 '17 09:01

Psytho


2 Answers

What about ${text.replace("\n", " ")}?

like image 102
Jan Kovařík Avatar answered Dec 30 '22 12:12

Jan Kovařík


You can use python's string literals - """ or ''' - and not change the string at all:

Run Keyword Unless  '''${text}''' == 'HelloWorld'      My Keyword     ${text}

They are designed for pretty much this purpose - to hold values having newline characters, plus quotes.

like image 34
Todor Minakov Avatar answered Dec 30 '22 10:12

Todor Minakov