kmiyashiro / grunt-mocha

[MOVED] Grunt task for running mocha specs in a headless browser (PhantomJS)
MIT License
370 stars 106 forks source link

iframe, embed, and objects intermittently fail, onLoadFinished gets called multiple times #144

Open saiichihashimoto opened 9 years ago

saiichihashimoto commented 9 years ago

I have a few tests that inject iframes, embeds, and/or objects. The more of these I have, the more likely my tests are to fail. However, they don't fail consistently. Running with debug, the tail end of the errors are:

[D] ["phantomjs","mocha.suite",{"title":"youtube-button-put-on-videos","fullTitle":"youtube-button-put-on-videos"}] [D] ["phantomjs","mocha.suite",{"title":"embed","fullTitle":"youtube-button-put-on-videos embed"}] [D] ["phantomjs","mocha.test",{"title":"should be on youtube embeds","fullTitle":"youtube-button-put-on-videos embed should be on youtube embeds"}] [D] ["phantomjs","onResourceRequested","https://www.youtube.com/v/VpXUIh7rlWI"] [D] ["phantomjs","mocha.pass",{"title":"should be on youtube embeds","fullTitle":"youtube-button-put-on-videos embed should be on youtube embeds","state":"passed","duration":7}] [D] ["phantomjs","mocha.test end",{"title":"should be on youtube embeds","fullTitle":"youtube-button-put-on-videos embed should be on youtube embeds","state":"passed","duration":7}]\ [D] ["phantomjs","onResourceError","https://www.youtube.com/v/VpXUIh7rlWI","Operation canceled"] [D] ["phantomjs","onResourceReceived","https://www.youtube.com/v/VpXUIh7rlWI"] [D] ["phantomjs","onLoadFinished","success"] [D] ["phantomjs","onLoadFinished","success"] [D] ["phantomjs","onLoadFinished","success"] [D] ["phantomjs","onLoadFinished","success"] [D] ["phantomjs","onLoadFinished","success"] [D] ["phantomjs","onLoadFinished","fail"] [D] ["phantomjs","fail.load","http://localhost:9500/index.html"]

onLoadFinished gets called multiple times. Is there a way to keep phantomjs from loading the iframes, embeds, and objects' sources? I need them to keep their src attributes for my tests.

kmiyashiro commented 9 years ago

I'm not really following what you're asking for. You want to have iframes with src attributes, but don't want them to load the src? Why not set all of the src URLs to a blank page on localhost?

saiichihashimoto commented 9 years ago

I've done poorly at describing this :)

I've written a very small unit test that describes my situation:

Code

index.html

<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <title>Mocha Spec Runner</title>
    <link rel="stylesheet" href="bower_components/mocha/mocha.css">
</head>
<body>
    <div id="mocha"></div>
    <script src="bower_components/jquery/dist/jquery.js"></script>
    <script src="bower_components/mocha/mocha.js"></script>
    <script>mocha.setup('bdd')</script>
    <script src="bower_components/chai/chai.js"></script>
    <script>
        var assert = chai.assert;
        var expect = chai.expect;
        var should = chai.should();
    </script>

    <script src="spec/test.js"></script>

    <script>
        if (navigator.userAgent.indexOf('PhantomJS') < 0) {
            mocha.run();
        }
    </script>
</body>
</html>

spec/test.js

'use strict';

(function() {
    describe('loading content through html src\'s', function() {
        it('should work just fine when tests end quickly', function() {
            $('body').append('<embed src="https://www.youtube.com/v/VpXUIh7rlWI">');
        });

        it('should work just fine when tests end after a while', function(done) {
            $('body').append('<embed src="https://www.youtube.com/v/VpXUIh7rlWI">');
            setTimeout(done, 1000);
        });
    });
})();

Running grunt-contrib-connect and opening them in my browser (Chrome) passes the tests. Running grunt-contrib-connect and having grunt-mocha point at them using the urls parameter doesn't just fail the test, the whole suite crashes:

output

$ grunt test --debug
Running "jshint:all" (jshint) task
[D] Task source: PATH_TO_PROJECT/node_modules/grunt-contrib-jshint/tasks/jshint.js

✔ No problems

Running "connect:test" (connect) task
[D] Task source: PATH_TO_PROJECT/node_modules/grunt-contrib-connect/tasks/connect.js
Started connect web server on http://localhost:9500

Running "mocha:all" (mocha) task
[D] Task source: PATH_TO_PROJECT/node_modules/grunt-mocha/tasks/mocha.js
Testing: http://localhost:9500/index.html
[D] ["PATH_TO_PROJECT/node_modules/grunt-mocha/phantomjs/main.js","/var/folders/ys/brqfgc211bn2d1_d5nwy24_00000gn/T/1425536366786.3347","http://localhost:9500/index.html","{\"timeout\":5000,\"inject\":\"PATH_TO_PROJECT/node_modules/grunt-mocha/phantomjs/bridge.js\",\"phantomScript\":\"PATH_TO_PROJECT/node_modules/grunt-mocha/phantomjs/main.js\",\"logErrors\":false,\"growlOnSuccess\":true,\"run\":true}"]
[D] ["phantomjs","private","version",{"major":1,"minor":9,"patch":8}]
[D] ["phantomjs","onResourceRequested","http://localhost:9500/index.html"]
[D] server GET /index.html 200 961 - 9 ms
[D] server GET /bower_components/mocha/mocha.css 200 4007 - 4 ms
[D] server GET /spec/test.js 200 493 - 2 ms
[D] server GET /bower_components/mocha/mocha.js 200 117185 - 5 ms
[D] server GET /bower_components/chai/chai.js 200 120817 - 4 ms
[D] server GET /bower_components/jquery/dist/jquery.js 200 247387 - 5 ms
[D] ["phantomjs","onUrlChanged","http://localhost:9500/index.html"]
[D] ["phantomjs","onResourceRequested","http://localhost:9500/bower_components/mocha/mocha.css"]
[D] ["phantomjs","onResourceRequested","http://localhost:9500/bower_components/jquery/dist/jquery.js"]
[D] ["phantomjs","onResourceRequested","http://localhost:9500/bower_components/mocha/mocha.js"]
[D] ["phantomjs","onResourceRequested","http://localhost:9500/bower_components/chai/chai.js"]
[D] ["phantomjs","onResourceRequested","http://localhost:9500/spec/test.js"]
[D] ["phantomjs","onResourceReceived","http://localhost:9500/index.html"]
[D] ["phantomjs","onResourceReceived","http://localhost:9500/bower_components/mocha/mocha.css"]
[D] ["phantomjs","onResourceReceived","http://localhost:9500/spec/test.js"]
[D] ["phantomjs","onResourceReceived","http://localhost:9500/bower_components/mocha/mocha.js"]
[D] ["phantomjs","onResourceReceived","http://localhost:9500/bower_components/chai/chai.js"]
[D] ["phantomjs","onResourceReceived","http://localhost:9500/bower_components/jquery/dist/jquery.js"]
[D] ["phantomjs","onInitialized"]
[D] ["phantomjs","onResourceRequested","http://localhost:35729/livereload.js?snipver=1"]
[D] ["phantomjs","onResourceError","http://localhost:35729/livereload.js?snipver=1","Connection refused"]
[D] ["phantomjs","onResourceReceived","http://localhost:35729/livereload.js?snipver=1"]
[D] ["phantomjs","inject","PATH_TO_PROJECT/node_modules/grunt-mocha/phantomjs/bridge.js"]
[D] ["phantomjs","mocha.start",{}]
[D] ["phantomjs","mocha.suite",{"title":"","fullTitle":""}]
[D] ["phantomjs","onLoadFinished","success"]
[D] ["phantomjs","mocha.suite",{"title":"loading content through html src's","fullTitle":"loading content through html src's"}]
[D] ["phantomjs","mocha.test",{"title":"should work just fine when tests end quickly","fullTitle":"loading content through html src's should work just fine when tests end quickly"}]
[D] ["phantomjs","onResourceRequested","https://www.youtube.com/v/VpXUIh7rlWI"]
[D] ["phantomjs","mocha.pass",{"title":"should work just fine when tests end quickly","fullTitle":"loading content through html src's should work just fine when tests end quickly","state":"passed","duration":3}]
[D] ["phantomjs","mocha.test end",{"title":"should work just fine when tests end quickly","fullTitle":"loading content through html src's should work just fine when tests end quickly","state":"passed","duration":3}]
[D] ["phantomjs","mocha.test",{"title":"should work just fine when tests end after a while","fullTitle":"loading content through html src's should work just fine when tests end after a while"}]
[D] ["phantomjs","onResourceRequested","https://www.youtube.com/v/VpXUIh7rlWI"]
[D] ["phantomjs","onLoadFinished","fail"]
[D] ["phantomjs","fail.load","http://localhost:9500/index.html"]
...ERROR
Warning: PhantomJS unable to load "http://localhost:9500/index.html" URI. Use --force to continue.

Aborted due to warnings.

Execution Time (2015-03-05 06:19:26 UTC)
jshint:all    194ms  ▇▇▇▇▇▇▇▇▇▇▇▇▇ 10%
connect:test   32ms  ▇▇ 2%
mocha:all      1.6s  ▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 88%
Total 1.9s

The first test, when run alone, works just fine in all cases. The second test, when run alone, fails in all cases.

These aren't the actual tests I'm wanting to run, just a stripped down version that illustrates the issue. I have some code I'm unit testing that affects only embeds/objects/iframes with specific values for their src, so setting them to localhost isn't really a usable substitute.

kmiyashiro commented 9 years ago

Ah, I see. https://github.com/kmiyashiro/grunt-mocha/blob/master/tasks/mocha.js#L98-L103

All solutions to this I've seen online are hacks. One such hack is just to remove the fail.load listener after the first success... I'd have to look into how grunt-lib-phantomjs attaches success events. You could do it too :smiley: