Reputation: 189
Having a problem setting options of cookies using variables, we use cookies and passwords that need to be secure, ie not in a repo so we store them in an auth.json file that we have engineers author in their respective implementations from a secure document. So we pull that data from the document and I am attempting to write the cookie dynamically as such:
const auth = require('../../fixtures/auth')
context('Assertions', () => {
beforeEach(() => {
cy.task('log', JSON.stringify(auth.cookies[0].path))
cy.setCookie(`'${auth.cookies[0].key}'`, `'${auth.cookies[0].value}'`,{ path: `/`} )
// cy.setCookie(auth.cookies[1].key, auth.cookies[1].value, {"path": auth.cookies[1].path} )
// cy.setCookie(auth.cookies[2].key, auth.cookies[2].value, {"path": auth.cookies[2].path} )
})
The above works when I hardcode the path, but when I try to pull the path in from the json using:
cy.setCookie(`'${auth.cookies[0].key}'`, `'${auth.cookies[0].value}'`,{ path: `'${auth.cookies[0].path}'`} )
The json looks like this:
"cookies": [
{"key": "da_key", "value": "da_value", "path": "da_path", "name": "da_name"},
...
I've tried all sorts of combinations of single, double quotes, JSON.stringify, etc. Nothing seems to work except hardcoding. I've looked all over the cypress site for an examples of dynamically passing cookies, esp with options, but to no avail. We will need to dynamically set path and names for cookies for other features we test so it would be helpful to know how to do this.
Using cypress ^8.6.0 and chrome 94
** Edit: Adding stack trace:
at <unknown (http://localhost:58166/__cypress/runner/cypress_runner.js:153781:77)From previous event:
at Context.setCookie (http://localhost:58166/__cypress/runner/cypress_runner.js:153970:16)
From Your Spec Code:
at Context.eval (webpack:///./cypress/integration/3-qa-sanity/clinique-sanity.spec.js:6:12)
From Node.js Internals:
Error: true
at Chrome._handleMessage (/Users/rhowk/Library/Caches/Cypress/8.7.0/Cypress.app/Contents/Resources/app/packages/server/node_modules/chrome-remote-interface/lib/chrome.js:256:18)
at WebSocket.<anonymous> (/Users/rhowk/Library/Caches/Cypress/8.7.0/Cypress.app/Contents/Resources/app/packages/server/node_modules/chrome-remote-interface/lib/chrome.js:234:23)
at WebSocket.emit (events.js:376:21)
at Receiver.receiverOnMessage (/Users/rhowk/Library/Caches/Cypress/8.7.0/Cypress.app/Contents/Resources/app/packages/server/node_modules/ws/lib/websocket.js:825:21)
at Receiver.emit (events.js:376:21)
at Receiver.dataMessage (/Users/rhowk/Library/Caches/Cypress/8.7.0/Cypress.app/Contents/Resources/app/packages/server/node_modules/ws/lib/receiver.js:437:15)
at <unknown> (/Users/rhowk/Library/Caches/Cypress/8.7.0/Cypress.app/Contents/Resources/app/packages/server/node_modules/ws/lib/receiver.js:394:24)
at <unknown> (/Users/rhowk/Library/Caches/Cypress/8.7.0/Cypress.app/Contents/Resources/app/packages/server/node_modules/ws/lib/permessage-deflate.js:308:10)
at <unknown> (/Users/rhowk/Library/Caches/Cypress/8.7.0/Cypress.app/Contents/Resources/app/packages/server/node_modules/ws/lib/permessage-deflate.js:391:8)
at afterWrite (internal/streams/writable.js:466:6)
at onwrite (internal/streams/writable.js:446:8)
at InflateRaw.afterTransform (internal/streams/transform.js:103:4)
at Zlib.processCallback (zlib.js:586:9)
From previous event:
at $Cypress.automation (http://localhost:58166/__cypress/runner/cypress_runner.js:166441:13)
at automate (http://localhost:58166/__cypress/runner/cypress_runner.js:153732:23)
at automateCookies (http://localhost:58166/__cypress/runner/cypress_runner.js:153746:13)
at Context.setCookie (http://localhost:58166/__cypress/runner/cypress_runner.js:153967:15)
at <unknown> (http://localhost:58166/__cypress/runner/cypress_runner.js:168605:18)
From previous event:
at runCommand (http://localhost:58166/__cypress/runner/cypress_runner.js:168584:11)
at next (http://localhost:58166/__cypress/runner/cypress_runner.js:168727:17)
From previous event:
at <unknown> (http://localhost:58166/__cypress/runner/cypress_runner.js:182911:80)
From previous event:
at Object.run (http://localhost:58166/__cypress/runner/cypress_runner.js:182906:24)
at Object.run (http://localhost:58166/__cypress/runner/cypress_runner.js:168782:18)
at $Cy.cy.<computed> [as setCookie] (http://localhost:58166/__cypress/runner/cypress_runner.js:169952:20)
at Context.runnable.fn (http://localhost:58166/__cypress/runner/cypress_runner.js:170176:24)
at callFn (http://localhost:58166/__cypress/runner/cypress_runner.js:122963:22)
at Hook.../driver/node_modules/mocha/lib/runnable.js.Runnable.run (http://localhost:58166/__cypress/runner/cypress_runner.js:122950:8)
at <unknown> (http://localhost:58166/__cypress/runner/cypress_runner.js:176739:31)
From previous event:
at Object.onRunnableRun (http://localhost:58166/__cypress/runner/cypress_runner.js:176724:20)
at $Cypress.action (http://localhost:58166/__cypress/runner/cypress_runner.js:166256:29)
at Hook.Runnable.run (http://localhost:58166/__cypress/runner/cypress_runner.js:174428:14)
at next (http://localhost:58166/__cypress/runner/cypress_runner.js:123465:11)
at <unknown> (http://localhost:58166/__cypress/runner/cypress_runner.js:123509:6)
at timeslice (http://localhost:58166/__cypress/runner/cypress_runner.js:117435:28
Here are some refs to relevant functions in the cypress_runner.js webpack:
LN 153730
return automateCookies('set:cookie', cookie, options._log, options.timeout).then(pickCookieProps).then(resp => {
options.cookie = resp;
return resp;
}).catch(handleBackendError('setCookie', 'setting the requested cookie in', onFail));
},
LN 153889
setCookie(name, value, options = {}) {
const userOptions = options;
options = lodash__WEBPACK_IMPORTED_MODULE_0___default.a.defaults({}, userOptions, {
name,
value,
path: '/',
secure: false,
httpOnly: false,
log: true,
expiry: _cypress_utils__WEBPACK_IMPORTED_MODULE_2__[/* default */ "a"].addTwentyYears(),
timeout: config('responseTimeout')
});
const cookie = pickCookieProps(options)
This is a tough one to debug for me, but I am new to cypress so unfamiliar with how everything hangs together right now.
Upvotes: 0
Views: 600
Reputation: 5441
You should name the file auth.json
and require it with the extension specified so that correct conversions can happen behind the scenes.
const auth = require('../../fixtures/auth.json')
/cypress/fixtures/auth.json
{
"cookies": [
{"key": "da_key", "value": "da_value", "path": "da_path", "name": "da_name"}
]
}
Upvotes: 1