karatelabs / karate

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

Provide a variable to access the request information #226

Closed createam closed 6 years ago

createam commented 7 years ago

Provide a variable, similar to response, which can make it easier to access the information about the request sent by Karate. The rationale is to make the request and response available to external Java classes accessed through Java Interop.

More details available on the StackOverflow question, https://stackoverflow.com/questions/46846367/accessing-the-built-request-details-in-karate/

ptrthomas commented 7 years ago

@createam I'm thinking through this and just to confirm, you are looking for read-only right ? and you want to get the final values of the url, request and headers (perhaps cookies) when the HTTP request is actually fired. Which means these need to be set after the method step is processed ?

BTW are you really able to dynamically use method foo, because I thought variables are not supported.

createam commented 7 years ago

@ptrthomas Yes, read-only and after the complete request is built and ready to be fired (or after fired). I don't think there is any value in editing the generated request, that is supposed to be done using the karate-dsl constructs.

Yeah, method foo does not work. Just setting the variable anyway so that the validator can figure out what method was sent

Saurabh51084 commented 6 years ago

@ptrthomas - I have a doubt on the similar lines, Which is as follows:-

I am getting an integer value in the response of one of the soap request. I have added the value in variable and able to print it on the console. which confirms that variable is parsing the value correctly. Now i want to use the same variable as a parameter in another soap request. I have done research in Karate doc but unable to get the syntax/value/format in which i should be able to pass the variable in another soap. Can you please let me out with this?

Note:- Values in 1 response is coming at runtime and changing after every request.

ptrthomas commented 6 years ago

There are multiple ways to do this, the best reference which you may have missed is this set of regression test cases for Karate: xml.feature

specifically look at how the set keyword works or use an embedded expression for e.g. <myValue>#(myValue)</myValue>

ptrthomas commented 6 years ago

oops, closed by mistake, and @Saurabh51084 I think this is not related to this ticket. For these kinds of general questions, kindly use Stack Overflow: https://stackoverflow.com/questions/tagged/karate

ptrthomas commented 6 years ago

@createam can you check if this works for you: https://github.com/intuit/karate/blob/aefc49874615d9de11569ac2e4aa642722b0e091/karate-demo/src/test/java/demo/request/request.feature

also, you can now use expressions in the method.

Would you be able to test by building from source or I can release a patch version, let me know.

createam commented 6 years ago

@ptrthomas I could build from source and run some tests which includes the lastRequest. Works fine. Also using the expression for method worked great. Thanks for providing this enhancement.

Do you have plan to expose this lastRequest through a cleaner variable similar to the "response" variable rather than useing (karate.lastRequest)?

ptrthomas commented 6 years ago

@createam great. actually I thought hard about it. each built-in variable adds bloat to the context, which becomes painful especially when doing a call to other features - since the return value is an aggregate of all variables. so considering that this is a 0.01 % use case (you are the first to request this) I felt this is better. plus it would be for those who are writing frameworks on top of karate like you are, and know what you are doing. the other consideration is it gets low-level, for e.g. the request body is held as a byte-array, which you need to deal with.

I'm thinking that karate.prevRequest might be a slightly better name, what do you think ?

createam commented 6 years ago

@ptrthomas Makes sense. Saw that you have already changed the name. Thanks for making this update.