protractor icon indicating copy to clipboard operation
protractor copied to clipboard

Protractor and angular manual bootstrap

Open scastelein opened this issue 13 years ago • 49 comments

I'm having a problem when running protractor 0.8 and having troubles running protractor on our app that is manually bootstrapped.

Here are some snippets revealing the relevant parts of the application and setup: index.html:

 <script type="text/javascript" data-main="js/main" src="js/libraries/require.js"></script>

js/main.js:

require({
    shim: {
        .... snip ..
            }
           }, 
  ['require', ..... ], function(require) {
    return require(['bootstrap']);

bootstrap.js:

var appElm = $('html');
appElm.addClass('ng-app'); // this
angular.bootstrap(document, ['app']);

This works in practice and Angular is started in a correct way. When I view the html source I cannot see the ng-app attribute appearing on the html element. May be Protractor also fails to do so?

I use the protractor sample configuration and changed the relevant values to suit my app:

specs: [
    '../src/test/javascript/protractorSpec/*_spec.js',
  ],
  baseUrl: 'http://localhost:8080/Viewer/',
  rootElement: 'html',

Sample test:

describe('List SMC Charts', function() {
        var ptor= protractor.getInstance();

        beforeEach(function() {
          ptor.get('#/smcChart');
        });

        it('list, no filter', function() {
          var selectOption = ptor.findElement(ptor.By.input("selectOption"));
        });
});

When I run this test two errors appear:

 1) List SMC Charts   list, no filter
   Message:
     Error: Angular could not be found on the page http://localhost:8080/Viewer/#/smcChart
   Stacktrace:
     Error: Angular could not be found on the page http://localhost:8080/Viewer/#/smcChart
    at /usr/local/lib/node_modules/protractor/lib/protractor.js:392:15

and

2) List SMC Charts list, no filter
   Message:
     TypeError: Cannot call method 'input' of undefined
   Stacktrace:
     TypeError: Cannot call method 'input' of undefined
    at null.<anonymous> (/Users/........//Viewer/src/test/javascript/protractorSpec/some_spec.js:9:55)

The first error seems to be the most important one, but the last error is also quite interesting. It appears to me that the line: ptor.By.input is not working.

Any ideas?

scastelein avatar Aug 30 '13 12:08 scastelein

Is the Angular code being included on the page? It looks like you're requiring shim, bootstrap and others but angular.min.js isn't shown.

stickel avatar Sep 03 '13 18:09 stickel

'app': {
            deps: ['libraries/angular', 'libraries/angular-resource', 'libraries/angular-strap']
        },

The snippet above resides inside the shim{} element, I left it out of the snippets because it did not seem very relevant to me. But this is the way how angular.js is being loaded.

scastelein avatar Sep 04 '13 07:09 scastelein

Hi,

Yes, Protractor currently expects an ng-app, so manually bootstrapped sites will have problems. Hoping to get a workaround for this documented soon.

juliemr avatar Sep 04 '13 20:09 juliemr

Ok thx for the update. When you've figured out a workaround can you post it in this issue?

scastelein avatar Sep 05 '13 10:09 scastelein

Sorry for the slow response - you should be able to fix by doing

describe('List SMC Charts', function() {
        var ptor= protractor.getInstance();

        beforeEach(function() {
          ptor.driver.get('#/smcChart'); // Just use the plain old webdriver here, it won't complain if Angular isn't loaded yet.
        });

        it('list, no filter', function() {
          var selectOption = ptor.findElement(ptor.By.input("selectOption"));
        });
});

juliemr avatar Sep 10 '13 19:09 juliemr

Did you set up the ptor variable somewhere? e.g. var ptor = protractor.getInstance()?

juliemr avatar Sep 24 '13 20:09 juliemr

Any update on this? Your suggested fix isn't working for me ([email protected], [email protected])

I have an awkward combination of a non-angular login screen followed by a requirejs, manually bootstrapped app:

describe('User levels', function() {
    var ptor, driver;

    var findByName = function (name) {
        return driver.findElement(protractor.By.name(name));
    };

    describe('Editors', function() {
        ptor = protractor.getInstance();
        driver = ptor.driver;
        it('Can log in', function() {
            driver.get('http://localhost:8000/');
            findByName('login').sendKeys('test_editor');
            findByName('pass').sendKeys('rubbish');
            driver.findElement(protractor.By.css('button[type="submit"]')).click();
        }, 20000);

        it('Shows the homepage', function() {
            var el = ptor.findElement(protractor.By.css('h1[field="pageTitle"]'));
            expect(el.getText()).toEqual('Welcome to Home');
        }, 30000);

    });
});

And my main.js:

require([
    'angular',
    'app'
], function (angular, app) {
    'use strict';
    var body = document.body, name = app.name;
    body.setAttribute('ng-app', name);
    body.setAttribute('id', 'ng-app');
    angular.bootstrap(body, [name]);
});

The login part works and I can see a flash of the protected page loading before protractor errors out with UnknownError: javascript error: angular is not defined. I've tried re-loading the page with driver.get('...') & even ptor.get('...') but nothing works.

Any hints/advice/fixes gratefully received.

magnetised avatar Sep 25 '13 14:09 magnetised

@magnetised you'll need some way of telling your test to wait until the login is done before trying other tests. I think what's happening is that you're trying to use ptor.findElement before Angular has loaded on the page.

Your 'Can log in' spec should end with a wait statement that checks some condition for your app. Check out the example here - https://github.com/angular/protractor/blob/master/spec/login/viaConfigConf.js

juliemr avatar Sep 25 '13 15:09 juliemr

@juliemr Brilliant, that works perfectly.

I had understood that protractor would do the "wait until angular has loaded" but I guess you can't cover all the cases.

Thanks again.

magnetised avatar Sep 25 '13 16:09 magnetised

I have exactly the same setup. @juliemr do you mind sharing your solution. What is your waiting condition? Thanks!

jorgenfb avatar Oct 07 '13 18:10 jorgenfb

@jorgenfb I wonder if you meant that comment to be directed at me?

My solution just involved waiting for the angular router to boot and change the url from the bare '/' that the login system redirects you to, to the default route -- in our case '/#view'.

I made it into a function that I could reuse it across all the tests:

var waitUntilLoaded = function() {
    ptor.wait(function() {
      return ptor.driver.getCurrentUrl().then(function(url) {
        return /view/.test(url);
      });
    });
};

I don't know if this will work for you, it depends on your routing config.

magnetised avatar Oct 09 '13 09:10 magnetised

@juliemr Thank you, works like a charm :)

jorgenfb avatar Oct 09 '13 10:10 jorgenfb

I'm having a similar problem (found on SO at: http://stackoverflow.com/questions/19391813/protractor-fails-to-find-angular) with Protractor not finding Angular even though Angular is very plainly loaded and running. Is the easy solution (testing without trying to find Angular) the way that we should be going? It seems to defeat the purpose of Protractor being specifically built to test Angular apps. edit: I should also note that the proposed solution in this issue does not work for me.

mbielski avatar Oct 17 '13 15:10 mbielski

+1 for having better angular manual bootstrap documentation

teddyhwang avatar Oct 31 '13 20:10 teddyhwang

is this still relevant?

hankduan avatar Jun 23 '14 22:06 hankduan

I honestly don't know as I've since given up trying and don't really have the time to go back and see if something has changed. Considering the version changes since then, if you're looking to close this I wouldn't argue. It can always be re-opened later.

mbielski avatar Jun 24 '14 01:06 mbielski

I just started using protractor today and our app uses require.js... if there have been any updates with respect to testing an application that is manually bootstrapped, I , for one, am interested. :-)

andrew-luhring avatar Jun 30 '14 22:06 andrew-luhring

Same problem here. Got application with the login page outside the angular app. Workaround in previous comments seems to be outdated. Would very much like updated document on how to be able test that kind of scenario.

pgayvallet avatar Jul 01 '14 14:07 pgayvallet

@hankduan Yes, I think so.

I'm struggling with this issue right now. Background is similar: I have a bigger application which dependencies are managed via RequireJS.


I've already updated my app using window.name='NG_DEFER_BOOTSTRAP!'; & angular.resumeBootstrap(), however it still won't work.

Said this, I do not know what to do next. For further investigations I've set up a test case at https://github.com/knalli/protractor-with-deferred-bootstrap

  • git clone https://github.com/knalli/protractor-with-deferred-bootstrap.git
  • cd protractor-with-deferred-bootstrap
  • npm install
  • npm run-script webdriver (Selenium Server)
  • npm run-script server (Demo App Server serving a mini AJS app)
  • npm run-script e2e-test (runs protractor...)

The result is

Using the selenium server at http://localhost:4444/wd/hub
Login: Click
Login: Waiting...
A Jasmine spec timed out. Resetting the WebDriver Control Flow.
The last active task was: 
unknown
F

Failures:

followed by

UnknownError: unknown error: [ng:btstrpd] App Already Bootstrapped with this Element '

The last error is a little bit weird and perhaps even misleading: The test has been performed (successful as far as expectations were be okay and what we can see in the browser).

Crosscheck: If I switch the demo app to non deferred bootstrapping with manual includes (this variant is available in the branch no-deferred), then it will work successful.


I've already tried some so called "workrounds":

  1. http://www.sebdangerfield.me.uk/2014/01/angularjs-protractor-app-already-bootstrapped-error/ Irrelevant or misleading --> no difference.
  2. https://github.com/angular/protractor/issues/325#issuecomment-36738048 using protractor.getInstance() and ignoreSynchronization --> no difference
  3. https://github.com/angular/protractor/issues/325#issuecomment-30701683 patching clientSideScripts.js/waitForAngular --> no difference

knalli avatar Jul 06 '14 12:07 knalli

I wrote my first protractor tests yesterday and had the same issue as knalli.

miffels avatar Jul 07 '14 07:07 miffels