angular.js icon indicating copy to clipboard operation
angular.js copied to clipboard

scenario runner cannot communicate with the app if it has been bootstrapped manually

Open tnajdek opened this issue 13 years ago • 11 comments

Scenario runner uses ng:app to get hold of $injector, so that it can communicate with the app inside an iframe. So without ng:app, it can't find it.

A workaround suggested by Vojta is to manually add ng-app class to the relevant DOM node.

I suspect this problem will affect everyone who tries to use Angular together with Require JS.

tnajdek avatar Feb 15 '13 14:02 tnajdek

I posted a hack that we used to make it load our bootstrapped app here. https://groups.google.com/d/msg/testacular/aCORvWSqMcs/9wE5KHx7s-wJ

wesjones avatar Feb 26 '13 16:02 wesjones

nice workaround @cybus1 , thanks for sharing!

BTW: I've created a seed app that makes use of Vojta's workaround for those who just want to get started with Angular+RequireJS

tnajdek avatar Feb 26 '13 23:02 tnajdek

There is another issue when manually bootstraping the app and the scenario runner if the app is not immediatly bootstraped. In that case, the workaround doesn't work. I've created a plunker that shows that issue : http://plnkr.co/edit/4TxGbxDiTZm4BvCwauI3?p=preview

ffesseler avatar Apr 17 '13 11:04 ffesseler

I've run into the same issue. My stack is: Karma 0.8.5 (running with Chrome, OSX) AngularJS 1.0.6 RequireJS 2.1.5

I managed to fix it based on this answer: http://stackoverflow.com/questions/15499997/how-to-use-angular-scenario-with-requirejs

But I had to change a few things. Basically when using the ANGULAR_SCENARIO in Karma, it comes bundled with Angular 1.1.5-ab755a2, so I overrode the karma conf to load an angular-scenario.js that matches my angular version (1.0.6).

That was working fine when launching the DEBUG page of Karma, but not when launching the test "normally". To achieve the last bit, I had to change the hack a bit, especially that bit referencing the frame where my app is actually loaded:

if (typeof fn === 'function') {
  angular.element(window.top).bind('message', function(e) {
  if (e.data &&
    e.source === frame.prop('contentWindow') &&
    e.data.type === 'apploaded') {
      fn.call(frame, e);
    }
  });
  return this;
}

Apart from the fact that Karma doesn't come with an angular-scenario that can't specify a version of angular.js to be used (should that be an issue to raise on Karma's issue list?), I think the problem here is that when the app is bootstrap manually (i.e. no ng-app on the html tag), the runner should hook in some way to expect bootstrap to be called manually and only launches the tests once it's done (which is what the hack is doing).

Hope that helps, Fab

FabienDeshayes avatar Apr 29 '13 17:04 FabienDeshayes

Hi Fab,

I am also developing app by using angular.js and require.js. And followed all the steps which you mentioned.

My environment is like this ,- Angular JS - 1.1.4 Require.js - 2.1.2 Karma - 0.8.5

But all the time when I am running my e2e test then I am getting "resumeBootstrap" error .

And if I change my environment like below, - Angular JS - 1.0.6 Require JS - 2.1.2 Karma - 0.8.5

And in karma if I added angular-scenario.js which is provided by you in above link, then no error but it not executing any test cases.

Now for your code block ,-

if (typeof fn === 'function') { angular.element(window.top).bind('message', function(e) { if (e.data && e.source === frame.prop('contentWindow') && e.data.type === 'apploaded') { fn.call(frame, e); } }); return this; }

Can you tell me where to add this code in angular-scenario.js , so that my test will run.

please reply with your valuable response, it helps me lot. I spent more than enough amount of time to resolved this.

Thanks, Swapnil

swapbuk avatar May 02 '13 12:05 swapbuk

Hi Swapnil,

I basically load the "patch" after angular-scenario, defining it like this in my karma-conf:


// list of files / patterns to load in the browser
files = [
  'js/vendor/test/angular-scenario.js', // need v 1.0.6 to get the amd angular scenario working
  'js/test/util/amd-angular-scenario.js', // the "hack"
  ANGULAR_SCENARIO_ADAPTER,
  'test/e2e/**/*.js' // my tests
];

the amd-angular-scenario file looks like that (sorry it's a bit long):

(function() {
    var setUpAndRun = angular.scenario.setUpAndRun;

    angular.scenario.setUpAndRun = function(config) {
        amdSupport();
        return setUpAndRun.apply(this, arguments);
    };

    function amdSupport() {
        var getFrame_ = angular.scenario.Application.prototype.getFrame_;

        /**
         *  This function should be added to angular-scenario to support amd. It overrides the load behavior to wait from
         *  the inner amd frame to be ready.
         */
        angular.scenario.Application.prototype.getFrame_ = function() {
            var frame = getFrame_.apply(this, arguments);
            var load = frame.load;

            frame.load = function(fn) {

                if (typeof fn === 'function') {
                    angular.element(window.top).bind('message', function(e) {
                        if (e.data &&
                            e.source === frame.prop('contentWindow') &&
                            e.data.type === 'apploaded') {
                            fn.call(frame, e);
                        }
                    });
                    return this;
                }
                return load.apply(this, arguments);
            };

            return frame;
        };
    }
})();

Hope that helps, Fab

FabienDeshayes avatar May 02 '13 16:05 FabienDeshayes

Thanks Fab,

It helps me a lot.. now my e2e is working fine..

Thanks, Swapnil

swapbuk avatar May 03 '13 11:05 swapbuk

Hi fab,

My test will works only if I put setTimeout function inside each test otherwise its giving me error "Frame window is not accessible."

(function () { 'use strict';

describe('Export Flat App', function () {

  // This will work only if I put settime out otherwise it will not work.
    beforeEach(function () {
        setTimeout(function () {
            browser().navigateTo('/export-flat/desktop-index.html');
        }, 5000);

    });

// This test will not work and throws error. it('should jump to the /dashboard path when /deshtop-index.html is accessed', function () { expect(browser().location().path()).toBe("/dashboard"); });

// This test will work . it('should jump to the /link1 path when #/link1 is accessed', function () { setTimeout(function () { browser().navigateTo('#/link1'); expect(browser().location().path()).toBe("/link1"); expect(element('[ng-view] h1:first').text()). toMatch(/This is a dummy page for dummy 2 /); }, 3000); });

});

})();

Can you please tell me what is the problem?

Thanks, Swapnil

swapbuk avatar May 07 '13 12:05 swapbuk

As part of our effort to clean out old issues, this issue is being automatically closed since it has been inactivite for over two months.

Please try the newest versions of Angular (1.0.8 and 1.2.0-rc.1), and if the issue persists, comment below so we can discuss it.

Thanks!

btford avatar Aug 24 '13 00:08 btford

I encountered this issue with AngularJS 1.2.0-rc.1 and Karma 0.10.2. The solution I found was twofold.

Add the ng-app attribute after bootstrapping manually. Then, if the app is running in an iframe, emit an event to signal that the app has finished loading.

angular.element(document).ready ->
  angular.bootstrap document, ['app']

  html = document.getElementsByTagName('html')[0]

  html.setAttribute('ng-app', 'app')
  html.dataset.ngApp = 'app'

  if top isnt window
    top.postMessage { type: 'loadamd' }, '*'

A monkey patch intercepts window.karma.start and overrides angular.scenario.Application.prototype.getFrame_ to listen to the aforementioned event.

(function() {
    function amdSupport() {
        var getFrame_ = angular.scenario.Application.prototype.getFrame_;

        /**
         *  This function should be added to angular-scenario to support amd. It overrides the load behavior to wait from
         *  the inner amd frame to be ready.
         */
        angular.scenario.Application.prototype.getFrame_ = function() {
            var frame = getFrame_.apply(this, arguments);
            var load = frame.load;

            frame.load = function(fn) {
                if (typeof fn === 'function') {
                    angular.element(window).bind('message', function(e) {
                        if (e.data && e.source === frame.prop('contentWindow') && e.data.type === 'loadamd') {
                            fn.call(frame, e);
                        }
                    });
                    return this;
                }
                return load.apply(this, arguments);
            }

            return frame;
        };
    }

    (function(start) {
        window.__karma__.start = function() {
            amdSupport();
            start.apply(this, arguments);
        }
    })(window.__karma__.start);
})();

The patch is a modified version of the one found here and in a few other places. I did it this way because Karma loads the ng-scenario framework first, consisting of both scenario runner and adapter, and the patch needed to be applied between them. Now it works fine both ways. Here's the relevant snippet from my Karma config.

frameworks: ['ng-scenario']
plugins: [
  'karma-ng-scenario',
  'karma-chrome-launcher'
]
files: [
  '../test/angular-scenario-amd-support.js'
  '**/*Spec.js'
]

Hope this helps.

Ezku avatar Sep 04 '13 13:09 Ezku

Thanks @Ezku, you just put me out of over 5 hours of misery... Confirming Ezku's code fixes things as of angular 1.2.0-b56b21a.

filiptc avatar Oct 08 '13 08:10 filiptc