Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nightwatch.js: window is undefined

I'm trying to use Nightwatch to test a React application. I'm using React-Router with it.

When running my test with Nightwatch window is undefined.

React uses the following snippet to test if the DOM is available:

var canUseDOM = !!(
  typeof window !== 'undefined' &&
  window.document &&
  window.document.createElement
);

From React.js source: ExecutionEnvironment.js#L16

React-Router expects canUseDOM to be true, otherwise it throws an error.

So my test fails because window is undefined when running Nightwatch.

Shouldn't window be present with selenium webdriver? How can I make window available?

like image 591
Sebastian Avatar asked Apr 18 '15 09:04

Sebastian


People also ask

What is the command to run the script Nightwatch?

You can add Nightwatch to your project simply by running npm install nightwatch --save-dev .

What is Nightwatch js used for?

Nightwatch. js is an integrated, easy to use End-to-End testing solution for web applications and websites, written in Node. js. It uses the W3C WebDriver API to drive browsers and perform commands and assertions on DOM elements.

Is Nightwatch js a framework?

Nightwatch. js is an open-source automated testing framework that is powered by Node. js and provides complete E2E (end to end) solutions to automation testing with Selenium Javascript be it for web apps, browser apps, and websites.

Is Nightwatch js open-source?

With its simple syntax and an in-built test runner, developers can run and manage tests without any hassle. For developers who use Nightwatch. js for their testing needs, Nightwatch will always remain free and open-source.


1 Answers

From Nighwatch.js (and selenium-webdriver, more specifically) you cannot directly access to the DOM of the client. You must use the execute() function to inject your script :

 this.demoTest = function (browser) {
   browser.execute(function(data) {

     var canUseDOM = !!(
       typeof window !== 'undefined' &&
       window.document &&
       window.document.createElement
     );
     alert('canUseDOM ?' + canUseDOM); 

     return true;
   }, [], null);
 };

More info in the API : http://nightwatchjs.org/api#execute

like image 107
Nicolas Pennec Avatar answered Sep 23 '22 08:09

Nicolas Pennec