在使用变量设置cookies选项时遇到问题,我们使用需要安全的cookies和密码,即不在repo中,所以我们将它们存储在auth.json文件中,我们让工程师在他们各自的实现中从安全文档中编写该文件。所以我们从文档中提取数据,我尝试这样动态地编写cookie:
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} )
})当我硬编码路径时,上述方法有效,但当我尝试使用以下命令从json中拉入路径时:
cy.setCookie(`'${auth.cookies[0].key}'`, `'${auth.cookies[0].value}'`,{ path: `'${auth.cookies[0].path}'`} )我得到了这个错误:

json如下所示:
"cookies": [
{"key": "da_key", "value": "da_value", "path": "da_path", "name": "da_name"},
...使用cypress ^8.6.0和chrome 94
**编辑:添加堆栈跟踪:
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下面是对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) 发布于 2021-10-26 00:33:29
您应该将文件命名为auth.json,并要求使用指定的扩展名,以便可以在幕后进行正确的转换。
const auth = require('../../fixtures/auth.json')/cypress/fixtures/auth.json
{
"cookies": [
{"key": "da_key", "value": "da_value", "path": "da_path", "name": "da_name"}
]
}https://stackoverflow.com/questions/69710924
复制相似问题