我正在尝试使用Nightwatch来测试React应用程序.我正在使用React-Router.
使用Nightwatch窗口运行测试时未定义.
React使用以下代码片段来测试DOM是否可用:
var canUseDOM = !!(
typeof window !== 'undefined' &&
window.document &&
window.document.createElement
);
来自React.js来源:ExecutionEnvironment.js#L16
React-Router expects
canUseDOM
to be true, otherwise it throws an
error.
所以我的测试失败了,因为在运行Nightwatch时窗口未定义.
selenium webdriver不应该出现窗口吗?
如何使窗口可用?
解决方法:
从Nighwatch.js(和更具体地说是selenium-webdriver),您无法直接访问客户端的DOM.
您必须使用execute()函数注入脚本:
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