量角器browser.wait破坏了测试

时间:2018-09-17 09:41:09

标签: protractor

我的一个量角器测试有一个奇怪的错误。

describe('The sign in page', () => {
browser.get('/');

it('Shows and removes error message when wrong credentials', () => {
    element(by.model('user.username')).sendKeys('dhjkdashjklasdhakjdhlsa@jkasdbaskjdhajskd.com');
    element(by.model('user.password')).sendKeys('asdjasdikajksd');

    let notification = element
                        .all(by.className('notification-template'));

    element(by.name('signInForm'))
        .submit()
        .then(() => {
            expect(
                notification.count()
            )
            .toBe(1);
        });
});
});

此测试正常。但是当我尝试测试通知的消失(在下面)时,我得到了错误。

function notPresenceOfAll(elementArrayFinder) {
return () => {
    return elementArrayFinder.count((count) => {
        return count === 0;
    });
};
}

describe('The sign in page', () => {
browser.get('/');

it('Shows and removes error message when wrong credentials', () => {
    element(by.model('user.username')).sendKeys('dhjkdashjklasdhakjdhlsa@jkasdbaskjdhajskd.com');
    element(by.model('user.password')).sendKeys('asdjasdikajksd');

    let notification = element
                        .all(by.className('notification-template'));

    element(by.name('signInForm'))
        .submit()
        .then(() => {
            expect(
                notification.count()
            )
            .toBe(1);

            browser.wait(
                notPresenceOfAll(notification),
                8000
            ).then(() => {
                expect(
                    notification.count()
                ).toBe(0);
            });
        });
});
});

我收到消息“预期0为1”和“等待8009ms后超时”。有什么想法吗?

1 个答案:

答案 0 :(得分:0)

错误来自函数notPresenceOfAll,您错过了在then()之后调用count()

function notPresenceOfAll(elementArrayFinder) {
    return () => {
        return elementArrayFinder.count().then((count) => {
            return count === 0;
        });
    };
}


describe('The sign in page', () => {

  browser.get('/');

  it('Shows and removes error message when wrong credentials', () => {
    element(by.model('user.username')).sendKeys('dhjkdaa@jkasdbaskjdhajskd.com');
    element(by.model('user.password')).sendKeys('asdjasdikajksd');
    element(by.name('signInForm')).submit();

    let notification = element.all(by.className('notification-template'));

    browser.sleep(10*1000) 

    // wait for 10 seconds, to see following expect will pass or not.
    // if pass, means you need to add some wait before do expect,
    // if not, means you given locator in above element.all() 
    // find wrong elements, rather than the notification element.
    expect(notification.count()).toBe(1); 

    // browser.wait(notPresenceOfAll(notification), 8000);

    browser.sleep(10*1000)
    // change browser.wait to browser.sleep, if after wait 10 seconds
    // notification.count() is still not 0, 
    // means the DOM node of the notification is hidden from page, 
    // rather than be removed from page, so you can't check its amount on page
    // but to check its visibility

    expect(notification.count()).toBe(0);

  });
});