How to track which async tasks protractor is waiting on?

后端 未结 4 1859
孤城傲影
孤城傲影 2021-01-31 04:39

I have a medium sized Angular application and for some reasons some of my protractor tests are timing out when run against my live production environment.

I am fairly sur

4条回答
  •  逝去的感伤
    2021-01-31 04:51

    JeB did a great job suggesting his way (some solution is better than nothing). In fact, there is way to get list of pending tasks without patching zone.js file. I get this idea while looking at angular sources: https://github.com/angular/angular/blob/master/packages/core/src/zone/ng_zone.ts#L134

    Here is how to:

    1. Import node_modules/zone.js/dist/task-tracking.js file after zone.js
    2. Then in your code get NgZone instance and use it like
    import * as _ from "lodash";
    ...
    const ngZone = moduleInstance.injector.get(NgZone);
    setInterval(() => {
        var taskTrackingZone = (ngZone)._inner.getZoneWith("TaskTrackingZone");
        if (!taskTrackingZone) {
            throw new Error("'TaskTrackingZone' zone not found! Have you loaded 'node_modules/zone.js/dist/task-tracking.js'?");
        }
        var tasks: any[] = taskTrackingZone._properties.TaskTrackingZone.getTasksFor("macroTask");
        tasks = _.clone(tasks);
        if (_.size(tasks) > 0) {
            console.log("ZONE pending tasks=", tasks);
        }
    }, 1000);
    
    

    Example is placed here: https://stackblitz.com/edit/zonejs-pending-tasks.

    How it works:

提交回复
热议问题