量角器辅助功能,如何等待?

时间:2017-06-22 10:22:33

标签: javascript asynchronous async-await protractor

我有这段代码:

    function searchBox(filterName, objectList, filterField) {
        randIndex = Math.floor(Math.random() * objectList.length);
        element(by.id(filterName)).clear();
        Delay(delay);
        element(by.id(filterName)).click();
        element(by.id(filterName)).sendKeys(objectList[randIndex][filterField]);
        console.log("FilterName: ", filterName);
        console.log("FilterField: ", filterField);
        Delay(delay);
        element.all(by.repeater(filterName)).each(function (item) {
            item.getText().then(function (text) {
                expect(text).toContain(objectList[randIndex][filterField]);
            })
        });
    };

    it('Search for company name in searchbox on My tab', function () {
        var randIndex;
        $('[ng-click="companyCtrl.tabClick(0)"]').click();
        Delay(delay);
        $('[ng-controller="CompanyListController as companyCtrl"]').evaluate("companyCtrl.partnersList").then(function (partnersList) {
            searchBox('companyFilterMy', partnersList, 'name');
            searchBox('companyFilterMy', partnersList, 'city');
            searchBox('companyFilterMy', partnersList, 'statusText');
        });
    });

我想做await之类的事情 - 每次调用searchBox函数,因为到目前为止,下一个函数正好在之前调用。

1 个答案:

答案 0 :(得分:0)

我用npm install --save asyncawait安装了asyncawait,然后在我的spec文件的顶部我需要这两个函数:

var async = require('asyncawait/async');
var await = require('asyncawait/await');

之后我调整了上面的代码,看起来像这样,它的工作原理如下:

    var searchBox = async(function (filterName, objectList, filterField) {
        randIndex = Math.floor(Math.random() * objectList.length);
        await (element(by.id(filterName)).clear());
        Delay(delay);
        await (element(by.id(filterName)).click());
        await (element(by.id(filterName)).sendKeys(objectList[randIndex][filterField]));
        console.log("FilterName: ", filterName);
        console.log("FilterField: ", filterField);
        Delay(delay * 5);
        await (element.all(by.repeater(filterName)).each(function (item) {
            item.getText().then(function (text) {
                expect(text).toContain(objectList[randIndex][filterField]);
            })
        }));
        console.log("ASUIHGIUADSHGUH");
        return true;
    });

    it('Search for company name in searchbox on My tab', function () {
        var randIndex;
        $('[ng-click="companyCtrl.tabClick(0)"]').click();
        Delay(delay);
        $('[ng-controller="CompanyListController as companyCtrl"]').evaluate("companyCtrl.partnersList").then(async(function (partnersList) {
            await (searchBox('companyFilterMy', partnersList, 'name'));
            await (searchBox('companyFilterMy', partnersList, 'city'));
            await (searchBox('companyFilterMy', partnersList, 'statusText'));
        }));
    });