失败:脚本超时:在11秒内未收到结果来自:任务:Protractor.waitForAngular()-定位器:作者(css选择器,#my-btn)

Fra*_*rzi 1 typescript protractor e2e-testing angular angular-e2e

我正在尝试使用Protractor为我的Angular应用程序编写一些e2e测试。

我有一个id=my-btn想要单击的简单html按钮,使用:

$('#my-btn').click();
Run Code Online (Sandbox Code Playgroud)

不幸的是我遇到以下错误:

失败:脚本超时:11秒内未收到结果

来自:任务:Protractor.waitForAngular()-定位器:通过(css选择器,#my-btn)

(Session info: chrome=73.0.3683.75)
(Driver info: chromedriver=2.46.628411 (3324f4c8be9ff2f70a05a30ebc72ffb013e1a71e),platform=Mac OS X 10.14.3 x86_64)
Run Code Online (Sandbox Code Playgroud)

如果在点击之前我设置了:

browser.waitForAngularEnabled(false);
Run Code Online (Sandbox Code Playgroud)

那我就没有任何错误 问题在于这样做意味着:

 * If set to false, Protractor will not wait for Angular $http and $timeout
 * tasks to complete before interacting with the browser. This can cause
 * flaky tests, but should be used if, for instance, your app continuously
 * polls an API with $timeout.
Run Code Online (Sandbox Code Playgroud)

所以我想什么导致waitForAngular操作超时。

有没有办法检查仍然挂起的HTTP超时

我想调试我的应用程序以了解发生了什么。

Ben*_*min 8

我对此有些麻烦。您可以尝试一些方法。

  1. 手动检查是否有定时操作。以我的应用为例,该应用timer每5分钟进行一次健康检查。但是,这种timer操作不断出现在堆栈上,这意味着Angular从未稳定下来。

如果确实找到这样的操作,则可以使用ngZone.runOutsideAngular()它来防止其破坏测试的稳定性。

constructor(
    private ngZone: NgZone
  ) {}

ngOnInit() {
  this.ngZone.runOutsideAngular(() => {
    this.appStatusInterval = interval(this.appStatusUpdateIntervalTime)
       // rest of your code here
    });
  });
}
Run Code Online (Sandbox Code Playgroud)
  1. 打开开发工具并运行getAllAngularTestabilities()。尝试从那里获取什么信息。您可以尝试从源代码中获取其他数据。这点可能对您特别有用:
isStable(): boolean {
    return this._isZoneStable && this._pendingCount === 0 && !this._ngZone.hasPendingMacrotasks;
}
Run Code Online (Sandbox Code Playgroud)

通过依次检查这三个条件,您至少可以对导致Angular不稳定的问题有更多的了解。