What is the proper way to wait for a new URL to load in nightwatch?

Bryn Wolfe picture Bryn Wolfe · Apr 4, 2016 · Viewed 8.6k times · Source

A page I am testing has a button that takes you to a different page on the same site. After clicking on that button, I want to wait for that page to load before continuing. Normally, I would just wait for some element on that page to load, but since I recently updated nightwatch/selenium, that waitForElementPresent() test has stopped working. In the process of debugging the problem, I thought it made sense to wait for the new URL to load, but I don't see a nightwatch way to do that. I can hard code a wait with a pause() followed by an assert.urlContains(), but there's got to be a better way. Any suggestions?

What used to work:

this.waitForElementVisible(runCSS,3000)
    .click(runCSS)
    .waitForElementPresent(newPageElementCSS,5000)

but now it times out on the second wait, even though I can clearly see the new page on the browser display (Firefox 45.0.1 on Windows 8.1).

Answer

eyn picture eyn · May 11, 2018

Wait for something (a selector) that is unique to the page that will be loaded after the click. It can be anything as long as it doesn't exist on the current page.

For example,

This would wait for <div name="Thingy"> anywhere on the page:

client.waitForElementVisible('div[name="Thingy"]',3000)