简体   繁体   English

如何使用Robot Framework删除字符串中的空格?

[英]How to remove white space in a string with Robot Framework?

My Variable : 54, 22 我的变量:54、22

What I want : 54,22 我想要的:54,22

I've tried : 我试过了 :

Execute Javascript var a = 54, 22;var x = a.split(' ').join('');return x 执行JavaScript var a = 54,22; var x = a.split('').join('');返回x

and

Execute Javascript var a = 54, 22;var x = a.replace(/\\s/g, '');return x 执行JavaScript var a = 54,22; var x = a.replace(/ \\ s / g,'');返回x

and

log ${str.strip()} 记录$ {str.strip()}

But none of them solved my problem. 但是他们都没有解决我的问题。 When I execute these code seperately I got that error: 当我分别执行这些代码时,出现了该错误:

FAIL : WebDriverException: Message: u'unknown error: Runtime.evaluate threw exception: SyntaxError: Unexpected number\\n at Object.InjectedScript._evaluateOn (:895:140)\\n at Object.InjectedScript._evaluateAndWrap (:828:34)\\n at Object.InjectedScript.evaluate (:694:21)\\n (Session info: chrome=43.0.2357.124)\\n (Driver info: chromedriver=2.12.301325 (962dea43ddd90e7e4224a03fa3c36a421281abb7),platform=Windows NT 6.1 SP1 x86_64)' 失败:WebDriverException:消息:u未知错误:Runtime.evaluate引发异常:语法错误:Object.InjectedScript._evaluateOn(:895:140)处的意外数字\\ n(Object.InjectedScript._evaluateAndWrap(:828:34)\\ n n在Object.InjectedScript.evaluate(:694:21)\\ n(会话信息:chrome = 43.0.2357.124)\\ n(驱动程序信息:chromedriver = 2.12.301325(962dea43ddd90e7e4224a03fa3c36a421281abb7),平台= Windows NT 6.1 SP1 x86_64)'

How to solve that problem? 如何解决这个问题?

Using the String Library 使用字符串库

The String library has a Replace String keyword that you can use to remove whitespace, without having to resort to running javascript: String库具有一个Replace String关键字,您可以使用它来删除空格,而不必借助运行javascript:

*** Settings ***
| Library | String

*** Test Cases ***
| Example of removing whitespace
| | ${value}= | Replace String | 54, 22 | ${space} | ${empty}
| | Should be equal | ${value} | 54,22

Using the Evaluate keyword 使用评估关键字

You can run arbitrary python code with the Evaluate keyword. 您可以使用Evaluate关键字运行任意python代码。 For example: 例如:

*** Test Cases ***
| Example of removing whitespace
| | ${value}= | Evaluate | "54, 22".replace(" ", "")
| | Should be equal | ${value} | 54,22

您在变量中缺少逗号,这就是为什么出现语法错误的原因:

var a = '54, 22';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM