首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用TestCafe对所有测试进行身份验证

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

Stack Overflow用户
提问于 2018-10-25 02:45:21
回答 1查看 697关注 0票数 2

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

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

在尝试了许多选项之后,我能得到的最接近的解决方案是使用一个角色,比如

代码语言:javascript
复制
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;

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

如果我从角色中删除{ preserveUrl: true },example2和example3会加载空白页面。如果我从测试中删除.disablePageReloads,则escond和第三个测试的身份验证失败,并收到错误"Failed to find a DNS-record for the resource for the resource at...“。另外,如果我的角色是

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

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

我注意到,当我成功工作时,我应该获得的cookie实际上在调试时被保存在application下的会话中,并且被这个锤头存储包装器更改。测试时的基本url是前面有随机字母的testCafe服务器的ip和端口,例如172.0.0.1:8080/hoi23hh/https://acceptance-site.com,这会导致我的cookie保存在会话中(而不是在不使用test cafe时通常出现的cookie下),作为url。

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

因此,url中的"hoi23hh“变为"ohgbo223”,并且cookie/会话密钥不再与url匹配,并且认证失败。

对于持久化身份验证,同时仍然能够更改页面等,您有什么建议

EN

回答 1

Stack Overflow用户

发布于 2018-10-25 18:07:20

我不建议对角色使用disablePageReloads功能,因为它是内部的,可能不稳定。preserveUrl option将允许每个测试从https://acceptance-site.com页面开始。因此,我认为这是最适合您的方式::

代码语言:javascript
复制
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();
})
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52975982

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档