【问题标题】:Can Protractor ignores the timeouts of 3rd party plugin in angular?Protractor 可以忽略角度中第 3 方插件的超时吗?
【发布时间】:2021-02-01 15:41:31
【问题描述】:

我正在使用 amchart 在 Angular 应用程序中显示我的数据分析。如果我在包含 amchart 插件的页面上运行 E2E 测试,它无法完成(脚本超时),因为它使用图表的实时更新(动态)...

控制台中的这个命令'getAllAngularTestabilities()'显示页面上已经挂起Macrotasks,所以如果量角器在这里不工作,完全没关系。

[Testability]
0: Testability
taskTrackingZone: TaskTrackingZoneSpec {name: "TaskTrackingZone", microTasks: Array(0), macroTasks: Array(3), eventTasks: Array(474), properties: {…}}
_callbacks: []
_didWork: true
_isZoneStable: true
_ngZone: NgZone
hasPendingMacrotasks: true
hasPendingMicrotasks: false
isStable: true
lastRequestAnimationFrameId: -1
nativeRequestAnimationFrame: ƒ requestAnimationFrame()
onError: EventEmitter_ {_isScalar: false, observers: Array(1), closed: false, isStopped: false, hasError: false, …}
onMicrotaskEmpty: EventEmitter_ {_isScalar: false, observers: Array(1), closed: false, isStopped: false, hasError: false, …}
onStable: EventEmitter_ {_isScalar: false, observers: Array(2), closed: false, isStopped: false, hasError: false, …}
onUnstable: EventEmitter_ {_isScalar: false, observers: Array(1), closed: false, isStopped: false, hasError: false, …}
shouldCoalesceEventChangeDetection: false

我在那个页面上有 3 个图表,所以我检查了 NgZone 所说的:每秒收到一个请求,我无法关闭它们。我正在尝试在 amchart 的文档中找到解决方案,但我还没有找到任何东西......

ZONE pending tasks= 
(3) [ZoneTask, ZoneTask, ZoneTask]
0: ZoneTask
callback: ƒ ()
cancelFn: undefined
creationLocation: Error: Task 'macroTask' from 'setTimeout'. at TaskTrackingZoneSpec.push.FGvd.TaskTrackingZoneSpec.onScheduleTask (http://localhost:4200/vendor.js:54102:40) at ZoneDelegate.scheduleTask (http://localhost:4200/polyfills.js:9471:55) at Object.onScheduleTask (http://localhost:4200/polyfills.js:9365:69) at ZoneDelegate.scheduleTask (http://localhost:4200/polyfills.js:9471:55) at Zone.scheduleTask (http://localhost:4200/polyfills.js:9303:47) at Zone.scheduleMacroTask (http://localhost:4200/polyfills.js:9326:29) at scheduleMacroTaskWithCurrentZone (http://localhost:4200/polyfills.js:10227:29) at http://localhost:4200/polyfills.js:11679:34 at proto.<computed> (http://localhost:4200/polyfills.js:10542:52) at loop_1 (http://localhost:4200/vendor.js:23731:42)
data: {isPeriodic: false, delay: 1000, args: Arguments(2), handleId: 1516}
invoke: ƒ ()
runCount: 0
scheduleFn: ƒ scheduleTask(task)
source: "setTimeout"
type: "macroTask"
_state: "notScheduled"
_zone: Zone {_parent: Zone, _name: "angular", _properties: {…}, _zoneDelegate: ZoneDelegate}
_zoneDelegates: null
state: (...)
zone: (...)
__proto__: Object
1: ZoneTask {_zone: Zone, runCount: 0, _zoneDelegates: null, _state: "notScheduled", type: "macroTask", …}
2: ZoneTask {_zone: Zone, runCount: 0, _zoneDelegates: null, _state: "notScheduled", type: "macroTask", …}
length: 3
__proto__: Array(0)

更新!

我可以通过一个小小的解决方法来避免这个问题。 需要创建一个使用 runOutsideAngular() 的函数,如果我在回调中创建图表,则不会运行宏任务!

constructor(@Inject(PLATFORM_ID) private platformId, private zone: NgZone) {
  }

  // Run the function only in the browser
  browserOnly(f: () => void): void {
    if (isPlatformBrowser(this.platformId)) {
      this.zone.runOutsideAngular(() => {
        f();
      });
    }
  }

  ngOnInit(): void {
    this.browserOnly(() => {
      this.chart = am4core.create('line-chart-placeholder', am4charts.XYChart);
  });
}

【问题讨论】:

    标签: protractor amcharts


    【解决方案1】:

    是的,你可以忽略它。您需要禁用等待页面准备好的主量角器功能,如下所示

    await browser.waitForAngularEnabled(false)
    

    欲了解更多信息,请阅读here

    【讨论】:

    • 没关系,但是如果我使用这个解决方案,我必须打开/关闭等待。 ://
    • 不,为什么?把它们留在那里。我个人总是使用量角器的内置等待和显式等待元素以获得额外的稳定性
    • 还请记住,我们只是在谈论您的应用程序的一页(如果我理解正确的话),您可以在转到该页面之前禁用等待 angular,一旦您返回就重新启用。所以我认为这没有任何问题
    • 但如果你坚持,有一个解决方案可以有条件地禁用等待,lmk,但我不会走那条路
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-11-12
    • 2014-12-09
    • 1970-01-01
    • 2023-03-08
    • 1970-01-01
    • 2019-05-15
    • 1970-01-01
    相关资源
    最近更新 更多