Search code examples
nightwatch.js

Test page title with NightwatchJS


I need to check if the title of a page contains "Homepage".

I try the following browser.expect.element('title').text.to.contain('Homepage'); but the element title always comes back empty.

Any other element works but title seems to behave differently. How can I check for a substring?

Example:

Works:

browser
  .url('https://stackoverflow.com')
  .waitForElementVisible('body', 2000)
  .assert.title('Stack Overflow - Where Developers Learn, Share, & Build Careers');

Doesn't work:

browser
  .url('https://stackoverflow.com')
  .waitForElementVisible('body', 2000)
  .expect.element('title').text.to.contain('Developers');

Output: Expected element <title> text to contain: "Developers" - expected "contain 'Developers'" but got: ""

Yet this works: browser.expect.element('h1').text.to.contain('Learn, Share, Build');

It seems only visible elements can be tested, so I'm not sure how I can check something hidden.


Solution

  • Selenium only interacts with visible elements.

    This works:

       browser.getTitle(function(title) {
         this.assert.ok(title.includes("Homepage"));
       });