基于 OS 的条件匹配
Conditional match based on OS
我正在尝试使用 karate 版本 1.0.1 来测试命令行选项。到目前为止,在大多数情况下,一切正常,而且非常强大和简单:)
但是,我 运行 遇到了一个问题。我遇到的问题是我需要测试一些命令行脚本,其中输出可能因 OS.
而异
这是我尝试使用的场景示例
Feature: Test commands from the tool file
Scenario: Verify contents of tool help menu options
* if(windows) command('tool --help')
* if(!windows) command("./tool --help")
Then match exit == 0
And match out contains "Usage: tool --[command]"
And match out contains "no argument [Run in Jetty]"
And match out contains "--migrate [migrate tool database using database settings]"
And match out contains "-p xxxx [listening port to be used (replace xxxx with a port number)]"
And match out contains "--help [display this message]"
And match out contains "example: tool --migrate"
# Some commands are OS specific
# How to accomplish this??? What's below doesn't work
* if(!windows) match out contains "--status [check the status of the tool process and port]"
* if(windows) match out contains "--install [install tool as a windows service]"
* if(windows) match out contains "--remove [remove tool service]"
顶部的非OS 特定命令都按预期执行和验证输出。但是我不能将 if 语句与 match 语句一起使用。有什么办法吗?
我看过其他一些关于 if 语句中的条件匹配的帖子,但我认为这种情况可能有所不同。我还没有想出如何使用空手道完成这样的事情。除非我为不同的 OS 设置单独的 feature/package。
在此先感谢您提供的任何帮助。
是的,我认为 API 有您要找的东西,它是 karate.os
:https://github.com/karatelabs/karate#karate-os
所以这应该有效:
* if (karate.os.type == 'windows') command('tool --help')
您可能会从空手道机器人文档中获得更多想法:
https://github.com/karatelabs/karate/tree/master/karate-robot#robot
https://github.com/karatelabs/karate/tree/master/karate-robot#karatefork
编辑:好的,我可能已经匆忙阅读了您的问题。我认为解决方案可以是这样的,参考:
所以再创建一个辅助 JS 函数:
* def containsIfWindows =
"""
function(text) {
if (!windows) {
return;
}
var result = karate.match("out contains '" + text + "'");
if (!result.pass) {
karate.fail(result.message);
}
}
"""
那么你可以这样做:
* containsIfWindows('--remove [remove tool service]')
也就是说,由于字符串“包含”匹配在 JS 中很简单,这可能是您所需要的:
* def containsIfWindows =
"""
function(text) {
if (!out.includes(text)) {
karate.fail(result.message);
}
}
"""
我正在尝试使用 karate 版本 1.0.1 来测试命令行选项。到目前为止,在大多数情况下,一切正常,而且非常强大和简单:)
但是,我 运行 遇到了一个问题。我遇到的问题是我需要测试一些命令行脚本,其中输出可能因 OS.
而异这是我尝试使用的场景示例
Feature: Test commands from the tool file
Scenario: Verify contents of tool help menu options
* if(windows) command('tool --help')
* if(!windows) command("./tool --help")
Then match exit == 0
And match out contains "Usage: tool --[command]"
And match out contains "no argument [Run in Jetty]"
And match out contains "--migrate [migrate tool database using database settings]"
And match out contains "-p xxxx [listening port to be used (replace xxxx with a port number)]"
And match out contains "--help [display this message]"
And match out contains "example: tool --migrate"
# Some commands are OS specific
# How to accomplish this??? What's below doesn't work
* if(!windows) match out contains "--status [check the status of the tool process and port]"
* if(windows) match out contains "--install [install tool as a windows service]"
* if(windows) match out contains "--remove [remove tool service]"
顶部的非OS 特定命令都按预期执行和验证输出。但是我不能将 if 语句与 match 语句一起使用。有什么办法吗?
我看过其他一些关于 if 语句中的条件匹配的帖子,但我认为这种情况可能有所不同。我还没有想出如何使用空手道完成这样的事情。除非我为不同的 OS 设置单独的 feature/package。
在此先感谢您提供的任何帮助。
是的,我认为 API 有您要找的东西,它是 karate.os
:https://github.com/karatelabs/karate#karate-os
所以这应该有效:
* if (karate.os.type == 'windows') command('tool --help')
您可能会从空手道机器人文档中获得更多想法:
https://github.com/karatelabs/karate/tree/master/karate-robot#robot
https://github.com/karatelabs/karate/tree/master/karate-robot#karatefork
编辑:好的,我可能已经匆忙阅读了您的问题。我认为解决方案可以是这样的,参考:
所以再创建一个辅助 JS 函数:
* def containsIfWindows =
"""
function(text) {
if (!windows) {
return;
}
var result = karate.match("out contains '" + text + "'");
if (!result.pass) {
karate.fail(result.message);
}
}
"""
那么你可以这样做:
* containsIfWindows('--remove [remove tool service]')
也就是说,由于字符串“包含”匹配在 JS 中很简单,这可能是您所需要的:
* def containsIfWindows =
"""
function(text) {
if (!out.includes(text)) {
karate.fail(result.message);
}
}
"""