首页 > 解决方案 > 在 fixture.before 钩子中添加 Login 方法会在 Testcafe 中出现错误

问题描述

议程:我想在所有测试之前运行 login 方法,在所有测试之后运行 Logout 方法,这样如果 before 钩子失败,测试执行就不会发生。

我在 fixture.before 钩子中添加了登录逻辑,如下面的代码所示。但它给出了以下错误,可以帮助我修复它。

在此处输入图像描述

测试文件

import { Selector } from "testcafe";
import LoginPage from '../page-objects/login.po';

const loginPage = new LoginPage();

fixture`Getting Started`
.page`https://example.com/`
.before(async t => {
    await loginPage.login();
});

test("My First Test", async t => {
    const str = await Selector('.home-container h1').textContent;
    console.log(str);
});

页面对象类

import { Selector, t } from 'testcafe';
import CommonFunctions from '../commons/common-fns'

export default class LoginPage{


    constructor () {                 
        this.emailTxtBox = Selector('input[type="email"]');
        this.nextBttn = Selector('button[type="submit"]');
        this.microsoftNextBttn = Selector('input[type="submit"]');
        this.passwordTxtBox = Selector('input[type="password"]');
        this.signinBttn = Selector('input[type="submit"]');
        this.noBttn = Selector('#idBtn_Back');
    }

     async login() {
        await t
        .typeText(this.emailTxtBox, '')
        .click(this.nextBttn)
        .typeText(this.emailTxtBox, '')
        .click(this.microsoftNextBttn)
        .typeText(this.passwordTxtBox, '')
        .click(this.signinBttn)
        .click(this.noBttn);
    }
}

标签: testingautomated-testsui-automationtestcafeui-testing

解决方案



推荐阅读