我知道这个话题讨论得很多,但我的情况很特殊。
为了测试我们的验收环境,我们需要点击https://authentication-example.com ,它运行一个脚本来添加 session cookie,从而对我们进行身份验证。然后我们导航到https://acceptance-site.com运行测试。
在尝试了很多选项之后,我能找到的最接近的解决方案是使用 Role 比如
const a3 = Role('https://acceptance-site.com', async testController => {
await testController.navigateTo('https://authentication-example.com');
await testController.navigateTo('https://acceptance-site.com');
}, { preserveUrl: true });
fixture('Testing acceptance')
.beforeEach(async testController => {
await testController.useRole(authenticate);
});
test('example1', async testController => {
await testController.debug();
}).disablePageReloads;
test('example2', async testController => {
await testController.debug();
}).disablePageReloads;
test('example3', async testController => {
await testController.debug();
}).disablePageReloads;
该解决方案使我无法加载任何与 Angular 色结束时不同的新页面。
如果我从 Angular 色中删除 { preserveUrl: true },example2 和 example3 加载空白页。如果我从测试中删除 .disablePageReloads,则第二次和第三次测试的身份验证将失败,并且我会收到错误消息“无法找到位于...的资源的 DNS 记录” 另外,如果我的 Angular 色是
const a3 = Role('https://authentication-example.com', async testController => {
await testController.navigateTo('https://acceptance-site.com');
}, { preserveUrl: true });
所有测试的身份验证均失败。
我注意到,当工作成功时,我应该得到的cookie实际上在调试时保存在应用程序下的 session 中,并且被这个锤头存储包装器改变了。 测试时的基本 url 是 testCafe 服务器的 ip 和端口,前面是随机字母,例如 172.0.0.1:8080/hoi23hh/https://acceptance-site.com 这导致我的 cookie 被保存在 session 中(而不是在不使用 test cafe 时通常发生的 cookie 下)作为 hammerhead|storage-wrapper|hoi23hh|acceptance-site.com
当我删除 .disablePageReloads,但在 Angular 色上保留 preserveUrl: true 时,“cookie”保持不变,但基本 url 更改为类似 172.0.0.1:8080/ohgbo223/https://acceptance-site.com
所以 url 中的“hoi23hh”更改为“ohgbo223”,cookie/ session key 不再与 url 匹配,身份验证失败。
对于在保持身份验证的同时仍然能够更改页面等,您有什么建议
最佳答案
我不建议对 Angular 色使用 disablePageReloads
功能,因为它是内部的并且可能不稳定。
preserveUrl option将允许每个测试从 https://acceptance-site.com
页面开始。所以,我认为这是你的情况下最可取的方式::
const a3 = Role('https://acceptance-site.com', async testController => {
...
}, { preserveUrl: true });
fixture('Testing acceptance')
.beforeEach(async testController => {
await testController.useRole(authenticate);
});
test('example1', async testController => {
await testController.debug();
})
test('example2', async testController => {
await testController.debug();
})
关于javascript - 如何使用 TestCafe 对所有测试进行身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52975982/