2

Here's a quick example of a test that clicks a field to enter in some value:

it('should enter someValue into field', function() {
  var field = $('.someField');

  // Insert timer start function here
  field.click();
  this.switchToActiveElement().sendKeys('someValue');
  this.hitEnter();
  // Insert timer stop function here and output result!

  assert.eventually.equal(field.getText(), 'someValue');
});

What I'd like to do is time how long it takes click the field and enter in some value, then output the execution time of this somehow. I was originally thinking I could just insert console.time() and console.timeEnd() functions but of course this won't work with protractor's control flow, right?

Any help with this would be very much appreciated. Thanks!

jjelly
  • 73
  • 9
  • can you define your own time_begin variable and time_end variable, and measure elapsed time by comparing them? E.g. read system time and assign it to time_begin before your clicking and read system time and assign it to time_end, subtract time_begin from time_end. – Yu Zhang Jun 21 '16 at 23:36
  • Well, if you're using [jasmine-spec-reporter](https://www.npmjs.com/package/jasmine-spec-reporter), it has an option for `displayStackDuration` -- However, that captures the time of each `it` block, not the individual actions included under that block. Not sure if that's what you want. – Gunderson Jun 22 '16 at 11:39
  • @YuZhang thanks for the comment! The problem with inserting time_begin and time_end variables in the commented lines to measure time is that protractor hasn't actually performed any operations yet since they are promises that get added to the control flow queue. So the timer will start and stop immediately, which isn't useful. – jjelly Jun 22 '16 at 18:59
  • @Gunderson thanks for the comment as well! Yes, I'm actually using mocha which captures the time of the `it` block as you have mentioned, but I don't want to include the assertion in my time. I've thought about moving the assertion outside of the `it` block to address this, but it would make my code less pretty :( – jjelly Jun 22 '16 at 19:01

1 Answers1

3

All you have to do is refer to your variables within the context of the control flow:

it('should enter someValue into field', function() {
  var field = $('.someField');
  var startTime;

  // Insert timer start function here
  browser.controlFlow().execute(function() {
      startTime = new Date().getTime();
  });

  field.click();
  this.switchToActiveElement().sendKeys('someValue');
  this.hitEnter();

  // Insert timer stop function here and output result!
  browser.controlFlow().execute(function() {
      var endTime = new Date().getTime();
      var elapsedTime = endTime - startTime;
      console.log('elapsedTime = ' + elapsedTime + 'ms');
  });

  assert.eventually.equal(field.getText(), 'someValue');
});

The times you record may not be too precise, because protractor has a built-in waitForAngular() right before performing the click, and does not wait for anything to happen right after hitting the enter key. You could add your own browser.waitForAngular() before recording the endTime if you're interested in capturing how long it took for the page to settle.

martin770
  • 1,271
  • 11
  • 15