在量角器 Jasmine 中获取 'Expected undefined to be false'

Getting 'Expected undefined to be false' in protractor Jasmine

我正在验证是否不能显示其中一个元素。

代码如下:

规格文件:

expect(usersPage.isCreateTabDisplayed()).toBe(false);

方法的 usersPage 定义:

this.isCreateTabDisplayed = function()
    {
        waitUtil.isElementVisible(usersTab,repo.configData.configuration.longWaitTime);
        createUserTab.isPresent()
            .then( function(displayed) {
                console.log("create user tab  : "+ displayed);
                return displayed;
            });
    }

元素定义如下:

<li ng-class="{active: (subview == 'add')}" ng-show="iSU || dp.ua" class="ng-hide"><a href="#/setup/users/all/add/" class="ng-binding">Create user</a></li>

当我 运行 代码时,我收到 "Expected undefined to be false" 错误消息。

控制台日志显示为 "create user tab : false" 但为什么我收到错误消息以及如何处理它。

您需要 then 个序列和 return

this.isCreateTabDisplayed = function()
    {
        return waitUtil.isElementVisible(usersTab,repo.configData.configuration.longWaitTime).then(() => {
            return createUserTab.isPresent().then( function(displayed) { // or you can just "return createUserTab.isPresent()"
                console.log("create user tab  : "+ displayed);
                return displayed;
            });
        });
    }

另外,你的问题中有protractor标签,为什么不用呢? 您的代码应如下所示:

expect(usersPage.getCreateTab().isDisplayed()).toBe(false);

getCreateTab(): ElementFinder {
    browser.wait(ExpectedConditions.visibilityOf(usersTab), repo.configData.configuration.longWaitTime).then(() => {
        return createUserTab; // or put there locator to "element" (find) createUserTab
    });
}

如果我遗漏了什么,请在评论中指出我。