量角器,完成和期待,我们为什么需要等待? [英] Protractor, Done and Expect, why do we need wait?

查看:177
本文介绍了量角器,完成和期待,我们为什么需要等待?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为Done会让事情同步运行,这意味着在点击链接后,点击回拨会在点击后发生,显然不是因为这不起作用。

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

代码底部的getCurrentUrl()返回第一页的URL。如果我在浏览器中看到它在测试中发生变化,如何获取当前URL?

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?

推荐答案

您认为点击更改url后,promise会得到解决,但由于您正在测试非角度页面而关闭同步,因此会立即解决。
您应该等待网址自行更改:

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

然后在测试中:

ele.click().then(function () {
    browser.wait(urlChanged('google'), 5000);
    done();
});

这篇关于量角器,完成和期待,我们为什么需要等待?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆