【问题标题】:Testcafe Cookie handling?Testcafe Cookie 处理?
【发布时间】:2019-07-04 08:27:17
【问题描述】:

我有一个关于 TestCafe 使用的问题。我有一个脚本(夹具),里面有两个测试。如果第一个测试运行 URL 并且如果没有登录,脚本将登录到站点。

但是:第二个测试也总是登录。看起来 Testcafe 无法识别在夹具中完成的 cookie。固定装置或跑步者内的字符串是什么,用于保存设置的 cookie?

import { Selector } from 'testcafe';

fixture `Ordner erstellen`
   .page `xxxx`
   .before(async ctx  => {
      ctx.clLogin = `xxxx`;
      ctx.clPassword = `xxx`;
    });
test('Create and Delete Folder1', async t => {
const testfolder = Selector('.np-folder-name[title="19233456"]')
await t
   .typeText(Selector('#email'), t.fixtureCtx.clLogin, {
      caretPos: 0
   })
   .typeText(Selector('#password'), t.fixtureCtx.clPassword, {
      caretPos: 0
   })
   .click(Selector('span').withText('Login'))
   .click(Selector('.np-top-section-tab.folder'))
await t
   .wait(2000)
   .expect(testfolder.withText('19233456').exists).notOk()

test('Create and Delete Folder2', async t => {
const testfolder = Selector('.np-folder-name[title="19233456"]')
await t
   .typeText(Selector('#email'), t.fixtureCtx.clLogin, {
      caretPos: 0
   })
   .typeText(Selector('#password'), t.fixtureCtx.clPassword, {
      caretPos: 0
   })
   .click(Selector('span').withText('Login'))
   .click(Selector('.np-top-section-tab.folder'))
await t
   .wait(2000)
   .expect(testfolder.withText('19233456').exists).notOk()

我还尝试了 testcafe 的角色概念。但这也不好用。

import { Selector, Role } from 'testcafe';



const admin = Role('https://bc3-channel.cliplister.com/', async t => {
    await t
        .typeText(Selector('#email'), `xxx`, {
            caretPos: 0
        })
        .typeText(Selector('#password'), `xxx`, {
            caretPos: 0
        })
        .click(Selector('span').withText('Login'))
});

fixture `Ordner erstellen`
    .page `xxx`


test('Create and Delete Folder', async t => {
    const testfolder = Selector('.np-folder-name[title="19233456"]')
    await t
        .useRole(admin)
        .navigateTo('xxx')
        .click(Selector('.np-top-section-tab.folder'))

});


test('Create and Delete Folder2', async t => {
    const testfolder = Selector('.np-folder-name[title="19233456"]')
    await t
        .navigateTo('xxx')
        .click(Selector('.np-top-section-tab.folder'))
    await t
        .wait(2000)
        .expect(testfolder.withText('19233456').exists).notOk()

});

【问题讨论】:

标签: cookies automated-tests e2e-testing web-testing testcafe


【解决方案1】:

TestCafe 中的每个测试都从干净的 cookie 和存储开始。如果您希望保持登录状态,则需要在每个测试中使用 useRole 调用。useRole 函数会在第二次和后续调用中恢复第一次调用时保存的 cookie 和存储。您可以在 beforeEach 钩子中调用 useRole 函数,而不是在每个测试中手动编写它。

User Roles

Test Hooks

【讨论】:

    【解决方案2】:

    这是您可以使用 Role API 解决的方法。

    使用您的登录操作创建一个 Login.js 页面对象文件

    const adminLogin = Role('https://bc3-channel.cliplister.com/', async t => {
        await t
            .typeText(Selector('#email'), `xxx`, {
                caretPos: 0
            })
            .typeText(Selector('#password'), `xxx`, {
                caretPos: 0
            })
            .click(Selector('span').withText('Login'))
    });
    

    然后在你的fixture文件中调用这个const adminLogin,如下所示:fixture.js

    import { adminLogin } from '../page-objects/login';
    fixture `Ordner erstellen`.beforeEach(async t => {
      await t.useRole(adminLogin ).navigateTo('url of the page that you want to open');
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-04-05
      • 2019-11-18
      • 1970-01-01
      • 2012-01-29
      • 1970-01-01
      • 2018-12-08
      • 1970-01-01
      • 2020-05-14
      相关资源
      最近更新 更多