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

标签 javascript testing automated-tests e2e-testing testcafe

我正在尝试在页面对象中创建一个方法来执行我最终会经常使用的特定测试。我已经关注了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(...)

最佳答案

我在你的代码中发现了一些错误。请检查一下。 1) isRequired 方法不返回任何内容,这就是为什么你得到 undefined 的原因。 2) 我认为您不需要将 isRequired 方法包装在单独的 expect 调用中。只需编写 await 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>

测试代码:

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);
});

关于javascript - 在页面对象方法内共享断言,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54936657/

相关文章:

testing - TFS 用户只允许创建错误

testing - 拖动点不起作用并且不会引发错误

Selenium、Siebel 和 QTP

testing - 是否有 testNG 报告存储库

javascript - 使用 Javascript 调整图像大小而不在 DOM 上渲染 Canvas

javascript - 一个轻松创建 html 日历表的插件

testing - 与质量保证相比,什么是质量检查?

javascript - 设置间隔和清除间隔。出事了

javascript - PHP:如何使用 simple_html_dom 解析器将 CSS 文本对齐属性添加到元素的已经存在的内联样式属性?

node.js - 测试 Express response locals