Search code examples
javascriptangularjsprotractorrepeater

Protractor: can't get repeater element


I'm testing basic example from https://angularjs.org/ called "Add new control". The problem is that after adding new value to repeater I can't obtain any element . Only first one.

...
var listRepeater = by.repeater('todo in todoList.todos');
var list = element.all(listRepeater);
expect(list.count()).toEqual(2);

addNewElement();

expect(list.count()).toEqual(3);
expect(list.get(0).element(by.model("todo.done")).getAttribute("checked")).toEqual("true");
expect(list.get(2).element(by.model("todo.done")).getAttribute("checked")).toEqual("true");

After adding new element check on element's count is Ok: it's equal ti 3 and I know that new values was added properly. And I can clearly get right "checked" property of first element (it's disabled by default). But when I'm trying to get(2) element I have error about null object. Same with the get(1). Only get(0) works fine. But 2 rows repeater have by default with out a doubt.


Solution

  • The problem is that the second and the third elements are unchecked and don't have a checked attribute - this is why you are getting null.

    Instead, a correct way to check if a checkbox is checked is to use isSelected():

    expect(list.get(0).element(by.model("todo.done")).isSelected()).toEqual(true);
    expect(list.get(2).element(by.model("todo.done")).isSelected()).toEqual(false);
    

    Worked for me.