我们想使用PhantomJS测试GWT单页Web应用程序。
问题是第3步导致整个主体被执行多次,这意味着我们的page.open()函数体内的console.log语句多次打印。
任何人都知道问题是什么?我们如何让身体只执行一次?
以下是产生此行为的代码:
var page = require('webpage').create();
page.onConsoleMessage = function (msg) {
console.log(msg);
};
page.open("http://gwt-web-app.com#bookmark", function (status) {
page.evaluate(function () {
console.log('start login');
document.querySelector('input#username').value = 'user';
document.querySelector('input#password').value = 'pass';
document.querySelector('button').click(); // causes body to be executed multiple times
console.log('end login');
});
window.setTimeout(function() {
page.evaluate(function() {
console.log('start element query');
// query elements, and print using console.log
console.log('end element query')
});
}, 4000);
window.setTimeout(function () {
console.log('exit');
phantom.exit();
}, 10000);
});
输出如下(运行phantomjs test_app.js
后):
start login
end login
start login
end login
start login
end login
start login
end login
start login
end login
start login
end login
start login
end login
start login
end login
start login
end login
start login
end login
start login
end login
start element query
end element query
start login
end login
start login
end login
start login
end login
start login
end login
start element query
end element query
start login
end login
start element query
end element query
start login
end login
start element query
end element query
start login
end login
start element query
end element query
start login
end login
start element query
end element query
start login
end login
start element query
end element query
start login
end login
start element query
end element query
start login
end login
start element query
end element query
start element query
end element query
start login
end login
start element query
end element query
start login
end login
start element query
end element query
start login
end login
start element query
end element query
start login
end login
start element query
end element query
start login
end login
start element query
end element query
start login
end login
start element query
end element query
start login
end login
start element query
end element query
start login
end login
exit