javascript - 如何使用 TestCafe 对所有测试进行身份验证

标签 javascript cookies e2e-testing testcafe user-acceptance-testing

我知道这个话题讨论得很多,但我的情况很特殊。

为了测试我们的验收环境,我们需要点击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/

相关文章:

javascript - 将新项目附加到 Vue 2 中的列表

javascript - html css 和 js 不工作

javascript - 在不刷新页面的情况下更新主题颜色元

ios - 如何在 Swift 中使用 UIWebView 管理 cookie

javascript - cookie 10 秒后不会过期

testing - "Mock"带有 Protractor、Cucumber 和 Chai 的后端 http 响应

javascript - Angular JS IE9 Hashbang url 重写

tomcat - ColdFusion 11 - 来自非 ASCII Cookie 字符的 500 内部服务器错误

angular - 带 Headless Chrome 的 Protractor 无法在 AWS CodeBuild 上运行,但可以在本地运行

java - bazel - 测试运行时的可写归档路径