删除变量中的 EOL 以比较字符串

Psy*_*tho 5 robotframework

我使用Run Keyword Unless比较变量和字符串:

Run Keyword Unless  '${text}' == 'HelloWorld'      My Keyword     ${text}
Run Code Online (Sandbox Code Playgroud)

有时${text}由以“\n”分隔的两行组成(例如“一行\n两行”)。如果是这样,测试将失败并显示错误:

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

我解决了删除 '\n' 的问题String.Replace String,如下所示:

${one_line_text}=     String.Replace String    ${text}     \n     ${SPACE}
Run Keyword Unless  '${one_line_text}' == 'HelloWorld'      My Keyword    ${text}   
Run Code Online (Sandbox Code Playgroud)

有没有办法在单独的关键字中不显式删除 EOL 的情况下做到这一点?

Jan*_*řík 6

怎么样${text.replace("\n", " ")}


Tod*_*kov 5

您可以使用 python 的字符串文字 -"""'''- 并且根本不更改字符串:

Run Keyword Unless  '''${text}''' == 'HelloWorld'      My Keyword     ${text}
Run Code Online (Sandbox Code Playgroud)

它们的设计几乎就是为了这个目的 - 保存具有换行符和引号的值。