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

For Loop is executing only once in Protractor Tests

我正在尝试自动化 angular 应用程序。我有存储在数组中的元素下拉列表。我想遍历所有的数组元素,如果满足条件就做一些操作。但是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');
}

}

您的代码在 if..else 前后存在一些语法错误。现在,根据您的代码,您有二维数组。对于要执行多次的 for 循环,您的数组应该有多个元素作为第一个索引。意思是 - 如果你有一个数组 array1 =[ ['Sens1','Sens2','Sens1','Sens2','Sens3']]; 并且你在你的 for 循环中使用 array1.length ,它将只执行一次而不是五次,因为数组的长度是一个。如果要遍历此处的每个元素,则必须在 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');
        }

    }
//});