加载太快时无法获取 URL 的标题

Can't get title of the URL when loading too fast

我对摩卡还很陌生,到目前为止我似乎很喜欢它。但是我确实遇到了一个小问题,它似乎试图太快地定位 pageTitle,如果幸运的话,它实际上设法找到了标题,但有时却找不到,我正在寻找类似 "wait until the element is presented, if not after 10 sec then throw a error" 的方法

browser.get(url);


it('should have a title', (done) => {
    browser.driver
        .then(() => browser.getPageTitle())
        .then((text) => {
            assert.equal(text, "TEST", 'Not able to find the title');
        })
        .then(() => done());
});

现在,如果我幸运的话,它设法捕获了它,但大多数情况下它会抛出一个错误,这是一个空响应,我认为它没有获得标题是因为它太快了。我怎样才能使函数类似于 "Wait until the title is there, and if its not after 10 sec then error"

试试这个:

setTimeout(() => {
    browser.get(url);


    it('should have a title', (done) => {
        browser.driver
            .then(() => browser.getPageTitle())
            .then((text) => {
                assert.equal(text, "TEST", 'Not able to find the title');
            })
            .then(() => done());
    });

}, 1000);

听起来你需要 browser.waitexpectedCondition。试试这个

browser.wait(protractor.ExpectedConditions.urlContains('required_url'),
    10*1000,
    "Url did not contain 'required_url' within 10 seconds"
);

按承诺导入 chai 和 chai

const chai = require('chai').use(require('chai-as-promised'));
const expect = chai.expect;

现在尝试使用 expect

it('Check page title', () => { expect(browser.getTitle()).toEqual('Protractor practice website - WebTables'); })

现在 expect 是 chai 断言库而不是默认的 jasmine。