量角器 - 索引超出范围。尝试访问索引处的元素:0

时间:2017-03-27 02:44:28

标签: javascript protractor

我想知道是否有人设法解决了这个问题?基本上我使用这个功能

this.removeContributorFromPermissions = function(name) {
        $('button[title="Unlock owner management options"]').click();
        browser.sleep(2000); // here to create a delay until button appears.
        var remove = element.all(by.repeater('existingOwner in companyOwners')).filter(function(rowElement){
            return rowElement.element(by.css('td[ng-bind="existingOwner.Name"]')).getText().then(function(text){
                return text.trim() == name;
            });
        });
        remove.first().$('.btn.close').click();
        element(by.buttonText('Remove')).click();
    };

这两个断言

it('Newly added new contributor is deleted from the company permissions', function() {
        settingsPage.removeContributorFromPermissions(browser.params.settings.contributors.newContributorName);
        var owners = element.all(by.repeater('existingOwner in companyOwners').column('existingOwner.Name'));
        expect(owners).not.toContain(browser.params.settings.contributors.newContributorName);
    });

it('Newly added existing contributor is deleted from the company permissions', function() {
        settingsPage.removeContributorFromPermissions(browser.params.settings.contributors.existingContributorName);
        var owners = element.all(by.repeater('existingOwner in companyOwners').column('existingOwner.Name'));
        expect(owners).not.toContain(browser.params.settings.contributors.existingContributorName);
    });

第一次迭代运行并成功删除用户,但第二次迭代在完成browser.sleep()后停止;

完全错误

1) Testing company permission area Newly added existing contributor is deleted from the company permissions
  - Failed: Index out of bound. Trying to access element at index: 0, but there are only 0 elements that match locator by.repeater("existingOwner in companyOwners")

1 个答案:

答案 0 :(得分:1)

我相信你的2s硬睡眠是不够的,这就是让测试变得片状的东西(在第一次迭代中它足够但在几秒钟内它没有)。您是否考虑过使用ExpectedConditions

    var EC = protractor.ExpectedConditions;
    var repeaterElement = element(by.repeater('existingOwner in companyOwners'));

    //Wait up to 10 seconds for elements to be visible
    browser.wait(EC.visibilityOf(repeaterElement), 10000);
    var remove = element.all(by.repeater('existingOwner in companyOwners')).filter(function(rowElement){
            return rowElement.element(by.css('td[ng-bind="existingOwner.Name"]')).getText().then(function(text){
                 return text.trim() == name;
            });
    });
    remove.first().$('.btn.close').click();
    element(by.buttonText('Remove')).click();