Adding custom step definitions to Karate framework

2.2k Views Asked by At

I have a need to extract a field parsed from a "complex" response header and use that value later in the test.

It seems that the "header" keyword in Karate is set up for setting request headers, not parsing response headers.

Is there a way to add a custom step definition maintaining access to the scenario variable stores? It appears the variable stores are private in the StepDefs class, and there doesn't seem to be a way to extend it easily.

1

There are 1 best solutions below

1
On BEST ANSWER

You can get access to the response headers. Please look at the documentation for responseHeaders.

That said, the match header short-cut is most likely what you are looking for.

Karate's philosophy is that you never need to write custom step-definitions.

edit: some examples, sounds like you just need to do some string manipulation of the Location header ? You can freely mix JS code into Karate expressions.

* def location = responseHeaders['Location'][0]
# assume location = 'foo?bar=baz'
* def bar = location.substring(location.indexOf('bar=') + 4)