TL;DR
当Cypress抛出以下错误时,它到底为什么感到难过:
! CypressError
cy.setCookie() had an unexpected error setting the requested cookie in Chrome.
> Error: Sanitizing cookie failed
详细描述
我有一个从.txt
-文件设置一堆cookie的函数。这样我就可以通过执行此功能并设置所有必要的cookie来"登录"。
但当我这样做时,Cypress抛出了一个我不理解的错误(请参阅上面TL;DR部分中的错误)。
堆栈跟踪:
at <unknown> (http://mylocaltestdomain.test/__cypress/runner/cypress_runner.js:139868:76)
From previous event:
at <unknown> (http://mylocaltestdomain.test/__cypress/runner/cypress_runner.js:140071:17)
at <unknown> (http://mylocaltestdomain.test/__cypress/runner/cypress_runner.js:152530:14)
From previous event:
at $Cy.retryIfCommandAUTOriginMismatch (http://mylocaltestdomain.test/__cypress/runner/cypress_runner.js:152529:25)
at Context.setCookie (http://mylocaltestdomain.test/__cypress/runner/cypress_runner.js:140068:17)
at wrapped (http://mylocaltestdomain.test/__cypress/runner/cypress_runner.js:157849:43)
From Your Spec Code:
at Context.eval (webpack:///./cypress/e2e/preparations/prep-wp-login-and-cookies.cy.js:40:6)
From Node.js Internals:
Error: Sanitizing cookie failed
at <unknown> (<embedded>:708:509041)
at _._handleMessage (<embedded>:708:511054)
at A.<anonymous> (<embedded>:708:510873)
at A.emit (node:events:527:28)
at g.B (<embedded>:708:496211)
at g.emit (node:events:527:28)
at g.dataMessage (<embedded>:708:476631)
at <unknown> (<embedded>:708:476164)
at <unknown> (<embedded>:708:467511)
at <unknown> (<embedded>:708:468452)
at afterWrite (node:internal/streams/writable:497:5)
我的代码
Cypress.Commands.add( "setWordPressAuthCookies", () => {
let authCookiesPath = 'development/tmp/authCookies.txt';
cy.fixture( authCookiesPath, 'utf8' )
.then( (cookies) => {
cookies.forEach( (cookie) => {
// cy.log( JSON.stringify( cookie ) ); // See the cookie contents
cy.setCookie( cookie.name, cookie.value, {
domain: Cypress.env('currentDomain'),
path: cookie.path,
secure: cookie.secure,
httpOnly: cookie.httpOnly,
expiry: cookie.expiry
});
});
});
});
cookie的内容显示错误:
{
"name": "wp-settings-12",
"value": "editor%3Dhtml%26ampampampnetwork_users_list_mode%3Dlist%26ampampamplibraryContent%3Dbrowse%26ampamplibraryContent%3Dbrowse%26ampamphidetb%3D0%26ampampmfold%3Do%26ampampimgsize%3Dfull%26ampampalign%3Dleft%26libraryContent%3Dbrowse%26advImgDetails%3Dshow",
"path": "/",
"secure": false,
"httpOnly": false,
"expiry": 1703771330.714386,
"domain": "mylocaltestdomain.text"
}
所有cookie
以下是此功能应设置的所有cookie:
[
{
"name": "wp-settings-12",
"value": "editor%3Dhtml%26ampampampnetwork_users_list_mode%3Dlist%26ampampamplibraryContent%3Dbrowse%26ampamplibraryContent%3Dbrowse%26ampamphidetb%3D0%26ampampmfold%3Do%26ampampimgsize%3Dfull%26ampampalign%3Dleft%26libraryContent%3Dbrowse%26advImgDetails%3Dshow",
"path": "/",
"secure": false,
"httpOnly": false,
"expiry": 1703771330.714386,
"domain": "mylocaltestdomain.test"
},
{
"name": "wp-settings-time-12",
"value": "1672235330",
"path": "/",
"secure": false,
"httpOnly": false,
"expiry": 1703771330.714419,
"domain": "mylocaltestdomain.test"
},
{
"name": "wordpress_80f89aaabbb62cf5ead75a68566e5b74",
"value": "my_wordpress_username%7C1672408128%7CrnwumENI8t74dgQmv0tsY98tDRqGdxSR09t2BA61Lt3%7C744e72790320da01c1e359610e45b697272223cf3031a84cf9728e26d88e5af8",
"path": "/wp-content/plugins",
"secure": false,
"httpOnly": true,
"domain": "mylocaltestdomain.test"
},
{
"name": "wordpress_test_cookie",
"value": "WP%20Cookie%20check",
"path": "/",
"secure": false,
"httpOnly": false,
"domain": "mylocaltestdomain.test"
},
{
"name": "wordpress_80f89aaabbb62cf5ead75a68566e5b74",
"value": "my_wordpress_username%7C1672408128%7CrnwumENI8t74dgQmv0tsY98tDRqGdxSR09t2BA61Lt3%7C744e72790320da01c1e359610e45b697272223cf3031a84cf9728e26d88e5af8",
"path": "/",
"secure": false,
"httpOnly": true,
"domain": "mylocaltestdomain.test"
},
{
"name": "wordpress_logged_in_80f89aaabbb62cf5ead75a68566e5b74",
"value": "my_wordpress_username%7C1672408128%7CrnwumENI8t74dgQmv0tsY98tDRqGdxSR09t2BA61Lt3%7C11d4ba33c9b1116655e41449a92f98811579707cfec296866bed8619fdbfeb8a",
"path": "/",
"secure": false,
"httpOnly": true,
"domain": "mylocaltestdomain.test"
}
]
我在几个不同的网站上工作。然而,这是我第一次尝试在多站点上这样做。
赛普拉斯到底是怎么伤心的?
初始解决方案尝试
我首先尝试了显而易见的:
- 重新启动Cypress
- 重新生成
authCookies.txt
-文件,然后重试
没有变化。
解决方案尝试1:阅读文档链接
错误指向以下URL:https://docs.cypress.io/api/commands/setcookie.但我在里面找不到关于这个错误的任何信息。没有消毒或其他什么。
我还尝试再次生成authCookies.txt-文件(这是另一个函数)。但我得到了同样的结果。
解决方案尝试2:跳过(麻烦的)cookie
我尝试更改cookie的"设置"以跳过此处列出的设置,如下所示:
Cypress.Commands.add( "setWordPressAuthCookies", () => {
let authCookiesPath = 'development/tmp/authCookies.txt';
cy.fixture( authCookiesPath, 'utf8' )
.then( (cookies) => {
cookies.forEach( (cookie) => {
// cy.log( JSON.stringify( cookie ) ); // See the cookie contents
if( ! ['wp-settings-12'].includes(cookie.name) ){ // <-- MY ATTEMPTED FIX START
cy.setCookie( cookie.name, cookie.value, {
domain: Cypress.env('currentDomain'),
path: cookie.path,
secure: cookie.secure,
httpOnly: cookie.httpOnly,
expiry: cookie.expiry
});
} // <-- MY ATTEMPTED FIX END
});
});
});
它只是为下一个(名称为:wp-settings-time-12
)抛出相同的错误
如果跳过,则为wordpress_80f89aaabbb62cf5ead75a68566e5b74
。
解决方案尝试3:通过JavaScript(而不是Cypress)设置cookie
这似乎是一个过度的操作,重写函数来完成此操作
我希望相信赛普拉斯,而不仅仅是找到奇怪的捷径。
解决方案尝试4:更新Cypress
我使用的是Cypress版本11.2.0
我已更新为12.2.0
。
同样的结果。
解决方案尝试5:不同的浏览器
我厌倦了Firefox开发者版,得到了一个稍微不同的错误:
setCookie wordpress_test_cookie, WP%20Cookie%20check, {domain: mylocaltestdomain.test/subsite-path}
CypressError
cy.setCookie() had an unexpected error setting the requested cookie in Firefox Developer Edition.
> Error: Permission denied to set cookie {"domain":"mylocaltestdomain.test/subsite-path","expirationDate":2303459061,"firstPartyDomain":null,"httpOnly":false,"name":"wordpress_test_cookie","partitionKey":null,"path":"/","sameSite":"no_restriction","secure":false,"storeId":null,"url":"http://mylocaltestdomain.test/subsite-path/","value":"WP%20Cookie%20check"}
也许这与subsite-path
有关。嗯。。。!
我的罪魁祸首是fixture文件末尾的一行新行。由于用户配置或.editorconfig
规则的原因,这些通常是IDE在保存时自动添加的。