0

My Chai assertion doesn't seem to fail when using the below async command:

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

Exception message being outputted to the console window:

[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' }
SamP
  • 417
  • 5
  • 24
  • It seems right to me, your expected text `Thank You for your Message!2` is different from the content of the `#contact_reply` element which is `Thank You for your Message!`. Notice the `2` at the end of `return expect(value).to.equal("Thank You for your Message!2")` – Gustavo Fonseca Feb 23 '19 at 17:21
  • Thanks Gustavo Fonseca but I would expect my test to fail with the above code however it seems to indicate my tests are passing, but unsure why :/ – SamP Feb 23 '19 at 19:30

1 Answers1

1

In order for your test to fail the testrunner (I'm assuming it's webdriverio) has to receive an AssertionError. In your test that error will be thrown by chai in this line:

expect(value).to.equal("Thank You for your Message!2");

But you catch it here:

catch(e) {
        console.log(e)
    } 

so it will never get to the testrunner and the test will not fail. Try this code instead:

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

or if you don't need to print the error:

async confirmSuccessfulSubmission() {
    let value = await $("#contact_reply h1").getText()
    expect(value).to.equal("Thank You for your Message!2"); 
}
Gilad Shnoor
  • 374
  • 3
  • 12