首页 > 解决方案 > Testcafe Role 构造函数不执行认证功能

问题描述

我的 Testcafe 测试目前无法运行,因为我定义的角色不会初始化。

我有一个功能,我想在登录过程中检索数据。但似乎整个登录逻辑在 Role 构造函数中被跳过。

我有一个自定义角色界面:

interface MyRole extends Role {
   clientName?: string;
   aodRoles?: string[];
   sub?: string;
}

以及处理登录过程的函数:

// before role initialization
console.log( 'gets executed' );

const role: MyRole = Role( url, async t => {
         // everything in here is skipped...
         console.log( "Starting login" );
  
         userInfo = await this.login( t, name );
  
         console.log( "Login finished" );
        },
        // As the initial opening of the given url will automatically redirect to the openid-provider, it will never be of use.
        // Therefore, we keep the url after login
        { preserveUrl: true }
      );

console.log( 'gets also executed' );

role实例已初始化,但此处作为参数传递的整个函数将被忽略。有人知道什么会导致这种行为吗?

标签: typescripttestingautomated-testse2e-testingtestcafe

解决方案


我试图重现这个问题,但我这边一切正常。这是我的代码:

import { Selector, Role } from 'testcafe';

interface MyRole extends Role {
    clientName?: string;
    aodRoles?: string[];
    sub?: string;
}

const developerName = Selector('#developer-name');
 
console.log( 'gets executed' );

const role: MyRole = Role('http://devexpress.github.io/testcafe/example/', async t => {
    // everything in here is skipped...
    console.log( "Starting login" );

    //userInfo = await this.login( t, name );

    console.log( "Login finished" );
  },
  // As the initial opening of the given url will automatically redirect to the openid-provider, it will never be of use.
  // Therefore, we keep the url after login
  { preserveUrl: true }
);

fixture `My Fixture`;

test('My test', async t => {
  await t
      .useRole(role)
      .typeText(developerName, 'Peter')
      .typeText(developerName, 'Paker', { replace: true })
      .typeText(developerName, 'r', { caretPos: 2 })
      .expect(developerName.value).eql('Parker');
});

控制台输出包含以下几行:

被执行

开始登录

登录完成

您是否在测试中使用通过 .useRole 方法创建的角色?你会检查我的例子在你身边是否正常工作吗?


推荐阅读