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

家住魔仙堡 提交于 2019-12-13 00:29:30

问题


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:


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();
});


来源:https://stackoverflow.com/questions/30891693/protractor-done-and-expect-why-do-we-need-wait

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!