karatelabs / karate

Test Automation Made Simple
https://karatelabs.github.io/karate
MIT License
8.32k stars 1.95k forks source link

make sure url remains set even when entering called features #2209

Closed ptrthomas closed 1 year ago

ptrthomas commented 1 year ago

moving this comment into a new ticket for clarity: https://github.com/karatelabs/karate/issues/2202#issuecomment-1338690477

there was confusion about whether a url should "reset" when entering a "called" feature. I personally may have lost the original intent during some tricky refactoring and discussions

previous related threads: #2054 and #1990

but I have a couple more questions - there are 2 cases, when a call happens in "shared (global) scope" or "isolated (local) scope": https://github.com/karatelabs/karate#shared-scope

my vote is: the url should be reset if the call is "isolated scope" because the intent is to call "with a clean slate"

and the url should behave just like it would in a single feature otherwise. which is to NOT reset when a call boundary is crossed

snow23man commented 1 year ago

@ptrthomas to answer your question, I'm not really sure I have a strong opinion one way or the other, so I will agree with you ;)

adrian-85 commented 1 year ago

I like the idea of following the shared scope rule. The only potential issue I can think of is if the test repo only tests a single url, and that can be set once in the karate-config.js, they may not want to clear the url.

anupamck commented 1 year ago

I understand the salient difference between shared scope and isolated scope as being the following. A shared scope invoke overwrites variables in the main flow, whereas an isolated scope invoke does not. But otherwise, my intuition would point to their behaviour being consistent. To this end, I would recommend that the url reset behaviour be consistent across both invokes.

ptrthomas commented 1 year ago

thanks all for your comments. this helps a lot

for reference - I don't know when I added this to the docs, but it could be after this confusion started to emerge.

https://github.com/karatelabs/karate#call

image

anyway, I will acknowledge the need to change or mis-understandings on my part and move forward.

can we all agree that only the url is "carried across" the call boundary. in other words, headers and path and parameters will be "lost".

we can take a decision to never reset the url within a Scenario no matter how many calls it goes through. one advantage of this approach is that it is easy to explain and there is no confusion

that said. I have a new suggestion. let me know if this makes sense:

* configure url = 'http://localhost:8080/v1'

so we introduce a new configure key. if you set it - it will be used throughout your tests, unless the user uses the url keyword !!

how does that sound, that seems to me that it is the best solution for all the issues being discussed. we already have configure headers so it is not a big stretch

let me know !!

adrian-85 commented 1 year ago

I actually looked for a configure option as my first solution. I think that would fit well with what the framework already does.

ptrthomas commented 1 year ago

all: this is implemented, see doc changes: https://github.com/karatelabs/karate/commit/000e8b678cb4411ddb73504f6b30312c9f617891

would be good if you can build from source and test, but stay tuned for a 1.4.0 RC release (which will be Java 11+ only)

ptrthomas commented 1 year ago

update: for those who'd like to test this, 1.4.0.RC2 is available in maven central

anupamck commented 1 year ago

I know I am late, but I tested this on 1.4.0.RC3 and it works well \m/

Glad to have this feature. Will make our tests cleaner.

adrian-85 commented 1 year ago

So in 1.4.0RC3 I configure the url, and that works fine for called features. But in the main feature file, when I run a request with just path defined, it takes the last Given url and appends path to that rather than using the configured url. Is this the expected behavior?

        Given url mainFeatureUrl
        And header Authorization = token
        And request someRequest
        When method POST
        Then status 200

        #Create member
        * configure url = coreApi
        * configure headers = {"Authorization": "#(oktaToken)"}
        * call read('classpath:coding/helpers/create/create-member.feature')
        * def memberId = $.memberId
        * def memberPlanId = $.memberPlanId

        #Create appointment
        * call read('classpath:coding/helpers/create/create-appointment.feature')
        * def appointmentId = $.appointmentId

        #mainFeatureUrl get's reused here rather than configured url.
        #Create evaluation
        Given path 'evaluation'
ptrthomas commented 1 year ago

@adrian-85 yes, this is how karate has worked from day one: https://github.com/karatelabs/karate#path

image

maybe if you don't use url at all you might get the expected behavior

adrian-85 commented 1 year ago

Ok, so the configure option will not work the same way that setting * url someUrl used to work, i.e. setting it in the current feature, and all called features.

ptrthomas commented 1 year ago

@adrian-85 I'm not sure what you propose. for now I consider this thread closed

adrian-85 commented 1 year ago

Not saying anything needs to change, I was just under the impression the configure option would allow the same functionality that was classified as a bug, meaning you set url once, and every request after that uses the configured value, regardless of what file the request takes place in.

I might be wrong, but I thought that's how configure headers works.

ptrthomas commented 1 year ago

@adrian-85 okay, you are mixing both concepts. fortunately I just checked and setting url to null will get you what you want. try this:

* url 'https://jsonplaceholder.typicode.com'
* path 'users'
* method get

* url null

* configure url = 'https://httpbin.org'
* path 'get'
* method get

does that help ?

ptrthomas commented 1 year ago

1.4.0 released