summaryrefslogtreecommitdiffstats
path: root/remote/test/browser/network/browser_navigationEvents.js
blob: 4f088214182b62c99d99407eca09b629cb1b5492 (plain)
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

// Test order and consistency of Network/Page events as a whole.
// Details of specific events are checked in event-specific test files.

const BASE_PATH = "http://example.com/browser/remote/test/browser/network";
const FRAMESET_URL = `${BASE_PATH}/doc_frameset.html`;
const FRAMESET_JS_URL = `${BASE_PATH}/file_framesetEvents.js`;
const PAGE_URL = `${BASE_PATH}/doc_networkEvents.html`;
const PAGE_JS_URL = `${BASE_PATH}/file_networkEvents.js`;

add_task(async function eventsForTopFrameNavigation({ client }) {
  const { history, frameId: frameIdNav } = await prepareTest(
    client,
    FRAMESET_URL,
    10
  );

  const documentEvents = filterEventsByType(history, "Document");
  const scriptEvents = filterEventsByType(history, "Script");
  const subdocumentEvents = filterEventsByType(history, "Subdocument");

  is(documentEvents.length, 2, "Expected number of Document events");
  is(subdocumentEvents.length, 2, "Expected number of Subdocument events");
  is(scriptEvents.length, 4, "Expected number of Script events");

  const navigatedEvents = history.findEvents("Page.navigate");
  is(navigatedEvents.length, 1, "Expected number of navigate done events");

  const frameAttachedEvents = history.findEvents("Page.frameAttached");
  is(frameAttachedEvents.length, 1, "Expected number of frame attached events");

  // network events for document and script
  assertEventOrder(documentEvents[0], documentEvents[1]);
  assertEventOrder(documentEvents[1], navigatedEvents[0], {
    ignoreTimestamps: true,
  });
  assertEventOrder(navigatedEvents[0], scriptEvents[0], {
    ignoreTimestamps: true,
  });
  assertEventOrder(scriptEvents[0], scriptEvents[1]);

  const docRequest = documentEvents[0].payload;
  is(docRequest.documentURL, FRAMESET_URL, "documenURL matches target url");
  is(docRequest.frameId, frameIdNav, "Got the expected frame id");
  is(docRequest.request.url, FRAMESET_URL, "Got the Document request");

  const docResponse = documentEvents[1].payload;
  is(docResponse.frameId, frameIdNav, "Got the expected frame id");
  is(docResponse.response.url, FRAMESET_URL, "Got the Document response");
  ok(!!docResponse.response.headers.server, "Document response has headers");
  // TODO? response reports extra request header "upgrade-insecure-requests":"1"
  // Assert.deepEqual(
  //   docResponse.response.requestHeaders,
  //   docRequest.request.headers,
  //   "Response event reports same request headers as request event"
  // );

  const scriptRequest = scriptEvents[0].payload;
  is(
    scriptRequest.documentURL,
    FRAMESET_URL,
    "documentURL is trigger document"
  );
  is(scriptRequest.frameId, frameIdNav, "Got the expected frame id");
  is(scriptRequest.request.url, FRAMESET_JS_URL, "Got the Script request");

  const scriptResponse = scriptEvents[1].payload;
  is(scriptResponse.frameId, frameIdNav, "Got the expected frame id");
  todo(
    scriptResponse.loaderId === docRequest.loaderId,
    "The same loaderId is used for dependent responses (Bug 1637838)"
  );
  is(scriptResponse.response.url, FRAMESET_JS_URL, "Got the Script response");
  Assert.deepEqual(
    scriptResponse.response.requestHeaders,
    scriptRequest.request.headers,
    "Response event reports same request headers as request event"
  );

  // frame is attached after all resources of the document have been loaded
  // and before sub document starts loading
  assertEventOrder(scriptEvents[1], frameAttachedEvents[0], {
    ignoreTimestamps: true,
  });
  assertEventOrder(frameAttachedEvents[0], subdocumentEvents[0], {
    ignoreTimestamps: true,
  });

  const {
    frameId: frameIdSubFrame,
    parentFrameId,
  } = frameAttachedEvents[0].payload;
  is(parentFrameId, frameIdNav, "Got expected parent frame id");

  // network events for subdocument and script
  assertEventOrder(subdocumentEvents[0], subdocumentEvents[1]);
  assertEventOrder(subdocumentEvents[1], scriptEvents[2]);
  assertEventOrder(scriptEvents[2], scriptEvents[3]);

  const subdocRequest = subdocumentEvents[0].payload;
  is(
    subdocRequest.documentURL,
    FRAMESET_URL,
    "documentURL is trigger document"
  );
  is(subdocRequest.frameId, frameIdSubFrame, "Got the expected frame id");
  is(subdocRequest.request.url, PAGE_URL, "Got the Subdocument request");

  const subdocResponse = subdocumentEvents[1].payload;
  is(subdocResponse.frameId, frameIdSubFrame, "Got the expected frame id");
  is(subdocResponse.response.url, PAGE_URL, "Got the Subdocument response");

  const subscriptRequest = scriptEvents[2].payload;
  is(subscriptRequest.documentURL, PAGE_URL, "documentURL is trigger document");
  is(subscriptRequest.frameId, frameIdSubFrame, "Got the expected frame id");
  is(subscriptRequest.request.url, PAGE_JS_URL, "Got the Script request");

  const subscriptResponse = scriptEvents[3].payload;
  is(subscriptResponse.frameId, frameIdSubFrame, "Got the expected frame id");
  is(subscriptResponse.response.url, PAGE_JS_URL, "Got the Script response");
  todo(
    subscriptResponse.loaderId === subdocRequest.loaderId,
    "The same loaderId is used for dependent responses (Bug 1637838)"
  );
  Assert.deepEqual(
    subscriptResponse.response.requestHeaders,
    subscriptRequest.request.headers,
    "Response event reports same request headers as request event"
  );

  const lifeCycleEvents = history
    .findEvents("Page.lifecycleEvent")
    .map(event => event.payload);
  for (const { name, loaderId } of lifeCycleEvents) {
    is(
      loaderId,
      docRequest.loaderId,
      `${name} lifecycle event has same loaderId as Document request`
    );
  }
});

async function prepareTest(client, url, totalCount) {
  const REQUEST = "Network.requestWillBeSent";
  const RESPONSE = "Network.responseReceived";
  const FRAMEATTACHED = "Page.frameAttached";
  const LIFECYCLE = "Page.livecycleEvent";

  const { Network, Page } = client;
  const history = new RecordEvents(totalCount);

  history.addRecorder({
    event: Network.requestWillBeSent,
    eventName: REQUEST,
    messageFn: payload => {
      return `Received ${REQUEST} for ${payload.request?.url}`;
    },
  });

  history.addRecorder({
    event: Network.responseReceived,
    eventName: RESPONSE,
    messageFn: payload => {
      return `Received ${RESPONSE} for ${payload.response?.url}`;
    },
  });

  history.addRecorder({
    event: Page.frameAttached,
    eventName: FRAMEATTACHED,
    messageFn: ({ frameId, parentFrameId: parentId }) => {
      return `Received ${FRAMEATTACHED} frame=${frameId} parent=${parentId}`;
    },
  });

  history.addRecorder({
    event: Page.lifecycleEvent,
    eventName: LIFECYCLE,
    messageFn: payload => {
      return `Received ${LIFECYCLE} ${payload.name}`;
    },
  });

  await Network.enable();
  await Page.enable();

  const navigateDone = history.addPromise("Page.navigate");
  const { frameId } = await Page.navigate({ url }).then(navigateDone);
  ok(frameId, "Page.navigate returned a frameId");

  info("Wait for events");
  const events = await history.record();

  info(`Received events: ${events.map(getDescriptionForEvent)}`);
  is(events.length, totalCount, "Received expected number of events");

  return { history, frameId };
}