andrbmgi
andrbmgi

Reputation: 528

Protractor: How to check if an entry occurs at all in a repeater

My tests create a new entry in the app I am testing. The test inserts a comment that includes a timestamp so I can recognize it later.

On the page where the test runs, a list of all entries in the database is presented, each with a button next to it. The test should get the repeater, test if the task is in there at all, navigate to it and click the correct button.

I can navigate to the task and click the button but I am stumped on how to test if the test entry is there at all.

it('should start a tasks', function() {

    repeater = by.repeater('task in taskList');
    tasks = element.all( repeater );

    expect( tasks.count() ).not.toBe(0);

    // ???
    // the missing piece would go here 
    // = Check if the list contains a task with the time stamped comment

    // Now in detail we single out the task an click its button to
    // perform additional tests
    tasks.map( function(elmnt, indx){               
      description = elmnt.findElement( by.className('taskDescription') );
      description.getText().then( function(text){
        if ( text.indexOf( timeStampedComment ) > -1 ) {
          console.log("Found the test entry!");
          // click the button *(1)
          // some expectation
        };
      });
    });

});

If the test never gets to *(1) it would go through succesfully, so I need to test if it ever got there or if a fitting entry is there at all. Due to the complex promises structure I am unsure how to do it.

I made a plunkr as requested.

How do I know if the test ever ran? (I might have a brain knot here and the solution is apparent)

EDIT: I updated the plnkr to actually contain a test. As you can see the test succeeds if you comment out the test entry.

Upvotes: 1

Views: 3705

Answers (2)

Juri
Juri

Reputation: 32950

I'm new to Protractor, but did something like this, which solved the issue for me:

it('should have Juri in the list', function(){
  var data = element.all(by.repeater('person in people').column('person.name'));
  data.getText().then(function(text){
    expect(text).toContain('Juri');
  });
});

or even the shorter form

it('should have Juri in the list', function(){
  var data = element.all(by.repeater('person in people').column('person.name'));

  expect(data.getText()).toContain('Juri');
});

(note, in this case Protractor.expect handles the promise for you)

Upvotes: 3

Michal Charemza
Michal Charemza

Reputation: 27062

If I'm understanding. You want to test for the existance of an item in a list that satisfies some condition, and fail an expectation if it's not found. You can do this with the returned value of map and toContain:

var hasComment = tasks.map(function(task, index) { 
  var description = task.findElement(by.className('taskDescription'));
  return description.getText().then(function(text) {
    var hasTimeStamp = (text.indexOf(timeStampedComment) > -1);
    return hasTimeStamp;
  });
});

expect(hasComment).toContain(true);

Notice the return statements. These are key to getting the promise hasComment's resolved value to be an array of boolean tests, each the result of the test for a timestamp.

Upvotes: 1

Related Questions