3

My case is as follows - I have a simple page which lists available products:

<ul class="products ng-scope" id="products">
  <!-- ngRepeat: tag in products -->
  <li ng-repeat="product in products" class="ng-scope">
    <a ng-href="/#/viewProduct/1" class="ng-binding" href="/#/viewProduct/1">Product A</a>
  </li><li ng-repeat="product in products" class="ng-scope">
    <a ng-href="/#/viewProduct/2" class="ng-binding" href="/#/viewProduct/2">Product B</a>
  </li><li ng-repeat="product in products" class="ng-scope">
    <a ng-href="/#/viewProduct/3" class="ng-binding" href="/#/viewProduct/3">Product C</a>
  </li><li ng-repeat="product in products" class="ng-scope">
    <a ng-href="/#/viewProduct/4" class="ng-binding" href="/#/viewProduct/4">Product D</a>
  </li><li ng-repeat="product in products" class="ng-scope">
    <a ng-href="/#/viewProduct/5" class="ng-binding" href="/#/viewProduct/5">Product E</a>
  </li>
</ul>

In my angular scenario I want to test if the list of products is equal to expected. I know it can/should be a unit test, but suppose I want an e2e test.

After reading the official docs (http://docs.angularjs.org/guide/dev_guide.e2e-testing - which as usual cover 10% of the subject) and debugging angular code I managed to write the following test:

'use strict';

describe('my app', function() {
  var productsList = [
    'Product A',
    'Product B',
    'Product C',
    'Product D',
    'Product E'
  ];

  it('should list all products', function() {
    browser().navigateTo('/#/products');
    var foundProducts = element('#products li').query(function(elements, done) {
      var productsArray = [];
      elements.each(function(index) {
        productsArray.push(elements[index].innerText);
      });

      done(null, productsArray);
    });

    expect(foundProducts).toEqual(productsList);
  });
});

Can anyone please tell me how this can be done easier?

And the second question, why elements[index].text() is undefined - which in fact is a question how to jet a jQuery object out of elements[index].

4

1 回答 1

4

Found the solutution:

describe('my app', function() {
  var productsList = [
    ['Product A'],
    ['Product B'],
    ['Product C'],
    ['Product D'],
    ['Product E']
  ];

  it('should list all products', function() {
    browser().navigateTo('/#/products');

    expect(repeater('#products li').count()).toEqual(productsList.length);

    for (var i = 0; i < productsList.length; i++) {
      expect(repeater('#products li').row(i)).toEqual(productsList[i]);
    }

  });
});
于 2013-10-02T15:30:53.743 回答