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

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


        
7条回答
  •  借酒劲吻你
    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.

提交回复
热议问题