2

In one of our use case, we are trying to read a feature file that's sole purpose is to Authenticate the user. From it's response, I am then extracting token and sessionId information that would be passed as a Header in the next API call.

CallingFeature

RetrieveOffers.feature

And call read('classpath:features/AuthenticateUser.feature')
* def token = response.data.uToken
* def sessionId = response.data.user.uSessionId
* configure headers = call read('classpath:headers.js) {uToken : #(token), uSessionId : #(sessionId)'}
And call read('classpath:features/EligibleOffer.feature)
And path '/api/v1/applyOffer'
And method post
Then status 200

Called Feature

EligibleOffer.feature

Scenario: Validate the user is able to get the eligible offers
Given url baseUrl
And path '/api/v1/offers'
When method get
Then status 200

It is doing all correct, In the sense that it sets up the necessary header information for the api call /api/v1/applyOffer which is in the feature file EligibleOffer.feature but when it tries to hit the api, the following error is coming every time no matter even if I increase the timeout.

ERROR com.intuit.karate - java.net.SocketTimeoutException: Read timed out, http call failed after 20054 milliseconds for url: ...

Solution tried - I tried to configure the timeout but it did not work at all.

However, If I then rewrite my feature file by compiling everything in a single feature file, it works fine. There is no timeout issue at all.

RetrieveOffers.feature

* def token = response.data.uToken
* def sessionId = response.data.user.uSessionId
* configure headers = call read('classpath:headers.js) {uToken : #(token), uSessionId : #(sessionId)'}
Given url baseUrl
And path '/api/v1/offers'
When method get
Then status 200
And path '/api/v1/applyOffer'
And method post
Then status 200

1 Answer 1

1

Use Background section. This section will execute before each scenario in feature file. Use callonce keyword to avoid multiple calls to AuthenticateUser.feature. This will cache the results.

Background: 
Given url baseUrl
And callonce read('classpath:features/AuthenticateUser.feature')
* def token = response.data.uToken
* def sessionId = response.data.user.uSessionId
* configure headers = call read('classpath:headers.js) {uToken : #(token), uSessionId : #(sessionId)'}

Scenario: Validate the user is able to get the eligible offers

And path '/api/v1/offers'
When method get
Then status 200

Scenario: Validate the user is able to apply the eligible offers

And path '/api/v1/applyOffer'
And method post
Then status 200

Sign up to request clarification or add additional context in comments.

3 Comments

yes, also look at karate.callSingle(): github.com/karatelabs/karate#karatecallsingle
Thanks but I must have mentioned earlier that I am indeed using the Background section. The peculiar case is that the header info for AuthenticateUser.feature is different and I dont need it for later API calls. For next API's (in this case both /api/v1/offers and /api/v1/applyOffer ) it needs a new header(infact same for both) information, which is what I am doing with this line - * configure headers = call read('classpath:headers.js) {uToken : #(token), uSessionId : #(sessionId)'}. It picks up the header but then erroring out with SocketTimeoutExcetion.
@USingh I guess you will understand that such issues are impossible to diagnose with the info you have provided. if you can provide a sample that others can replicate, that's another matter. for now I can only suggest you troubleshoot this yourself - maybe other answers may help: stackoverflow.com/search?q=%5Bkarate%5D+SocketTimeoutException

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.