Angular / Protractor测试中的函数调用顺序错误

时间:2018-01-15 16:52:21

标签: javascript jquery angularjs angular protractor

我试图获取网格中列的值,并在单击排序按钮后将列与同一列进行比较。我有一个获取列值的函数,以及我在单击列标题后调用的第二个函数。第二个函数获取新的列顺序并将其与第一个函数进行比较。出于某种原因,我的checkOrder'一旦浏览器启动,无论它在测试用例中的位置如何,都会始终调用函数,因此返回undefined。这导致第二个函数始终确认列的顺序实际上已从undefined更改为调用第二个函数时的列顺序。 (谁会猜到):) 这是一个测试套件:

it('should sort auditing log grid by time column', () => {
    page.navigateTo();
    sleep();
    checkOrder('1');
    page.getTimeColumn().click();
    checkOrderHasNotChanged('1');
    checkOrder('1');
    page.getTimeColumn().click();
    checkOrderHasNotChanged('1');
    checkOrder('1');
    page.getTimeColumn().click().then(function() {
        console.log(checkOrder('1')
    )});
    expect(checkOrderHasNotChanged('1'));
}); 

以下是功能:

    export function checkOrderHasChanged(column: string) {
        var unSorted = [];
        var ele = element.all(by.css('kendo-grid-list tr td:nth-of-type(' + column + ')'));
        ele.map(function (eachValue) {
            return eachValue.getText().then(function (unSorted) {
                return unSorted;
            });
        })
        .then(function (unSorted) {
            var sorted = unSorted.slice();
            sorted = sorted.sort();
            expect(checkOrder(column)).not.toEqual(unSorted.reverse());
        });
    }

    export function checkOrder(column: string) {
        var unSorted = [];
        var ele = element.all(by.css('kendo-grid-list tr td:nth-of-type(' + column + ')'));
        ele.map(function (eachValue) {
            return eachValue.getText().then(function (unSorted) {
                return unSorted;
            })
        })
    }

感谢您提出的任何建议/解释!

1 个答案:

答案 0 :(得分:0)

你错过了checkOrder()中的返回ele.map(函数(eachValue),以及代码中的错误位置。我对你的代码做了一些更改,请尝试一下。

it('should sort auditing log grid by time column', () => {
    page.navigateTo();
    sleep();
    let beforeVals = readColumnValue('1');
    page.getTimeColumn().click();
    let afterVals = readColumnValue('1');
    checkOrderHasNotChanged(beforeVals, afterVals);
}); 

export function checkOrderHasChanged(beforeSortVals, afterSortVals) {
    Promise.all([
        beforeSortVals,
        afterSortVals
    ])
    .then(function (data) {
        let beforeVals = data[0];
        let afterVals = data[1];
        // do assertion
        //expect(beforeVals).not.toEqual(afterVals);
    });
}

export function readColumnValue(column: string) {
    let eles = element.all(by.css('kendo-grid-list tr td:nth-of-type(' + column + ')'));

    return eles.map(function (eachValue) {
        return eachValue.getText().then(function(text) {
            return text.trim();
        });
    });
}