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?
You can add Nightwatch to your project simply by running npm install nightwatch --save-dev .
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With