量角器 - 检索函数中全局数组的值

时间:2017-05-10 13:09:32

标签: javascript automation jasmine protractor

我正在尝试在全局定义的空数组中推送数组内容&然后在另一个函数中检索其内容。

以下是我尝试过的代码:

describe('My Test', function() {
var arrayf3=[];
 var indexf3='not found';
    it('Test starts', function() {
    browser.ignoreSynchronization = true;
    browser.get('https://www.w3schools.com/angular/');
    var elm = element(by.id('leftmenuinner')).all(By.css('[target="_top"]'));
    elm.count().then(function(count) {
        Methods.pushToArray(0, count, elm);
    })
    var texttocheck='Data Binding';
    Methods.getIndex(0, arrayf3.length, arrayf3, texttocheck);
    console.log('Text content of global array is ' + arrayf3);
    console.log('index of the array number having texttofind is ' + indexf3);
    })
    var Methods = {
    getIndex :function (i, max, array, texttocheck) {
        if (i < max) {
        console.log('text[' + i + '].indexOf = ' + array[i].indexOf(texttocheck))
        if (array[i].indexOf(texttocheck) > 0) {
            indexf3 = i;
        } else {
            Methods.getIndex(i + 1, max, array, texttocheck);
        }
        }
    },

    pushToArray :function (i, max, elm) {
        if (i < max) {
        elm.get(i).getText().then(function(tmpText) {
            console.log("The array "+tmpText);
            arrayf3.push(tmpText);     
        })
        Methods.pushToArray(i + 1, max, elm);
        }

    },

    }
});

问题是我获得了以下占位符值的空值:

全局数组的文本内容是

具有texttofind的数组编号的索引是

我想要使用在这个全局空数组中复制的数组值&amp;显示在相同的阻止功能'测试开始'

1 个答案:

答案 0 :(得分:1)

量角器的element.all固有地知道如何在每个元素上getText()并将值作为数组返回。

    it('Test starts', function() {
        browser.ignoreSynchronization = true;
        browser.get('https://www.w3schools.com/angular/');

        var getIndexOfElementByPartialText = function(inputText) {
            return element(by.id('leftmenuinner')).all(by.css('[target="_top"]')).getText().then(function(values) {
                var indexNumber;
                values.forEach(function(value, index) {
                    if (new RegExp(inputText).test(value)) {
                        if (indexNumber === undefined) {
                            indexNumber = index;
                        } else {
                            throw new Error('multiple elements match the input text');
                        }
                    }
                });
                if (indexNumber === undefined) {
                    throw new Error('no elements match the input text');
                } else {
                    return indexNumber;
                }
            });
        });

        expect(getIndexOfElementByPartialText('thing1').toBe(1);
        expect(getIndexOfElementByPartialText('thing2').toBe(2);
    });

根据可重复使用的功能编辑答案提供它。

相关问题