Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Response object not natively available when calling features from karate-config.js #1835

Closed
kirksl opened this issue Nov 12, 2021 · 11 comments
Closed
Assignees
Milestone

Comments

@kirksl
Copy link
Contributor

kirksl commented Nov 12, 2021

This issue started occurring with Karate 1.0

The following feature file will fail when called from karate-config with error org.graalvm.polyglot.PolyglotException: ReferenceError: "response" is not defined

This same feature file will succeed when called outside of/after karate-config

Scenario: this will fail
    * table params
        | param1 | param2 | param3 |
        | 'abc'  | 'def'  | 'ghi'  |
    * call read('classpath:com/repro/httpbin/httpbin.feature@endpoint=get_httpbin') params
    * def headers = response.headers

The same feature file, now edited, setting call to a variable and referencing the variable works

Scenario: this will succeed
    * table params
        | param1 | param2 | param3 |
        | 'abc'  | 'def'  | 'ghi'  |
    * def r = call read('classpath:com/repro/httpbin/httpbin.feature@endpoint=get_httpbin') params
    * def headers = r[0].response.headers

Attaching a demo project referencing the issue
myproject.zip

To Repro
Open /src/test/java/com/repro/tests/test.feature and run @test=test1 scenario
Witness error in console or /build/karate.log

Open /src/test/java/com/repro/setup/customers/customers_setup.feature and goto line 11 to reach problematic code.
Comment out option 1 section of code
Uncomment option 2 section of code
Run @test=test1 scenario again

@ptrthomas ptrthomas assigned ptrthomas and unassigned ptrthomas Nov 13, 2021
@ptrthomas ptrthomas added the bug label Nov 13, 2021
@ptrthomas ptrthomas added this to the 1.2.0 milestone Nov 13, 2021
@ptrthomas
Copy link
Member

that's weird - since I'm having a very busy month cc @joelpramos since he is familiar with this code

@joelpramos
Copy link
Contributor

I’m touching the calling of features with the fix I’m doing for #1793 so I’ll look into this one too.

joelpramos added a commit to joelpramos/karate that referenced this issue Nov 14, 2021
joelpramos added a commit to joelpramos/karate that referenced this issue Nov 14, 2021
… work of sharing config across shared contexts
@joelpramos
Copy link
Contributor

@kirksl can you give it a try with the last commit at #1828

@kirksl
Copy link
Contributor Author

kirksl commented Nov 15, 2021

@joelpramos i still see the issue when trying latest develop branch
image

@joelpramos
Copy link
Contributor

Took a look into your scenario bit better - problem is when calling a Feature with an array (i.e. multiple executions) in sharedScope the variables are not getting propagated to the parent.

I noticed that when fixing the original issue that lead me to refactor some details on the callFeature (Config changes weren't getting propagated to caller in shared scope).

The fix is to replace ScenarioEngine.java:2053 with the following:

            if (resultVariables.isMap()) {
                setVariables(resultVariables.getValue());
            } else if (resultVariables.isList()) {
                ((List) resultVariables.getValue()).forEach(r -> {
                    setVariables((Map) r);
                });
            }

i.e. the else condition will try to propagate all variables of the called Feature. Looking for yours and @ptrthomas opinion though... how do we think this should behave? Since it's an iteration we'll have the same variables with multiple values (e.g. the response object itself in case of calling an API) so is there value in propagating the shared scope?

I guess that was the design pre v1.0 so I'm ok implementing that behavior (and do the same to propagate any Config changes) but I think we should maybe add a note in the documentation to avoid this as the order of the execution of the scenarios cannot be guaranteed.

@joelpramos
Copy link
Contributor

Btw to reproduce just need to add an array as argument in the call of the following file: karate-core/src/test/java/com/intuit/karate/core/parallel/call-single-from-config3.feature .

Response will become an array thus it won't propagate the response to the caller.

@ptrthomas
Copy link
Member

@joelpramos I'm ok to propagate, a call array loop always runs on one thread (scenario outlines can run in parallel)

joelpramos added a commit to joelpramos/karate that referenced this issue Nov 20, 2021
@joelpramos joelpramos mentioned this issue Nov 20, 2021
5 tasks
ptrthomas added a commit that referenced this issue Nov 20, 2021
@joelpramos
Copy link
Contributor

@kirksl fixed in develop

@kirksl
Copy link
Contributor Author

kirksl commented Nov 26, 2021

thank you @joelpramos
my test is limited, but this appears fixed.
once the issue below is fixed i can fully test this on my end
#1838

@ptrthomas
Copy link
Member

1.2.0 released

@ptrthomas
Copy link
Member

tagging this to #2009 - I'm going to undo this change. so a "call loop in shared scope" will never propagate variables back to caller. it doesn't make sense

ptrthomas added a commit that referenced this issue Aug 2, 2022
note that this commit reverts the change made in #1835
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants