Conditional match based on OS

Viewed 148

I'm attempting to use karate version 1.0.1 to test command line options. So far, for the most part, everything is working and it is amazingly powerful and simple :)

But, I'm running into an issue. The problem I have is that I need to test some command line scripts where the output may vary depending on OS.

Here is an example of a scenario I'm attempting to use

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]"

The non-OS specific commands at the top all execute and verify output as expected. However I cannot use the if statement along with the match statement. Is there some way to do this?

I have seen some other posts about conditional matching within if statements, but I think this scenario may be different. I haven't yet figured out how to accomplish something like this using karate. Unless I were to have separate feature/package for the different OS's.

Thanks in advance for any help that can be provided.

1 Answers

Yes, I think the API has what you are looking for, it is karate.os: https://github.com/karatelabs/karate#karate-os

enter image description here

So this should work:

* if (karate.os.type == 'windows') command('tool --help')

You may get more ideas from the Karate Robot documentation:

https://github.com/karatelabs/karate/tree/master/karate-robot#robot

https://github.com/karatelabs/karate/tree/master/karate-robot#karatefork

EDIT: okay, I may have read your question in a hurry. I think the solution can be something like this, refer: https://stackoverflow.com/a/50350442/143475

So create one more helper JS function:

* def containsIfWindows =
"""
function(text) {
   if (!windows) {
     return;
   }
   var result = karate.match("out contains '" + text + "'");
   if (!result.pass) {
     karate.fail(result.message);
   }
}
"""

Then you can just do:

* containsIfWindows('--remove      [remove tool service]')

That said, since a string "contains" match is simple in JS, this may all you need:

* def containsIfWindows =
"""
function(text) {
   if (!out.includes(text)) {
     karate.fail(result.message);
   }
}
"""
Related