angular / protractor

E2E test framework for Angular apps
http://www.protractortest.org
MIT License
8.75k stars 2.31k forks source link

browser.navigate().back(); is causing "document unloaded while waiting for result" error #2600

Closed bmsoko closed 9 years ago

bmsoko commented 9 years ago

Hello, I've read all the questions/answers on the platforms, and none of them helped me to solve my issues, the thing is that I have the following test script:

    it('Verify user sees the home page correctly when hitting back button', function () {
        browser.get('/flight/los-angeles/');
        expect(testUtils.getTextFromField(topSearchBarPage.airportDestLocator)).toContain("LAX");
        element.all(by.css(".day-droppable.ui-droppable")).get(30).click();
        element(by.css(".btn-primary")).click();
        browser.navigate().back();
        expect(browser.getTitle()).toMatch('page title');
        expect(browser.getCurrentUrl()).toContain("/flight/los-angeles/");
    });

And then I'm getting this error:

UnknownError: javascript error: document unloaded while waiting for result

I can see (because I've placed an sleep) that right after executing the navigate.back() the test tries to reload the page, and when it tries to execute the expects, it throws the error.

This is how the app looks like after the navigate.

reload

Please help!

Thanks

NickTomlin commented 9 years ago

If you can get this in a more isolated/reproducible state let's resubmit it as an issue and work on a fix. I'll try to reproduce this as well. Typically this happens when moving from a synchronized angular state to a non-synchronized one.

That said, I think this is best suited for SO or gitter. Please ask a question there with the 'protractor' tag or post in the Gitter Channel to get help.

bmsoko commented 9 years ago

Thanks @NickTomlin! but I had isolated that the issues here is that method in particular, since I had removed that step (the navigate back) and the expect function works, I had placed a browser.sleep(5000) right after the navigate.back() and that's how I found that it's trying to execute the next "expecs" on an "empty page". So, how's that you want me to resubmit the issue? I'll join those channels and see if someone can help!

Thanks for the fast reply!

Bruno

sjelin commented 9 years ago

I'm pretty sure the issue is actually just that element(by.css(".btn-primary")).click(); hasn't finished before browser.navigate().back(); happens. Try adding a browser.waitForAngular().

bmsoko commented 9 years ago

@sjelin I've tried what you told and it does not work, in fact, the clicking action does happens and finishes when it should since I'm seeing the action being done while the test runs... so, I think that the problem here is the navigate().back() is opening or refreshing without URL, and the next actions are the ones throwing the errors.

Can you please give me a hint to where can I look at to solve this?

Thanks a lot.

Bruno

bmsoko commented 9 years ago

@sjelin @NickTomlin I've just checked using it against Firefox, and the @sjelin's suggestion works! so, the problem is with Chrome! are you aware of any issues around with this with browser? Is there any workaround that you my suggest?

Thanks!!

sjelin commented 9 years ago

I still think it's a that the click is in some part pending when the navigation happens - that's what the error message says after all. Try:

    it('Verify user sees the home page correctly when hitting back button', function () {
        browser.get('/flight/los-angeles/');
        expect(testUtils.getTextFromField(topSearchBarPage.airportDestLocator)).toContain("LAX");
        element.all(by.css(".day-droppable.ui-droppable")).get(30).click();
        element(by.css(".btn-primary")).click();
        browser.waitForAngular().then(function() {
            browser.navigate().back();
            expect(browser.getTitle()).toMatch('page title');
            expect(browser.getCurrentUrl()).toContain("/flight/los-angeles/");
        });
    });
leomart commented 8 years ago

Actually, we're facing pretty much the same behavior of browser.navigate().back(); in our tests with only difference in error 'Error: Error while waiting for Protractor to sync with the page: "[ng:test] http://errors.angularjs.org/1.4.7/ng/test"' Obviously protractor loose synchronization on page reloading (which happens after browser.navigate().back(); in our case).

The code causing error is:

if (button === 'Back') {
            return browser.navigate().back().then(function(){
                return browser.waitForAngular();
            })
        }
    });

or

if (button === 'Back') {
            browser.navigate().back().
            return browser.waitForAngular();
        }

Reproducible on Firefox versions: 43.0.1, node v4.4.3, Protractor 4.0.9 Passes on Chrome. If we'll add browser.driver.sleep(2000); then the code works as expected, because this tiny delay covers the moment of page reloading and it is enough for Angular to appear on the page:

            return browser.navigate().back().then(function(){
                browser.driver.sleep(2000);
                return browser.waitForAngular();
            })

Stick to that temporary solution by now.