Protractor, Done and Expect, why do we need wait?

2019-08-12 12:33发布

I thought Done would make things run synchronously, meaning after I click a link, the click call back would happen after the click, apparently not as this doesn't work.

browser.ignoreSynchronization = true;
var firstURL = "https://www.google.com/?gws_rd=ssl";
describe("test", function () {
browser.get("http://www.google.com");
it("Should be on google url", function () {
    expect(browser.getCurrentUrl()).toBe(firstURL);
});
it("Should be able to type in text and click", function (done) {
    var ele = element.all(by.name("q")).first();
    ele.sendKeys("Protractor API");
    ele.click().then(function () {
        expect(true).toBe(true);
        done();
    });
});
it("Should be on new page", function (done) {
    browser.driver.getCurrentUrl().then(function (url) {
        debugger;
        done();
    });
});
});

The getCurrentUrl() at bottom of code returns the URL of the first page. How do I get the current URL when I can see it's changed in the browser from the test?

1条回答
戒情不戒烟
2楼-- · 2019-08-12 13:10

You assume that the click promise would get resolved after the url is changed but since you are testing a non-angular page and you turned off synchronization it gets resolved immediately. You should wait for the url to change yourself:

var urlChanged = function(expectedUrl) {
    return function() {
        return browser.getCurrentUrl().then(function(url) {
            return url.indexOf(expectedUrl) > -1;
        });
    };
};

then in the test:

ele.click().then(function () {
    browser.wait(urlChanged('google'), 5000);
    done();
});
查看更多
登录 后发表回答