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

时间:2018-10-24 18:45:21

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

我知道这个话题已经讨论了很多,但是我有一个独特的情况。

为了测试我们的接受环境,我们需要点击https://authentication-example.com,它运行一个脚本来添加会话cookie,从而对我们进行身份验证。然后,我们导航到https://acceptance-site.com以运行测试。

尝试了许多选项之后,最接近解决方案的地方是使用角色 例如

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;

该解决方案使我无法加载任何与角色结尾不同的新页面。

如果我从角色中删除{reserveUrl:true},则example2和example3会加载空白页面。如果我从测试中删除.disablePageReloads,则escond和第三个测试的身份验证将失败,并且会出现错误“无法在...找到资源的DNS记录”。 另外,如果我的角色是

const a3 = Role('https://authentication-example.com', async testController => {
    await testController.navigateTo('https://acceptance-site.com');
}, { preserveUrl: true });

所有测试的身份验证均失败。

我注意到,当成功工作时,我应该获取的cookie实际上在调试时保存在应用程序的会话中,并且由锤头式存储包装程序对其进行了更改。 测试时的基本url是testCafe服务器的ip和端口,后跟随机字母,例如172.0.0.1:8080/hoi23hh/https://acceptance-site.com 这导致我的cookie被保存在会话中(不在不使用测试咖啡馆的情况下通常出现在cookie下) 锤头|存储包装纸| hoi23hh | acceptance-site.com

当我删除.disablePageReloads,但在角色上保留prepareUrl:true时,“ cookie”保持不变,但基本url变为类似 172.0.0.1:8080/ohgbo223 / https://acceptance-site.com

因此,URL中的“ hoi23hh”更改为“ ohgbo223”,并且cookie /会话密钥不再与该URL匹配,并且身份验证失败。

您在保留身份验证的同时还能更改页面等的建议是什么

1 个答案:

答案 0 :(得分:2)

我不建议在角色中使用disablePageReloads功能,因为它是内部功能,并且可能不稳定。 preserveUrl选项将允许每个测试从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();
})
相关问题