title: Protractor - looping through table to select option from listbox
body: <p>I'm trying to loop through a table, using Protractor, to see if a row contains a select element. Then select 1 of two particular options. Thus far the test 'passes' but nothing is actually being selected. My latest attempts are below.</p>
  
  <pre><code>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 &lt;= 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 &lt; 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();
  });
  </code></pre>
  
  <hr>
  
  <p>This is attempt #2:</p>
  
  <pre><code>element.all(by.repeater('row in datarows')).then(function(rows) {
      for (var i = 1; i &lt;= 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" &amp;&amp; varOne.Length &lt; 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();
          }
      }
  });
  </code></pre>
  
  <hr>
  
  <p>This is the latest attempt:</p>
  
  <pre><code>var varOne = "";
  var varTwo = "";
  
  element.all(by.xpath("//table[@id='tableID']/tbody/tr")).then(function (rows) {
      for (var i = 1; i &lt; (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 &lt; 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);
                                  });
                              }
                          });
                      }
                  });
              }
          });
      }
  });
  </code></pre>