【问题标题】:How to Authenticate for all tests using TestCafe如何使用 TestCafe 对所有测试进行身份验证
【发布时间】:2018-10-24 18:45:20
【问题描述】:

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

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

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

如果我从角色中删除 { preserveUrl: 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,但保留角色上的 preserveUrl: true 时,“cookie”保持不变,但基本 url 更改为类似 172.0.0.1:8080/ohgbo223/https://acceptance-site.com

所以 url 中的“hoi23hh”更改为“ohgbo223”,cookie/会话密钥不再匹配 url,身份验证失败。

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

【问题讨论】:

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


    【解决方案1】:

    我不建议将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();
    })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-12-08
      • 1970-01-01
      • 2016-05-05
      • 1970-01-01
      • 2014-12-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多