theHussle
theHussle

Reputation: 321

Protractor - Index out of bound. Trying to access element at index: 0

I wonder if anyone has managed to solve this issue? Basically I use this function

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

And these two asserts

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

The first iteration runs through and successfully deletes the user, but the second one stops after completing the browser.sleep();

Full Error

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")

Upvotes: 1

Views: 1435

Answers (1)

nilesh
nilesh

Reputation: 14289

I believe your 2s hard sleep is not enough and thats what is making tests flaky (during 1st iteration its enough but during seconds its not). Have you considered using 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();

Upvotes: 1

Related Questions