Advertisement
Guest User

Untitled

a guest
Feb 6th, 2016
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.24 KB | None | 0 0
  1. var fs = require('fs');
  2. var webserver = require('webserver');
  3.  
  4. casper.test.begin('Image onload should be invoked', 1, {
  5. setUp: function() {
  6. var server = webserver.create();
  7. this.server = server.listen(8080, function(request, response) {
  8. if (request.url == '/') {
  9. response.writeHead(200, { 'Content-Type': 'text/html' });
  10.  
  11. response.write('' +
  12. '<!doctype html>n' +
  13. '<html>n' +
  14. '<head>n' +
  15. '<script type="text/javascript">n' +
  16. 'window.onload = function() {n' +
  17. 'var px = document.createElement("img");n' +
  18. 'px.onload = function() {n' +
  19. 'window._pxLoad = true;n' +
  20. '};n' +
  21. 'px.src = "px.gif";n' +
  22. '};n' +
  23. '</script>n' +
  24. '</head>n' +
  25. '<body></body>n' +
  26. '</html>' +
  27. '');
  28. } else if (request.url.match(/px.gif$/i)) {
  29. response.writeHead(200, {
  30. 'Content-Type': 'image/gif',
  31. 'Cache-Control': 'no-cache'
  32. });
  33.  
  34. var filePath = fs.workingDirectory + request.url.split('/').join(fs.separator).replace(/d/gi, '');
  35.  
  36. response.write(fs.read(filePath));
  37. }
  38.  
  39. response.close();
  40. });
  41. },
  42.  
  43. tearDown: function() {
  44. this.server.close();
  45. },
  46.  
  47. test: function(test) {
  48. casper.start('http://localhost:8190', function() {
  49. this.waitFor(function() {
  50. return this.evaluate(function () {
  51. return window._pxLoad !== undefined;
  52. });
  53. }, function then() {
  54. var flag = this.evaluate(function () {
  55. return window._pxLoad;
  56. });
  57.  
  58. test.assertTruthy(flag, 'Image has been successfully loaded');
  59. });
  60. });
  61.  
  62. casper.run(function () {
  63. test.done();
  64. });
  65. }
  66. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement