-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathNetworkTracingTest.ts
More file actions
137 lines (122 loc) · 3.81 KB
/
NetworkTracingTest.ts
File metadata and controls
137 lines (122 loc) · 3.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import {
AddToQueue,
appInfo,
Autowired,
DataUi,
DataUiRequest,
FromQueue,
Job,
Launcher,
NetworkTracing,
NoFilter,
PuppeteerUtil,
PuppeteerWorkerFactory,
RequestMapping,
ViewEncapsulation,
Page
} from "ppspider";
import {Request, Response} from "express";
@DataUi({
label: "NetworkTracing测试",
encapsulation: ViewEncapsulation.None,
// language=CSS
style: `
`,
// language=Angular2HTML
template: `
<div class="container-fluid">
<div class="row" style="margin-top: 12px">
<div class="col-sm-6">
<form>
<div class="form-group">
<label for="url">Url</label>
<input [(ngModel)]="url" id="url" name="url" class="form-control">
</div>
<button (click)="addJob(url)" [disabled]="url ? null : true" class="btn btn-primary">
Submit
</button>
</form>
</div>
</div>
<div class="row" style="margin-top: 12px">
<div class="col-md-12">
<p *ngFor="let msg of msgs">{{msg}}</p>
</div>
</div>
</div>
`
})
class NetworkTracingTestUi {
url: string = "https://www.baidu.com";
msgs: string[] = [];
addJob(url: string): Promise<any> {
// just a stub
return null;
}
onProcess(msg: string) {
if (msg.startsWith("open(") && msg.endsWith(")")) {
const jobId = msg.substring(5, msg.length - 1);
const viewerUrl = `https://chromedevtools.github.io/timeline-viewer/?loadTimelineFromURL=${document.baseURI}tracing/?id=${jobId}`;
window.open(viewerUrl, "_blank");
}
else {
this.msgs.push(msg);
}
}
}
class NetworkTracingTask {
@Autowired(NetworkTracingTestUi)
private networkTracingTestUi: NetworkTracingTestUi;
@DataUiRequest(NetworkTracingTestUi.prototype.addJob)
@AddToQueue({
name: "networkTracing",
filterType: NoFilter
})
addJob(url: string) {
this.networkTracingTestUi.onProcess(url + " 任务添加成功");
return url;
}
@FromQueue({
name: "networkTracing"
})
async networkTracing(page: Page, job: Job) {
this.networkTracingTestUi.onProcess(job.url + " 正在打开");
await PuppeteerUtil.defaultViewPort(page);
const networkTracing = new NetworkTracing(page);
await page.goto(job.url);
const pageRequests = networkTracing.requests();
pageRequests["_id"] = job._id;
await appInfo.db.save("networkTracing", pageRequests);
this.networkTracingTestUi.onProcess(job.url + " NetworkTracing记录成功");
this.networkTracingTestUi.onProcess("open(" + job._id + ")");
}
@RequestMapping("/tracing")
async getTracingFile(req: Request, res: Response) {
const pageRequests = await appInfo.db.findById("networkTracing", req.query.id);
const traceEvents = NetworkTracing.requestsToTraceEvents(pageRequests);
const traceEventsStr = JSON.stringify(traceEvents);
res.header("Access-Control-Allow-Origin", "https://chromedevtools.github.io");
res.header("Access-Control-Allow-Credentials", "true");
res.header("Access-Control-Allow-Headers", "Content-Type,X-Requested-With");
res.header("Access-Control-Allow-Methods","GET,OPTIONS");
res.header('Content-Type', "text/plain; charset=utf-8");
res.write(traceEventsStr, 'utf-8');
res.end();
}
}
@Launcher({
workplace: "workplace_networkTracing",
tasks: [
NetworkTracingTask
],
dataUis: [
NetworkTracingTestUi
],
workerFactorys: [
new PuppeteerWorkerFactory({
headless: false,
devtools: true
})
]
})
class App {}