首页 > 解决方案 > 在量角器茉莉花中得到“预期未定义为假”

问题描述

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

代码如下:

规格文件:

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>

当我运行代码时,我收到“预期未定义为假”错误消息。

控制台日志显示为“创建用户选项卡:false”,但为什么我会收到错误消息以及如何处理它。

标签: jasmineprotractor

解决方案


你需要有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;
            });
        });
    }

另外,您的问题中有量角器标签,那么为什么不使用它呢?您的代码应如下所示:

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

如果我错过了什么,请在评论中指出我。


推荐阅读