For Loop在量角器测试中仅执行一次

时间:2018-07-10 05:03:55

标签: javascript angularjs promise protractor ui-automation

我正在尝试自动执行角度应用程序。我有存储在数组中的元素的下拉列表。我想遍历所有数组元素并在满足条件的情况下执行一些操作。但是for循环仅运行一次。

expect(element(by.css('button.button.primary')).isPresent()).toBe(true).then(function() {

    for (var i = 0; i < array1.length; ++i) {
        browser.sleep(2000);
        console.log(array1);
        console.log('Configuring ' + array1[0][i]);
        browser.sleep(3000);

        //  First sens config
        if (array1[0][i] === 'Sens1') {
            element(By.linkText(array1[0][i])).click().then(function() {
                console.log('Configuring ' + array1[0][i]);
            });

            element(by.xpath('//button[@type="submit"]')).click();
            browser.sleep(1000);
            browser.driver.findElement(by.css('button.button.primary')).click().then(function() {
                browser.sleep(1000);
            });
        });

}

// Second Sens Config
else if (array1[0][i] === ''
    Sens2 ')') {
    element(By.linkText(array1[0][i])).click().then(function() {
        console.log('Configuring ' + array1[0][i]);
    });
    element(by.xpath('//se-sensor-chooser-table//tr/td[4]')).getText().then(function(state) {

        element(by.xpath('//button[@type="submit"]')).click();
        browser.sleep(1000);
        browser.driver.findElement(by.css('button.button.primary')).click().then(function() {
            browser.sleep(1000);
        });
    });
} else {
    console.log('No Sensors');
}

}

1 个答案:

答案 0 :(得分:0)

您的代码中if..else附近存在一些语法错误。现在,从您的代码中,您有了二维数组。为了使for循环执行多次,您的数组的第一个索引应包含多个元素。含义-如果您有一个array1 =[ ['Sens1','Sens2','Sens1','Sens2','Sens3']];数组,并且在for循环内使用array1.length,则该数组仅执行一次,而不是五次,因为数组的长度为1。如果要遍历这里的每个元素,则必须在for循环中使用array1[0].length。我添加了一个数组供您参考,并在下面格式化并更正了您的代码。您可以运行它以查看它是否正常运行,然后取消注释就可以正常运行,除非最终没有任何逻辑上的缺失。确保您具有正确的阵列。希望对您有所帮助。

尝试-

//expect(element(by.css('button.button.primary')).isPresent()).toBe(true).then(function () {
    array1 =[ ['Sens1','Sens2','Sens1','Sens2','Sens3']];
    for (var i = 0; i < array1[0].length; i++) {
        console.log("inside forloop");
        /* browser.sleep(2000);
        console.log(array1);
        console.log('Configuring ' + array1[0][i]);
        browser.sleep(3000); */

        //  First sens config
        if (array1[0][i] === 'Sens1') {
            console.log("inside if");
            /* element(By.linkText(array1[0][i])).click().then(function () {
                console.log('Configuring ' + array1[0][i]);
            });

            element(by.xpath('//button[@type="submit"]')).click();
            browser.sleep(1000);
            browser.driver.findElement(by.css('button.button.primary')).click().then(function () {
                browser.sleep(1000);
            }); */
        } else if (array1[0][i] === 'Sens2') {
            console.log("inside else");
            /* element(By.linkText(array1[0][i])).click().then(function () {
                console.log('Configuring ' + array1[0][i]);
            });
            element(by.xpath('//se-sensor-chooser-table//tr/td[4]')).getText().then(function (state) {

                element(by.xpath('//button[@type="submit"]')).click();
                browser.sleep(1000);
                browser.driver.findElement(by.css('button.button.primary')).click().then(function () {
                    browser.sleep(1000);
                });
            }); */
        } else {
            console.log('No Sensors');
        }

    }
//});