【问题标题】:Protractor- Retrieve value of global array in a function量角器 - 在函数中检索全局数组的值
【发布时间】:2017-10-09 03:52:41
【问题描述】:

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

下面是我试过的代码:

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 的数组编号的索引是

我希望在这个全局空数组中复制的数组值在同一个 it 块函数“测试开始”中使用和显示

【问题讨论】:

    标签: javascript automation jasmine protractor


    【解决方案1】:

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

    编辑了答案以提供可重用功能。

    【讨论】:

    • 这不是我想要的。请再次阅读问题和代码,以了解它打算做什么以及我真正想要什么。您发布的答案只是打印数组值,我希望将数组内容推送到空白数组(全局定义)中,然后使用 indexOf 方法查找部分文本的索引号。
    猜你喜欢
    • 1970-01-01
    • 2014-07-26
    • 2023-03-12
    • 2013-10-04
    • 1970-01-01
    • 2011-10-11
    • 1970-01-01
    • 1970-01-01
    • 2020-04-08
    相关资源
    最近更新 更多