question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Nightwatch "Expect" API assertions are not working

See original GitHub issue

I’m submitting a … (check one with “x”)

[x] bug report
[ ] feature request
[ ] support request => Please do not submit support request here, instead use https://groups.google.com/d/forum/nightwatch-cucumber

Current behavior The step using “Expect” assertions API always succeeds.

Expected/desired behavior The step using “Expect” assertions API should fail in case of not met condition.

Reproduction of the problem

Scenario: Home page greets politely
  Then the page should contain text "Hello World!"
 
Then(/^the page should contain text "(.*?)"$/, (text) => {
        return client.expect.element('body').text.to.contain(text);
}); 

What is the expected behavior? Nightwatch “Expect” assertions API should work the same as “Assert” API

What is the motivation / use case for changing the behavior? “Expect” API is very easy to use and favored by Nightwatch team.

Please tell us about your environment:

  • nightwatch-cucumber version: 7.0.7

  • nightwatch version: 0.9.12

  • cucumber version: 2.0.0-rc.6

  • Node.js version: 6.9.4

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
mykltcommented, Feb 11, 2017

Hi @mucsi96 !

I’ve investigated this issue as well. So you are right, that Nightwatch assertions do not throw exceptions. But the difference between “assert” and “expect” APIs is that the later does not return client’s api (promisified one) after adding command to CommandQueue.

Having in mind above findings, I was able to use below workaround:

Then(/^the page should contain text "(.*?)"$/, (text) => {
  return client.perform(() => {
    client.expect.element('body').text.to.contain(text);
  })
})

“perform” fits perfectly here because even in Nightwatch API docs it is written that it:

allows access to the “api” in a callback. Can be useful if you want to read variables set by other commands.

On the other hand, it would be much better to have ability to assert using Expect API in some terser way. Before closing this issue, I will spend some more time on investigation whether I could come up with any terser approach. If you have any ideas, please share.

Thanks!

0reactions
mykltcommented, Feb 11, 2017

Hi @mucsi96 , I found one more issue related to the same expect api and raised a pull request. Could you please review it?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Nightwatch assertions | Developer Guide
The expect assertions use a subset of the Expect api from the Chai framework and at this point are available for elements, cookies,...
Read more >
expect.element().to.not.be.present throws error when using ...
Describe the bug When testing assertion that element is not present on page, error is thrown when element is not found.
Read more >
Expect Assertions not working in Nightwatch - Stack Overflow
All other Nightwatch commands and assertions are working, but when I try to use Expect assertions I get an error: .expect.element(.
Read more >
Fixing 'expect' assertions in Nightwatch.js | by Simon W - ITNEXT
Now all of your expect API assertions will be functions, rather than property accessors! That original test snippet will now look like this:...
Read more >
Chapter 5.1 - Expect and Assert - Test Automation University
Chapter 1 - Installing & Configuring Nightwatch.js · Chapter 2 - Test Runners · Chapter 3.1 - Finding Elements · Chapter 3.2 -...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found