有没有办法在 Protractor 的控制流中添加定时器事件来测量特定任务的执行时间?

Is there a way to add timer events to Protractor's control flow to measure execution time of specific tasks?

下面是一个简单的测试示例,点击字段输入某个值:

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');
});

我想做的是点击该字段需要多长时间并输入一些值,然后以某种方式输出它的执行时间。我原本以为我可以只插入 console.time()console.timeEnd() 函数,但这当然不适用于量角器的控制流程,对吧?

如能提供任何帮助,我们将不胜感激。谢谢!

您所要做的就是在控制流的上下文中引用您的变量:

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');
});

您记录的时间可能不太精确,因为量角器在执行点击之前有一个内置的 waitForAngular(),并且不会在按下回车键后立即等待任何事情发生。如果您有兴趣捕获页面稳定所需的时间,您可以在记录结束时间之前添加自己的 browser.waitForAngular()