字符串比较不匹配,但“包含”匹配

时间:2019-09-20 14:12:55

标签: groovy soapui

我在使用groovy时遇到一个奇怪的问题,我找到了一种解决方法,但我不满意,因此也许有人可以帮助我:

我使用ReadyAPI 2.8。在我的测试案例中,我有一些常规步骤。 在其中之一中,我从上一个测试步骤中恢复了一个字符串,并且如果它与字符串“ TJA470”匹配,我想进行特定的处理。上一个测试步骤给出了一个ssh命令输出的字符串。

这是常规步骤代码:

//.vscode/launch.json
{
    "version": "1.0.0",
    "configurations": [
        {
            "type": "extendscript-debug",
            "request": "launch",
            "name": "Foo Testing",
            "program": "${workspaceFolder}/foo.jsx",
            // "stopOnEntry": true,
            // "trace": true, //debugging output
            "targetSpecifier": "indesign-14.064",
            "engineName": "main"
        }
    ]
}

这是控制台结果:

def hbox_ref = context.expand( '${get current HBox reference#hbox_ref}' )
// this returns me the data as a String

log.info hbox_ref 
log.info "\"$hbox_ref\"" // to check if there is no spurious blank
log.info hbox_ref.class

log.info (hbox_ref == "TJA470") => returns false
log.info (hbox_ref.equals("TJA470")) => returns false
log.info (hbox_ref.contains("TJA470")) => returns true

尽管有差异,但直接检验为==或等于,我在相同类型的所有其他比较中都使用了这些检验,并且很有效。

正如您在这里看到的,大多数逻辑情况返回false,我真的不知道为什么。

如果我在“ groovy游乐场”之类的工具中执行相同的脚本,它将按预期工作! :(

我根本不是时髦专家,肯定有一些我想念的东西,但是我觉得这很棘手!

如果有人可以帮助... 谢谢

1 个答案:

答案 0 :(得分:1)

谢谢,我发现了问题所在: 通过复制/粘贴控制台返回的问题,它表明在文本末尾有一个特殊字符。这在SOAPUI日志输出中不可见... 我在脚本中添加了以下处理:

def hbox_ref = context.expand( '${get current HBox reference#hbox_ref}' )
hbox_ref = hbox_ref.replaceAll("[^a-zA-Z0-9]+","")

hbox_ref = hbox_ref.replaceAll("[^\\w]+","")

这给出

log.info (hbox_ref == "TJA470") => returns true (at last !)

更优雅的解决方案(感谢SiKing):

(hbox_ref.trim() == "TJA470")

代替使用replaceAll

相关问题