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