Javascript 在页面对象方法内共享断言

Javascript 在页面对象方法内共享断言,javascript,testing,automated-tests,e2e-testing,testcafe,Javascript,Testing,Automated Tests,E2e Testing,Testcafe,我试图在page对象中创建一个方法,该方法执行一个特定的测试,最终我会经常使用它。我遵循了,但那是用于键入/单击的,可能无法使用expect AssertionError: expected undefined to be truthy 该错误特别指向我的测试中的这一行: await t.expect(await page.nameTextInput.isRequired()).ok() 它在我在页面对象模型中使用的“TextInputFeature”中调用isRequired检查: exp

我试图在page对象中创建一个方法,该方法执行一个特定的测试,最终我会经常使用它。我遵循了,但那是用于键入/单击的,可能无法使用
expect

AssertionError: expected undefined to be truthy
该错误特别指向我的测试中的这一行:

await t.expect(await page.nameTextInput.isRequired()).ok()
它在我在页面对象模型中使用的“TextInputFeature”中调用
isRequired
检查:

export default class TextInputFeature {
    constructor(model) {
        this.input = AngularJSSelector.byModel(model);
        this.label = this.input.parent().prevSibling('label');
        this.asterisk = this.label.find('.required');
    }

    async isRequired() {
        await t
            .expect(this.input.hasAttribute('required')).ok()
            .expect(this.asterisk.exists).ok();
    }
}
编辑:以下“作品”:

…但我的目标是允许链接:

await t
      .expect(...)
      .click(...)
      .expect(page.racTextInput.isRequired()).ok()
      .expect(...)

我在你的代码中发现了一些错误。请检查一下。 1)
isRequired
方法不返回任何内容,这就是为什么您得到了
undefined
。 2) 我认为您不需要在单独的
expect
调用中包装
isRequired
方法。只写
wait page.nameTextInput.isRequired()
3) 您错过了
isRequired
方法中的
t
参数,但是,我认为这只是一个输入错误

更新:

测试页面:

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
    <label>Name
    <div class="required">*</div>
    </label>
    <input required="required" type="text" id="name">
</body>
</html>

我遵循了这个例子,但我也看到了同样的方法。1) async方法使用
await
并且不需要返回,事实上eslint要求我这样做2)我希望它被链接,这是我的目标。3) 根据文档,
t
被导入,而不是作为参数传递。我已经修改了我的答案,以演示我建议的方法
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
    <label>Name
    <div class="required">*</div>
    </label>
    <input required="required" type="text" id="name">
</body>
</html>
import { Selector } from 'testcafe';

class TextInputFeature {
    constructor () {
        this.input    = Selector('input#name');
        this.label    = Selector('label');
        this.asterisk = this.label.find('.required');
    }

    async isRequired () {
        const hasRequiredAttribute = await this.input.hasAttribute('required');
        const asteriskExists       = await this.asterisk.exists;

        return hasRequiredAttribute && asteriskExists;
    }
}

fixture`fixture`
    .page`../pages/index.html`;

test(`test`, async t => {
    const inputFeature = new TextInputFeature();

    await t
        .click(inputFeature.label)
        .expect(await inputFeature.isRequired()).ok()
        .click(inputFeature.label);
});