【发布时间】:2021-12-11 02:35:20
【问题描述】:
在使用变量设置 cookie 选项时遇到问题,我们使用需要安全的 cookie 和密码,即不在存储库中,因此我们将它们存储在 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"},
...
我尝试过各种单引号、双引号、JSON.stringify 等的组合。除了硬编码外,似乎没有任何效果。我在 cypress 网站上查看了动态传递 cookie 的示例,尤其是带有选项的示例,但无济于事。我们需要为我们测试的其他功能动态设置 cookie 的路径和名称,因此了解如何执行此操作会很有帮助。
使用 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)
这对我来说很难调试,但我是 cypress 的新手,所以不熟悉现在所有东西是如何连接在一起的。
【问题讨论】:
标签: node.js cookies cypress options setcookie