1

I'm trying to setup a framework to run Graphql calls and create and E2E environment.

I've got the following setup so far but i can't seem to get the headers part of it working. i have managed to set the auth for each request and it all works but as it logs in for each request it doesn't really work as expected.

I want do the following steps:

  • run a login Test (different usernames valid/invalid)
  • run a logout test (Ensure token is removed)
  • Then login with correct user and extract the "set-cookie" header (to use globally for all future requests)

I was trying to use the following:

Karate-config.js

karate.callSingle('classpath:com/Auth/common-headers.feature', config);

headers.js

function fn() {
    var headers = {}
    headers["set-cookie"] = sessionAccessId
    karate.log('Cookie Value: ', headers)

    return headers
}

common-headers.feature

Feature: Login to Application and extract header

Background:
    * url serverAuthenticateUri
    * header Accept = 'application/json'

Scenario: 'Login to the system given credentials'

Given request { username: '#(username)', password: '#(password)'}
When method post
Then status 200
And match $.success == '#(result)'
And def myResult = response

* def sessionAccessId = responseHeaders['set-cookie'][0]

* configure headers = read('classpath:headers.js')
* print 'headers:', karate.prevRequest.headers

feature-file.feature

Feature: sample test script

Background:
  * url serverBaseUri

  * def caseResp = call read('classpath:com/E2E/POC/CommonFeatures/CreateCaseRequest.feature')
  * def caseReqId = caseResp.response.data.createCaseAndRequest.siblings[0].id
  * def caseId = caseResp.response.data.createCaseAndRequest.siblings[0].forensicCaseId

  * def graphQlCallsPath = 'classpath:com/E2E/POC/GraphQl/intForensic/'
  * def commmonFiles = 'classpath:E2E/CommonFiles/'


Scenario: TC1a - Request Server Details from Config DB (1st Run):
          Should handle requesting Server Details Data from Config Database.

  * def queryFile = graphQlCallsPath + '20-TC1a_req_req_valid_id.graphql'
  * def responseFile = graphQlCallsPath + '20-TC1a_resp_req_valid_id.json'

  Given def query = read(queryFile)
  And replace query.reqId = caseReqId
  And request { query: '#(query)' }
  When method post
  Then status 200
  And json resp = read(responseFile)
  And replace resp.reqId = caseReqId
  And replace resp.caseID = caseId
  And match resp == $

I can log in correctly and i get the set-cookie token but this isn't being passed on the feature-file.feature and i get an error saying "not logged in" in the response.

Any help appreciated! I might be looking at this totally wrong and i have tried to follow the shared-scope as much as i can but can't understand in.

Duality
  • 11
  • 5

1 Answers1

1

Please make this change and hopefully that works !

headers["set-cookie"] = karate.get('sessionAccessId');

Why is explained here: (read the whole section carefully) https://github.com/intuit/karate#configure-headers

EDIT: one more suggestion:

var temp = karate.callSingle('classpath:com/Auth/common-headers.feature', config);
karate.configure('headers', { 'set-cookie': temp.sessionAccessId });

Some extra suggestions:

If you have just started with Karate - based on your question I would suggest you get one flow working in a single Scenario first without any use of call and with nothing whatsoever in karate-config.js. Hard-code everything and get it working first. Use the header keyword to set any headers you need. I also see you are trying to set a set-cookie header (which may work) but Karate has a special keyword for cookie.

And don't even think about callSingle() to start with :)

Once you get that first "hard-coded" flow working, then attempt to configure headers and then only finally try to do "framework" stuff. You seem to have jumped straight into super-complexity without getting the basics right.

Please read this other answer as well, because I suspect that you or someone in your team is attempting to introduce what I refer to as "too much re-use": https://stackoverflow.com/a/54126724/143475 - try not to do this.

Also note that your question is so complex that I have not been able to follow it, so please ask a simpler or more specifc question next time. If you still are stuck, kindly follow this process: https://github.com/intuit/karate/wiki/How-to-Submit-an-Issue

Peter Thomas
  • 54,465
  • 21
  • 84
  • 248
  • Hi Peter, Thanks for the response. That didn't work :( so adding some more information. I have got the single flow working great and i have got it working so i get the headers from the background and all the feature file can use the header cookie without having to log in for each feature. Basically, What i'm trying to achieve here is a global set header for login What i get with the above code is: 1. all paremeters set correctly, 2. call-single common-headers file executes, logs in and gets the cookie, 3. at this point the next call comes in and fails because cookie is empty – Duality Jan 24 '20 at 14:19
  • @Duality I have nothing more to add. try to follow the instructions in my last link, else you are on your own, or you can pair-program with a friend or colleague who can help – Peter Thomas Jan 24 '20 at 14:24
  • The flow in the above as i see it ---> karate-config.js --> calls header.feature file --> which calls header.js (to set it globally? according to your examples) --> the feature file then runs --> and uses existing headers setup (which isn't working). so is my headers.js file not setting the header globally or is it actually doing anything – Duality Jan 24 '20 at 14:27
  • @Duality I got a headache reading that. last try - I made one more edit. read it and keep the documentation handy. if it doesn't work and doesn't make sense - just don't use `callSingle()` and just have `* call read('classpath:com/Auth/common-headers.feature')` in the `Background` of every feature. all the best ! – Peter Thomas Jan 24 '20 at 14:31