【发布时间】:2022-01-19 12:58:44
【问题描述】:
我有 2 个域。对第一个执行的某些操作会导致在它们两个上设置相同的 cookie。我需要在两个域上读取此 cookie 的值。问题是,然而这在第一个是可能的,在导航到第二个之后,cookie 是存在的,但它的值是空的。
为什么?
获取 cookie 值的代码如下:
const getCookie = ClientFunction(() => {
const name = 'ConfigCookie';
const match = document.cookie.match(new RegExp(name + '=([^;]+)'));
let decodedValue;
if (match) decodedValue = decodeURIComponent(match[1]).replace(/%28/g, '(').replace(/%29/g, ')');
return JSON.parse(decodedValue || '');
})
这是测试代码(我删除了敏感数据)
test('xyz', async t => {
await t
.navigateTo(FirstDomain)
.click(firstDomainSubmitButtonSelector)
const firstDomainCookie = await getCookie();
const firstDomainConsents = firstDomainCookie.consents;
await t
.expect(consents).eql({here the expected value});
await t
.navigateTo(SecondDomain)
const secondDomainCookie = await getCookie();
const secondDomainConsents = secondDomainCookie.consents;
console.log(secondDomainConsents)
})
【问题讨论】:
-
据我记得它与安全相关,例如 JS 只能从其运行的域/站点读取 cookie 信息。否则任何恶意代码都可以读取任何内容的 cookie 内容
标签: typescript testing cookies automated-tests testcafe