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

问题描述 投票:2回答:3

我正在尝试使用量角器在一个表中循环查看行是否包含选择元素。然后选择两个特定选项之一。到目前为止,测试“通过”,但实际上没有选择任何内容。我的最新尝试如下。

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);
                                });
                            }
                        });
                    }
                });
            }
        });
    }
});
angularjs gruntjs protractor
3个回答
2
投票

类似代码

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

不会起作用,因为由于Java布尔值,promise对象是真实的,所以它总是求值为true。

您需要遵守承诺:

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

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


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();
                }
            });
        }
    });
});

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();
            }

        });
    });
© www.soinside.com 2019 - 2024. All rights reserved.