我有一個CasperJS測試套件需要驗證圖像onload事件。爲了驗證這一點,我有一個1x1像素的GIF圖片,這是我利用服務網絡服務器PhantomJS:PhantomJS webserver的CasperJS測試無法加載本地圖像
var fs = require('fs');
var webserver = require('webserver');
casper.test.begin('Image onload should be invoked', 1, {
setUp: function() {
var server = webserver.create();
this.server = server.listen(8080, function(request, response) {
if (request.url == '/') {
response.writeHead(200, { 'Content-Type': 'text/html' });
response.write('' +
'<!doctype html>\n' +
'<html>\n' +
'<head>\n' +
'<script type="text/javascript">\n' +
'window.onload = function() {\n' +
'var px = document.createElement("img");\n' +
'px.onload = function() {\n' +
'window._pxLoad = true;\n' +
'};\n' +
'px.src = "px.gif";\n' +
'};\n' +
'</script>\n' +
'</head>\n' +
'<body></body>\n' +
'</html>' +
'');
} else if (request.url.match(/px\.gif$/i)) {
response.writeHead(200, {
'Content-Type': 'image/gif',
'Cache-Control': 'no-cache'
});
var filePath = fs.workingDirectory + request.url.split('/').join(fs.separator).replace(/\d/gi, '');
response.write(fs.read(filePath));
}
response.close();
});
},
tearDown: function() {
this.server.close();
},
test: function(test) {
casper.start('http://localhost:8080', function() {
this.waitFor(function() {
return this.evaluate(function() {
return window._pxLoad !== undefined;
});
}, function then() {
var flag = this.evaluate(function() {
return window._pxLoad;
});
test.assertTruthy(flag, 'Image has been successfully loaded');
});
});
casper.run(function() {
test.done();
});
}
});
因爲window._pxLoad !== undefined
沒有在5000毫秒的超時時間內評估該測試失敗。我甚至在圖像處理程序中放置了console.log
語句,並且他們顯示我的路由有效,服務器確實收到了此調用,但看起來像git圖像根本沒有提供。 我試圖用來自互聯網的類似圖像this one替換本地電話px.gif
,並通過測試!所以這個問題肯定涉及到PhantomJS web服務器如何提供本地gif圖像。
是的,我知道casper的'resourceExists','waitForResource'等等。我有一個非常具體的測試用例可供參考。 – coquin