使用以下异步命令时,我的Chai断言似乎不会失败:
async confirmSuccessfulSubmission() {
try {
let value = await $("#contact_reply h1").getText()
if(value)
return expect(value).to.equal("Thank You for your Message!2");
return null
} catch(e) {
console.log(e)
}
}
异常消息将输出到控制台窗口:
[0-0] { AssertionError: expected 'Thank You for your Message!' to equal 'Thank You for your Message!2'
at ContactUs_Page.confirmSuccessfulSubmission (C:\Users\GBruno\Desktop\webdriverioFramework\pageObjects\ContactUs_Page.js:51:34)
at <anonymous>
at process._tickDomainCallback (internal/process/next_tick.js:229:7)
message: 'expected \'Thank You for your Message!\' to equal \'Thank You for your Message!2\'',
showDiff: true,
actual: 'Thank You for your Message!',
expected: 'Thank You for your Message!2' }
答案 0 :(得分:1)
为了使您的测试失败,testrunner(我假设它是webdriverio)必须收到AssertionError。在您的测试中,chai将在此行中引发错误:
expect(value).to.equal("Thank You for your Message!2");
但是你在这里抓到了
catch(e) {
console.log(e)
}
因此它将永远不会到达测试运行器,并且测试不会失败。请尝试以下代码:
async confirmSuccessfulSubmission() {
try {
let value = await $("#contact_reply h1").getText()
expect(value).to.equal("Thank You for your Message!2");
} catch(e) {
console.log(e)
throw(e)
}
}
或者如果您不需要打印错误:
async confirmSuccessfulSubmission() {
let value = await $("#contact_reply h1").getText()
expect(value).to.equal("Thank You for your Message!2");
}