如何使用 Robot Framework 删除字符串中的白色 space?
How to remove white space in a string with Robot Framework?
我的变量:54、22
我想要的:54,22
我试过了:
Execute Javascript var a = 54, 22;var x = a.split(' ').join('');return x
和
Execute Javascript var a = 54, 22;var x = a.replace(/\s/g, '');return x
和
log ${str.strip()}
但是其中 none 解决了我的问题。当我分别执行这些代码时,我得到了那个错误:
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)'
如何解决这个问题?
您的变量中缺少逗号,这就是出现语法错误的原因:
var a = '54, 22';
使用字符串库
可用于删除空格的 String library has a Replace String 关键字,而不必求助于 运行ning javascript:
*** Settings ***
| Library | String
*** Test Cases ***
| Example of removing whitespace
| | ${value}= | Replace String | 54, 22 | ${space} | ${empty}
| | Should be equal | ${value} | 54,22
使用评估关键字
您可以 运行 任意 python 带有 Evaluate 关键字的代码。例如:
*** Test Cases ***
| Example of removing whitespace
| | ${value}= | Evaluate | "54, 22".replace(" ", "")
| | Should be equal | ${value} | 54,22
我的变量:54、22
我想要的:54,22
我试过了:
Execute Javascript var a = 54, 22;var x = a.split(' ').join('');return x
和
Execute Javascript var a = 54, 22;var x = a.replace(/\s/g, '');return x
和
log ${str.strip()}
但是其中 none 解决了我的问题。当我分别执行这些代码时,我得到了那个错误:
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)'
如何解决这个问题?
您的变量中缺少逗号,这就是出现语法错误的原因:
var a = '54, 22';
使用字符串库
可用于删除空格的 String library has a Replace String 关键字,而不必求助于 运行ning javascript:
*** Settings ***
| Library | String
*** Test Cases ***
| Example of removing whitespace
| | ${value}= | Replace String | 54, 22 | ${space} | ${empty}
| | Should be equal | ${value} | 54,22
使用评估关键字
您可以 运行 任意 python 带有 Evaluate 关键字的代码。例如:
*** Test Cases ***
| Example of removing whitespace
| | ${value}= | Evaluate | "54, 22".replace(" ", "")
| | Should be equal | ${value} | 54,22