量角器如何测试列表中的最后一个项目类

时间:2014-03-24 12:12:47

标签: javascript angularjs protractor

在我的应用程序中我有一个项目列表,我可以添加新项目,我可以检查列表是否有新项目,现在我想测试新项目是否有类'nuovo',这是我的测试用例:

    it("should add an item to the address list with class 'nuovo'",function(){
    ptor.findElements(protractor.By.repeater('a in customer.LIVES_IN')).then(function(arr) {
      expect(arr.length).toEqual(2);
        var initial_length = arr.length;
      // set values to  new address
      ptor.findElement(protractor.By.model('street')).sendKeys('via');
      ptor.findElement(protractor.By.model('city')).sendKeys('G');
      ptor.findElement(protractor.By.model('number')).sendKeys('xx');
      ptor.findElement(protractor.By.model('use')).sendKeys('n');
        element(by.id('addAddressButton')).click();

        //check one address more
        ptor.findElements(protractor.By.repeater('a in customer.LIVES_IN')).then(function(arr) {

            expect(arr.length).toEqual(initial_length+1);
            var inserted_item = element(arr[arr.length-1]) //get the last element, the just inserted one
            //console.log(inserted_item);
            var lastRow = element(by.repeater('a in customer.LIVES_IN').last);
            expect(lastRow).toHaveClass('nuovo');
        })
});

})

我收到此错误:

    Failures:

  1) E2E: main page should add an item to the address list with class 'nuovo'
   Message:
     TypeError: Object [object Object] has no method 'toHaveClass'
   Stacktrace:
     Error
    at null.<anonymous> (/home/arpho/Projects/neo4Scrum/test/e2e/addAddress_spec.js:19:1)
    at Object.<anonymous> (/home/arpho/Projects/neo4Scrum/test/e2e/addAddress_spec.js:2:1)
At async task:
      TypeError: Object [object Object] has no method 'toHaveClass'
    at /home/arpho/Projects/neo4Scrum/test/e2e/addAddress_spec.js:38:29
==== async task ====
WebDriver.call(function)
    at Object.findElementsOverride (/home/arpho/Projects/neo4Scrum/node_modules/protractor/lib/locators.js:273:21)
    at Protractor.findElements (/home/arpho/Projects/neo4Scrum/node_modules/protractor/lib/protractor.js:765:21)
    at /home/arpho/Projects/neo4Scrum/test/e2e/addAddress_spec.js:31:14
==== async task ====
asynchronous test function

Finished in 20.703 seconds

2 个答案:

答案 0 :(得分:1)

我会这样做的:

expect(lastRow.getAttribute('className')).toMatch('nuovo');

答案 1 :(得分:0)

我做了一些脏事,但现在我的测试似乎有效: 这是我的新测试用例:

it("should add an item to the address list with class 'nuovo'", function () {
  ptor.findElements(protractor.By.repeater('a in customer.LIVES_IN')).then(function (arr) {
    expect(arr.length).toEqual(2);
    var initial_length = arr.length;
    // set values to  new address
    ptor.findElement(protractor.By.model('street')).sendKeys('via');
    ptor.findElement(protractor.By.model('city')).sendKeys('G');
    ptor.findElement(protractor.By.model('number')).sendKeys('');
    ptor.findElement(protractor.By.model('use')).sendKeys('');
    element(by.id('addAddressButton')).click();

    //check one address more
    ptor.findElements(protractor.By.repeater('a in customer.LIVES_IN')).then(function (arr) {

      expect(arr.length).toEqual(initial_length + 1);
      var inserted_item = element(arr[arr.length - 1]) //get the last element, the just inserted one
      //console.log(inserted_item);
      var lastRow = arr[2];
      var attributes = '';
      //console.log(
      arr[2].getAttribute('className').then(function (o) {
        // console.log('then');
        //console.log(o)
        attributes = o;
        //console.log(attributes);
        var pos = attributes.indexOf('nuovo'); // if != -1 nuovo is in the attribute's list
        //console.log(attributes.indexOf('nuovo'));
        expect(pos != -1).toBe(true);
      });

    });
  });
});