在页面对象方法中共享断言

问题描述 投票:2回答:1

我正在尝试在页面对象中创建一个方法,该方法执行特定的测试,我最终会经常使用它。我跟着documentation example,但这是打字/点击,也许不能与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(...)
await page.racTextInput.isRequired();
await t
      .expect(...)

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

await t
      .expect(...)
      .click(...)
      .expect(page.racTextInput.isRequired()).ok()
      .expect(...)
javascript testing automated-tests e2e-testing testcafe
1个回答
3
投票

我在你的代码中发现了一些错误。请检查一下。 1)isRequired方法什么也没有返回,这就是你得到undefined的原因。 2)我认为你不需要在单独的isRequired调用中包装expect方法。它应该只写await page.nameTextInput.isRequired() 3)你错过了t方法中的isRequired参数,但是,我认为这只是一个错字

更新:

测试页面:

<!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);
});
© www.soinside.com 2019 - 2024. All rights reserved.