1

Given the following js where I set the url...

function fn() {
    var root = 'https://google.com';
    karate.log('root value is:  ', root);

    additionalconfig = {
        root: root
    }
    return additionalconfig;
}

and the following feature file...

Feature:
Background:
    * callonce read('seturl.js')
    * url root
    # * url 'https://www.google.com'    


Scenario Outline:
    # Given url root
    Given path 'search'
        And params {q: <option>}
    When method GET
    Then status 200

    Examples:
    | option  |
    | karate  |
    | api     |
    | test    |
    | graphql |

The url is being reset on subsequent runs after the first, but according to the documentation, this should not be happening.

reset error being returned:

01: root
<<<<
org.graalvm.polyglot.PolyglotException: ReferenceError: "root" is not defined
- <js>.:program(Unnamed:1)

Documentation referenced above - https://github.com/karatelabs/karate#path

Note that the path 'resets' after any HTTP request is made but not the url.

If the url is passed from a previous file, in the background, it only works for the first call, but if hardcoded (commented out) in the background, it works for all the scenario examples. Setting the url in the scenario itself also only appears to work for the first run as shown below as well. url variable set in background url variable set in scenario outline

Additionally, if I change the callonce to simply a call, the feature file works as expected, but that seems contradictory to the documentation - https://github.com/karatelabs/karate#callonce

Karate's callonce keyword behaves exactly like call but is guaranteed to execute only once. The results of the first call are cached, and any future calls will simply return the cached result instead of executing the JavaScript function (or feature) again and again.

As a result of the above tests, I see 2 potential issues based on the documentation actual results of "root" is not defined in subsequent runs

  1. the url value does get reset if set as a variable
  2. the callonce feature does not appear to be caching the results
mike
  • 383
  • 1
  • 12
  • Not able to reproduce both the issues reported here. All four scenarios executed fine when root variable used in background & scenario. Also *callonce* function called only once & cached for next scenario runs. Providing more details about your project structure would help. – Srinivasu Kaki Mar 03 '23 at 02:32
  • yes I think mike is missing something, also please read this: https://stackoverflow.com/a/46080568/143475 – Peter Thomas Mar 03 '23 at 11:02
  • code uploaded - https://github.com/Mike-c-Jackson/karate-base-config-use-sample/tree/master/src/test/java/api-tests/url_as2variables – mike Mar 03 '23 at 18:44
  • @PeterThomas - did the example I uploaded, work for you? – mike Mar 07 '23 at 18:59
  • @SrinivasuKaki - did the example I uploaded, work for you? – mike Mar 07 '23 at 18:59
  • @mike - no it was low priority for me, because the community had attempted to help. re-reading this now (and I apologies if I misunderstood, because this example seems to be un-necessarily convoluted) - each `Scenario Outline` starts from a "fresh slate" so there is no question about what the "previous" URL is. so I would say - Karate is not designed to work in the way you possibly want. now, I'll be happy to look at a fresh example where you keep it simple. is the question about outlines ? callones ? url ? path - maybe it is just me I'm just unable to follow the thread of this question – Peter Thomas Mar 07 '23 at 19:50
  • The issue is with the url resetting during multiple runs within a scenario outline. If the sample provided is over complicated, please let me know what is complicated about it. The callonce is, what I believe, is the issue found as a part of the above test, since using callonce results in the reset of the url, after the first run, but using call, the url persist for each example in the outline which goes against the documentation of the data being cached for subsequent tests. – mike Mar 08 '23 at 15:24
  • To simply put it, if you run an outline test with the url pulled from a separate file using call (which executes for every scenario) vs callonce (which should only execute once and use a cached value), you will see the issue – mike Mar 08 '23 at 15:27
  • still not working for me in this simple example – mike Mar 15 '23 at 20:45

0 Answers0