量角器 - 循环遍历表以从列表框中选择选项

时间:2014-09-16 17:35:09

标签: angularjs gruntjs protractor

我正在尝试使用Protractor遍历表,以查看某行是否包含select元素。然后选择两个特定选项中的一个。到目前为止,测试'通过'但实际上没有选择任何东西。我的最新尝试如下。

it('should loop through table and select an option if listbox is available', 
function () {
    var table= element(by.xpath("//table[@id='tableID']"));
    var count = table.length;
    var currentType = "";

    for (var i = 1; i <= count; i++) {
        tableSelect(i);
    }

    function tableSelect(i) {
        it('should loop through this table and associate', function () {
            expect(table.isDisplayed()).toBe(true);

            if (element(by.xpath("//table[@id='tableID']/tbody/tr[" + i + "]")).isDisplayed()) {

                if (element(by.xpath("//table[@id='tableID']/tbody/tr[" + i + "]//select[matches(@ng-change,'listChange()')]")).isDisplayed()) {

                    var varOne = element(by.xpath("//table[@id='tableID']/" + 
                         "tbody/tr[" + i + "]/td[4]/div/span/span")).getText();

                    if (currentType != "Moretext" || varOne.Length < 6) {
                        element(by.xpath("//table[@id='tableID']/tbody/tr[" + i +
                                         "]/td[6]/div/span/div/select")).
                            element(by.cssContainingText('option', 
                                    'This is option A')).click();
                    }
                    else {
                        element(by.xpath("//table[@id='tableID']/tbody/tr[" + i + "]/td[6]/div/span/div/select")).element(by.cssContainingText('option', 'This is option B')).click();
                    }
                }
                else if (element(by.xpath("//table[@id='tableID']/tbody/tr[" + i + "]/td[1]/div/span/span")).isDisplayed()) {
                    currentType = element(by.xpath("//table[@id='tableID']/tbody/tr[" + i + "]/td[1]/div/span/span")).getText();

                }
            }
        });
    }

    var saveBtn = elem(by.id('saveButton'));
    expect(associateBtn.isDisplayed()).toBe(true);
    expect(associateBtn.isEnabled()).toBe(true);
    saveBtn.click();
});

这是尝试#2:

element.all(by.repeater('row in datarows')).then(function(rows) {
    for (var i = 1; i <= rows.length; ++i) {
        if (element(by.xpath("//table[@id='tableID']/tbody/tr[" + i + "]//select[matches(@ng-change,'listChange()')]")).isDisplayed()) {

            var varOne = element(by.xpath("//table[@id='tableID']/tbody/tr[" + i + "]/td[4]/div/span/span")).getText();

            if (currentType != "MoreText" && varOne.Length < 6) {
                element(by.xpath("//table[@id='tableID']/tbody/tr[" + i + "]/td[6]/div/span/div/select")).element(by.cssContainingText('option', 'This is option A')).click();
            }
            else {
                element(by.xpath("//table[@id='tableID']/tbody/tr[" + i + "]/td[6]/div/span/div/select")).element(by.cssContainingText('option', 'This is option B')).click();
            }
        }
        else if (element(by.xpath("//table[@id='tableID']/tbody/tr[" + i + "]/td[1]/div/span/span")).isDisplayed()) {
            currentType = element(by.xpath("//table[@id='tableID']/tbody/tr[" + i + "]/td[1]/div/span/span")).getText();
        }
    }
});

这是最新的尝试:

var varOne = "";
var varTwo = "";

element.all(by.xpath("//table[@id='tableID']/tbody/tr")).then(function (rows) {
    for (var i = 1; i < (rows.length); i++) {

        console.log('rowcount = ' + i);

        element(by.xpath("//table[@id='tableID']/tbody/tr[" + i + "]")).isDisplayed().then(function (visible) {
            if (visible) {
                element(by.xpath("//table[@id='tableID']/tbody/tr[" + i + "]//select[@ng-change='listChange()']")).isDisplayed().then(function (visible) {
                    if (visible) {
                        element(by.xpath("//table[@id='tableID']/tbody/tr[" + i + "]/td[4]/div/span/span")).getText().then(function (monthText) {
                            varTwo = monthText;

                            console.log(varTwo);
                        });

                        if (varOne != "Revolving" || varTwo.length < 6) {
                            element(by.xpath("//table[@id='tableID']/tbody/tr[" + i + "]/td[6]/div/span/div/select")).element(by.cssContainingText('option', 'Exclude: Duplicate Account')).click();
                        }
                        else {
                            element(by.xpath("//table[@id='tableID']/tbody/tr[" + i + "]/td[6]/div/span/div/select")).element(by.cssContainingText('option', 'Include in Ratios')).click();
                        }
                    }
                    else {
                        element(by.xpath("//table[@id='tableID']/tbody/tr[" + i + "]/td[1]/div/span/span")).isDisplayed().then(function (visible) {
                            if (visible) {
                                element(by.xpath("//table[@id='tableID']/tbody/tr[" + i + "]/td[1]/div/span/span")).getText().then(function (currText) {
                                    varOne = currText;
                                    console.log(varOne);
                                });
                            }
                        });
                    }
                });
            }
        });
    }
});

3 个答案:

答案 0 :(得分:2)

代码

if (element(by.xpath("//table[@id='tableID']/tbody/tr[" + i + "]")).isDisplayed()) {

无法正常工作,因为总是评估为true,因为对于每个Javascript布尔值,promise对象都是真的。

你需要遵守诺言:

var elm = element(by.xpath("//table[@id='tableID']/tbody/tr[" + i + "]"));
elm.isDisplayed().then(function(visible) {
  if (visible) {
    // logic here
  }
});

假设元素存在,如果不存在,那么isDisplayed将在webdriver级别失败,因此您可以测试isPresent而不是isDisplayed

答案 1 :(得分:0)

这就是我过去常常让它发挥作用的地方。我调用了for循环中的函数,函数内部是执行繁重操作的代码。

it('should loop through table and select an option from each listbox available', function() {
    var varOne = "";
    var varTwo = "";

    element.all(by.xpath("//table[@id='tableID']/tbody/tr")).then(function (rows) {
        for (var i = 1; i < (rows.length); i++) {
            console.log('count = ' + i);
            selectWithinTable(i);
        }

        function selectWithinTable(i) {
            element(by.xpath("//table[@id='tableID']/tbody/tr[" + i + "]")).isDisplayed().then(function(visible) {
                if (visible) {
                    element(by.xpath("//table[@id='tableID']/tbody/tr[" + i + "]//select[@ng-change='listChange()']")).isDisplayed().then(function(visible) {
                        if (visible) {
                            element(by.xpath("//table[@id='tableID']/tbody/tr[" + i + "]/td[4]/div/span/span")).getText().then(function(someText) {
                                varTwo = someText;
                                console.log(varTwo);
                                console.log(varTwo.length);
                            });

                            if (varOne != "Revolving" || varTwo === "") {
                                element(by.xpath("//table[@id='tableID']/tbody/tr[" + i + "]/td[6]/div/span/div/select")).element(by.cssContainingText('option', 'This is option A')).click();
                            }
                            else {
                                element(by.xpath("//table[@id='tableID']/tbody/tr[" + i + "]/td[6]/div/span/div/select")).element(by.cssContainingText('option', 'This is option B')).click();
                            }
                        }
                        else {
                            element(by.xpath("//table[@id='tableID']/tbody/tr[" + i + "]/td[1]/div/span/span")).isDisplayed().then(function(visible) {
                                if (visible) {
                                    element(by.xpath("//table[@id='tableID']/tbody/tr[" + i + "]/td[1]/div/span/span")).getText().then(function (moreText) {
                                        varOne = moreText;
                                        console.log(varOne);
                                    });
                                }
                            });
                        }
                    });
                }
            });
        }
    });

    element(by.id('buttonID')).isDisplayed().then(function(visible) {
        if(visible) {
            element(by.id('buttonID')).isEnabled().then(function(enabled) {
                if (enabled) {
                    element(by.id('buttonID')).click();
                }
            });
        }
    });
});

答案 2 :(得分:0)

//下面的代码可帮助我遍历表中的特定行和单元格

async colRowIterate() {
    this.rowsTblValue.each(async (rowsValues: any) => {
        let cells = rowsValues.$$('td');
        cells.get(0).getText().then(async (cellvalues: any) => {
            await browser.sleep(10000);
            if (cellvalues == 'Harry') {
                cells.get(4).$('button').click();
            }

        });
    });