如何在函数返回值之前阻止执行?

时间:2015-09-01 01:26:21

标签: protractor angularjs-e2e

在量角器中,我正在尝试从按钮集合中获取按钮的文本,并根据返回的按钮对返回的项目执行一些操作

/**
* Function to return the element button with matching text
* @param {buttonsObject} element which matches multiple button objects.
* @param {buttonTextToSearch} button text which can be found in a collection of button objects.
* @return {buttonObject} this will retrun the button object which matches the text being searched for.
*/
var buttons = function(buttonsObject, buttonTextToSearch) {
        var obj = buttonsObject.then(function(buttonElements) {
            //Iterate each button object and populate the array with the resulting object
            var foundbuttons = buttonElements.map(function(item) {
                return item.getText().then(function(text) {
                    if(text.length > 0 && text.charCodeAt(0) !== 32 && text == buttonTextToSearch) {
                        console.log('new text: '+ text);
                        return item;
                    }
                });
            });
            //Wait until var foundbuttons is populated
            return protractor.promise.all(foundbuttons).then(function(allButtons){
                return allButtons.reduce(function(all, item, index) {
                    if(item !== undefined && item !== '' && item !== ' ') {
                        console.log('item !== undefined '+ item);
                        item.getText().then(function(text) {
                            console.log('REDUCE '+ text);
                        });
                        all.push(item);
                    }
                    return all;
                }, []);
            });
        });

        //Wait until var obj is populated and return the element at 0th index.
        return protractor.promise.all(obj).then(function(obj1) {
            return obj1[0];
        });
    }

    var BTN_OBJS = element.all(by.css('[ng-hide=multiEditMode] [class*=button]'));
    //Get element matching button text 'My Order'
    var btn = buttons(BTN_OBJS, 'My Orders'); 

    btn.then(function(args) {
        console.log('args '+ args);
    });

现在当我执行上面的代码片段时,下面一行

buttons(BTN_OBJS, 'My Orders');
即使在下面的函数返回任何值

之前,

仍然执行得很快

    btn.then(function(args) {
        console.log('args '+ args);
    });
PS:我是量角器的新手,因此忽略了我的不良知识以及任何错别字。

0 个答案:

没有答案