title: Protractor - Error: Index out of bound exception while using the same function for the second time
body: <p>I have the following function which selects a category from a list of available categories. This function works fine in my first test. But the same function with a different valid category name in my second test fails with the following error.</p>
  
  <p>Error: Index out of bound. Trying to access element at index: 0, but there are only 0 elements that match locator By.cssSelector(".grid-view-builder__category")</p>
  
  <pre><code>this.categoryElements = element.all(by.css('.grid-view-builder__category'));
  
  this.selectCategory = function (categoryName) {
  
      var filteredCategories = this.categoryElements.filter(function (category) {
          return category.getText().then(function (text) {
              log.info(text);
              return text === categoryName;
          })
      })
  
      filteredCategories.first().click().then(function () {
          log.info("Select Category: " + categoryName);
      }).then(null, function (err) {
          log.error("Category: " + categoryName + " Not Found !!" + err);
      });
  
  }
  </code></pre>
  
  <p>Spec File</p>
  
  <pre><code>var columnSelect = require('pages/grid/columns/columnselector-page')()
  
  it('Add Publisher ID Column to the Grid &amp; Verify', function () {
  
      var columnCountBefore = columnSelect.getColumnCount();
  
      columnSelect.openColumnSelector();
      columnSelect.selectCategory('Advanced');
      columnSelect.selectColumn('Publisher ID');
      columnSelect.apply();
  
      var columnCountAfter = columnSelect.getColumnCount();
  
      expect(columnCountAfter).toBeGreaterThan(columnCountBefore);
  
  });
  </code></pre>