Running into Error while waiting for Protractor to sync with the page with basic protractor test

前端 未结 7 1963
南旧
南旧 2020-12-03 10:31
describe(\'my homepage\', function() {
    var ptor = protractor.getInstance();
    beforeEach(function(){
        // ptor.ignoreSynchronization = true;
        ptor         


        
相关标签:
7条回答
  • 2020-12-03 10:53

    The rootElement param of the exports.config object defined in your protractor configuration file must match the element containing your ng-app directive. This doesn't have to be uniquely identifying the element -- 'div' suffices if the directive is in a div, as in my case.

    From referenceConf.js:

    // Selector for the element housing the angular app - this defaults to
    // body, but is necessary if ng-app is on a descendant of <body>  
    rootElement: 'div',
    

    I got started with Protractor by watching the otherwise excellent egghead.io lecture, where he uses a condensed exports.config. Since rootElement defaults to body, there is no hint as to what is wrong with your configuration if you don't start with a copy of the provided reference configuration, and even then the

    Error while waiting for Protractor to sync with the page: {}

    message doesn't give much of a clue.

    0 讨论(0)
  • 2020-12-03 10:54

    I'm using ChromeDriver and the above error usually occurs for the first test. I've managed to get around it like this:

    ptor.ignoreSynchronization = true;
    ptor.get(targetUrl);
    ptor.wait(
        function() {
                return ptor.driver.getCurrentUrl().then(
                    function(url) {
                        return targetUrl == url;
                    });
        }, 2000, 'It\'s taking too long to load ' + targetUrl + '!'
    );
    

    Essentially you are waiting for the current URL of the browser to become what you've asked for and allow 2s for this to happen. You probably want to switch the ignoreSynchronization = false afterwards, possibly wrapping it in a ptor.wait(...). Just wondering, would uncommenting the ptor.sleep(5000); not help?

    EDIT: After some experience with Promise/Deferred I've realised the correct way of doing this would be:

    loginBtn.click().then(function () {
        ptor.getCurrentUrl(targetUrl).then(function (newURL){
            expect(newURL).toBe(whatItShouldBe);
        });
    });
    

    Please note that if you are changing the URL (that is, moving away from the current AngularJS activated page to another, implying the AngularJS library needs to reload and init) than, at least in my experience, there's no way of avoiding the ptor.sleep(...) call. The above will only work if you are staying on the same Angular page, but changing the part of URL after the hashtag.

    0 讨论(0)
  • 2020-12-03 11:02

    I had to switch from doing this:

        describe('navigation', function(){
    
            browser.get('');
    
            var navbar = element(by.css('#nav'));
    
            it('should have a link to home in the navbar', function(){
                //validate
            });
    
            it('should have a link to search in the navbar', function(){
                //validate
            });
        });
    

    to doing this:

        describe('navigation', function(){
    
            beforeEach(function(){
                browser.get('');
            });
            var navbar = element(by.css('#nav'));
    
            it('should have a link to home in the navbar', function(){
                //validate
            });
    
            it('should have a link to search in the navbar', function(){
                //validate
            });
        });
    

    the key diff being:

        beforeEach(function(){
            browser.get('');
        });
    

    hope this may help someone.

    0 讨论(0)
  • 2020-12-03 11:06

    In my case, I encountered the error with the following code:

    describe("application", function() {
      it("should set the title", function() {
        browser.getTitle().then(function(title) {
          expect(title).toEqual("Welcome");
        });
      });
    });
    

    Fixed it by doing this:

    describe("application", function() {
      it("should set the title", function() {
        browser.get("#/home").then(function() {
          return browser.getTitle();
        }).then(function(title) {
          expect(title).toEqual("Welcome");
        });
      });
    });
    

    In other words, I was forgetting to navigate to the page I wanted to test, so Protractor was having trouble finding Angular. D'oh!

    0 讨论(0)
  • 2020-12-03 11:07

    I was getting this error:

    Failed: Error while waiting for Protractor to sync with the page: "window.angular is undefined. This could be either because this is a non-angular page or because your test involves client-side navigation, which can interfere with Protractor's bootstrapping. See http://git.io/v4gXM for details"

    The solution was to call page.navigateTo() before page.getTitle().

    Before:

    import { AppPage } from './app.po';
    
    describe('App', () => {
      let page: AppPage;
    
      beforeEach(() => {
        page = new AppPage();
      });
    
      it('should have the correct title', () => {
        expect(page.getTitle()).toEqual('...');
      })
    });
    

    After:

    import { AppPage } from './app.po';
    
    describe('App', () => {
      let page: AppPage;
    
      beforeEach(() => {
        page = new AppPage();
        page.navigateTo();
      });
    
      it('should have the correct title', () => {
        expect(page.getTitle()).toEqual('...');
      })
    });
    
    0 讨论(0)
  • 2020-12-03 11:10

    I got the same error message (Angular 1.2.13). My tests were kicked off too early and Protractor didn't seem to wait for Angular to load.

    It appeared that I had misconfigured the protractor config file. When the ng-app directive is not defined on the BODY-element, but on a descendant, you have to adjust the rootElement property in your protractor config file to the selector that defines your angular root element, for example:

    // protractor-conf.js
    rootElement: '.my-app',
    

    when your HTML is:

    <div ng-app="myApp" class="my-app">
    
    0 讨论(0)
提交回复
热议问题