Nightwatch.js:窗口未定义 [英] Nightwatch.js: window is undefined
问题描述
我正在尝试使用Nightwatch测试React应用程序.我正在使用React-Router.
I'm trying to use Nightwatch to test a React application. I'm using React-Router with it.
使用Nightwatch window
运行测试时未定义.
When running my test with Nightwatch window
is undefined.
React使用以下代码片段测试DOM是否可用:
React uses the following snippet to test if the DOM is available:
var canUseDOM = !!(
typeof window !== 'undefined' &&
window.document &&
window.document.createElement
);
从React.js来源: ExecutionEnvironment. js#L16
From React.js source: ExecutionEnvironment.js#L16
React-Router期望
canUseDOM
为true,否则抛出 错误.
React-Router expects
canUseDOM
to be true, otherwise it throws an error.
所以我的测试失败了,因为在运行Nightwatch时未定义window
.
So my test fails because window
is undefined when running Nightwatch.
硒cdriver是否不应该出现window
?
如何使window
可用?
Shouldn't window
be present with selenium webdriver?
How can I make window
available?
推荐答案
在Nighwatch.js(更具体地说是selenium-webdriver)中,您无法直接访问客户端的DOM. 您必须使用 execute()函数注入脚本:
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);
};
API中的更多信息: http://nightwatchjs.org/api#execute
More info in the API : http://nightwatchjs.org/api#execute
这篇关于Nightwatch.js:窗口未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!