简体   繁体   English

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

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

I am pretty new with mocha and I seem to love it so far.我对摩卡咖啡很陌生,到目前为止我似乎很喜欢它。 However I do get a small issue where it seems that it is trying to locate the pageTitle too fast and if I am lucky it actually manages to find the title but sometimes it doesnt and I am looking for a way something like "wait until the element is presented, if not after 10 sec then throw a error"但是我确实遇到了一个小问题,它似乎试图太快地定位 pageTitle,如果我幸运的话,它实际上设法找到了标题,但有时它没有,我正在寻找一种类似“等到元素出现,如果 10 秒后没有出现,则抛出错误”

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

Right now if I am lucky it manages to caught it but mostly it throws an error which is a empty reponse which I believe is that it doesn't get the title due to it is too fast.现在,如果我幸运的话,它设法抓住了它,但大多数情况下它会抛出一个错误,这是一个空响应,我认为这是因为它太快而没有获得标题。 How can I make a function something like "Wait until the title is there, and if its not after 10 sec then error"我怎样才能做一个类似“等到标题出现,如果它不在 10 秒后出现错误”之类的功能

Try this:尝试这个:

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

Sounds like you need a browser.wait with an expectedCondition .听起来你需要一个browser.wait和一个expectedCondition Try this尝试这个

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

Import chai and chai-as-promised进口柴和柴作为承诺

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

Try using expect now现在尝试使用 expect

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

Now expect is of chai assertion library and not default jasmine.现在期望是柴断言库而不是默认的茉莉花。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM