1

I am setting the cookie for UI tests in a feature file using the existing login functionality implemented for API tests. Similar to what is described as 'hybrid approach' in karate docs and also in this thread - Karate UI - Maintain browser cookies/session/local storage variable value accorss different scnearios

My feature file is:

Feature: UI one time login test

Background:
# I am using the existing login functionality. I perform a onetime login in karate-config and store the cookie in a config variable and assign that to token variable defined below.
* def token = { value: '#(userAuthInfo.authSession)' }

# Then configure chromedriver
* configure driver = { type: 'chromedriver', showDriverLog: true, webDriverSession: { capabilities: { alwaysMatch: { acceptInsecureCerts: true } } } }

# Set the cookie
* driver 'about:blank'
* cookie({ name: 'SESSION', value: token.value, domain: 'localhost' })

Scenario: Visit url - 1
# webBaseUrl is defined in config
* driver webBaseUrl + 'url1'

When i run this feature and check the logs, I can see that correct request data is passed to /cookie endpoint and response from web driver api is error: 400 - invalid cookie domain. I also opened my site in chrome manually and checked how the cookie is set and its exactly the same as I am passing the json in cookie() method, including the domain. Even tried with a different domain on a different server and get the same error. Maybe it's not specific to domain as omitting the domain key in the request produces the same result.

Details below:

POST request to set cookie

14:06:36.495 [pool-1-thread-1] DEBUG com.intuit.karate - request:
4 > POST http://localhost:9515/session/c837c3e34a7dffa03fa4dc281ba0e3da/cookie
4 > Accept-Encoding: gzip,deflate
4 > Connection: Keep-Alive
4 > Content-Length: 97
4 > Content-Type: application/json; charset=UTF-8
4 > Host: localhost:9515
4 > User-Agent: Apache-HttpClient/4.5.11 (Java/1.8.0_262)
{"cookie":{"name":"SESSION","value":"cd02f1d7-6100-450b-bad9-edba8ead96b3","domain":"localhost"}}

Error 400 response:

14:06:36.501 [pool-1-thread-1] DEBUG com.intuit.karate - response time in milliseconds: 4.97
4 < 400
4 < Content-Length: 1696
4 < Content-Type: application/json; charset=utf-8
4 < cache-control: no-cache
{"value":{"error":"invalid cookie domain","message":"invalid cookie domain\n  (Session info: chrome=86.0.4240.111)","stacktrace":"0   chromedriver                        0x00000001080bbc29 chromedriver + 2464809\n1   chromedriver                      

Any ideas what could be going on?

rochitsen
  • 402
  • 6
  • 18

1 Answers1

0

Possibly an area where you can investigate and help us make changes. I've noticed chromedriver is fussy. Try with geckodriver and see if there is any difference. Would be great if you try with some other UI framework and see if we are missing any special handling.

One suggestion is add an etc/hosts entry to map localhost to something.com and use that domain.

Also see this: https://stackoverflow.com/a/59099843/143475

Peter Thomas
  • 54,465
  • 21
  • 84
  • 248
  • I did notice something. When i replaced the `driver 'about:blank` with `driver webBaseUrl` which is the base url of the site, I did get status 200 on set cookie call. So the error 400 is gone. But, opening pages of the site still takes me to login page which means cookie is not actually set. Not sure what is happening here. I did try with both geckodriver and chromedriver. Same results. Maybe the token i am using to set in the cookie is not correct? But I can access the application api's with same token in karate api tests. – rochitsen Oct 30 '20 at 03:52
  • @rochitsen there is some work on cookies happening for the next version: https://github.com/intuit/karate/issues/1293 - what would be really helpful is a) provide a sample following this process: https://github.com/intuit/karate/tree/develop/examples/ui-test b) help us with the code c) at least comment with your findings and what we should change – Peter Thomas Oct 30 '20 at 04:50