Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 995
Bryn Wolfe Avatar asked Apr 04 '16 15:04

Bryn Wolfe


People also ask

How do I find my current URL on Nightwatch?

There is a way in Nightwatch but you didn't notice that, simply use browser. url() without argument but a callback can get the current URL.

What is the command to run the script Nightwatch?

You can add Nightwatch to your project simply by running npm install nightwatch --save-dev . This places the Nightwatch executable in your ./node_modules/.

How do you hover over elements in Nightwatch?

You can use the selenium API moveTo command. It will move the mouse to the given element and it should stay over that element until the next command involving the mouse is used. Just pause for the amount of time you would like to hover for.

Does Nightwatch use Selenium?

Nightwatch. js is an easy to use Node. js based End-to-End (E2E) testing solution for browser based apps and websites. It uses the powerful Selenium WebDriver API to perform commands and assertions on DOM elements.

How to wait for the correct URL in a test?

For such tests you can use the URL related wait methods from ‘thewaiter’ library, to wait until the URL is the correct one, before performing the rest of the test.

How to take Screenshot in Nightwatch using JavaScript?

In Nightwatch, it is really simple and can be done using one .click () function call where as the only argument the selector has to be passed. To read more about the function, check out Nightwatch documentation about it. As the final step, let’s take a screenshot of our result using .saveScreenshot () function.

What is Nightwatch JS?

Nightwatch.js is an open-source (check their GitHub repo and their home page) end-to-end framework that was initially released in 2014 by Andrei Rusu. Since then it has grown dramatically and is still moving forward.

Can I copy and paste the loadero Nightwatch script?

Since Loadero extends vanilla Nightwatch.js functionality with various custom commands and has additional steps inside module.exports to provide you with insightful data that can be analyzed after test execution, you can’t just copy-paste the script from our local text editor.


1 Answers

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)
like image 175
eyn Avatar answered Jan 03 '23 14:01

eyn