summaryrefslogtreecommitdiffstats
path: root/remote/cdp/test/browser/target/browser_getTargets.js
blob: b0eb0378ce8314811c13f3f5ee23173030afd2f9 (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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

const PAGE_TEST =
  "https://example.com/browser/remote/cdp/test/browser/target/doc_test.html";

add_task(
  async function getTargetsDetails({ client }) {
    const { Target, target } = client;

    await loadURL(PAGE_TEST);

    const { targetInfos } = await Target.getTargets();

    Assert.equal(targetInfos.length, 1, "Got expected amount of targets");

    const targetInfo = targetInfos[0];
    is(targetInfo.id, target.id, "Got expected target id");
    is(targetInfo.type, "page", "Got expected target type");
    is(targetInfo.title, "Test Page", "Got expected target title");
    is(targetInfo.url, PAGE_TEST, "Got expected target URL");
    ok(targetInfo.attached, "Got expected target attached status");
  },
  { createTab: false }
);

add_task(
  async function getTargetsCount({ client }) {
    const { Target, target } = client;
    const { targetInfo: newTabTargetInfo } = await openTab(Target);

    await loadURL(PAGE_TEST);

    const { targetInfos } = await Target.getTargets();

    Assert.equal(targetInfos.length, 2, "Got expected amount of targets");
    const targetIds = targetInfos.map(info => info.id);
    ok(targetIds.includes(target.id), "Got expected original target id");
    ok(targetIds.includes(newTabTargetInfo.id), "Got expected new target id");
  },
  { createTab: false }
);

add_task(
  async function getTargetsAttached({ client }) {
    const { Target } = client;
    await openTab(Target);

    await loadURL(PAGE_TEST);

    const { targetInfos } = await Target.getTargets();

    ok(targetInfos[0].attached, "Current target is attached");
    ok(!targetInfos[1].attached, "New tab target is detached");
  },
  { createTab: false }
);

add_task(
  async function getTargets_filterAllBlank({ client }) {
    const { Target } = client;

    await loadURL(PAGE_TEST);

    // Blank/all filter so all targets are returned, including main process
    const { targetInfos } = await Target.getTargets({
      filter: [{}],
    });

    is(
      targetInfos.length,
      2,
      "Got expected amount of targets with all (blank) filter"
    );

    const pageTarget = targetInfos.find(info => info.type === "page");
    ok(!!pageTarget, "Found page target in targets with all (blank) filter");

    const mainProcessTarget = targetInfos.find(info => info.type === "browser");
    ok(
      !!mainProcessTarget,
      "Found main process target in targets with all (blank) filter"
    );
  },
  { createTab: false }
);

add_task(
  async function getTargets_filterAllExplicit({ client }) {
    const { Target } = client;

    await loadURL(PAGE_TEST);

    // Blank/all filter so all targets are returned, including main process
    const { targetInfos } = await Target.getTargets({
      filter: [{ type: "browser" }, { type: "page" }],
    });

    is(
      targetInfos.length,
      2,
      "Got expected amount of targets with all (explicit) filter"
    );

    const pageTarget = targetInfos.find(info => info.type === "page");
    ok(!!pageTarget, "Found page target in targets with all (explicit) filter");

    const mainProcessTarget = targetInfos.find(info => info.type === "browser");
    ok(
      !!mainProcessTarget,
      "Found main process target in targets with all (explicit) filter"
    );
  },
  { createTab: false }
);

add_task(
  async function getTargets_filterPage({ client }) {
    const { Target } = client;

    await loadURL(PAGE_TEST);

    // Filter so only page targets are returned
    // This returns same as default but pass our own custom filter to ensure
    const { targetInfos } = await Target.getTargets({
      filter: [{ type: "page" }],
    });

    is(
      targetInfos.length,
      1,
      "Got expected amount of targets with page filter"
    );
    is(
      targetInfos[0].type,
      "page",
      "Got expected type 'page' of target from page filter"
    );
  },
  { createTab: false }
);

add_task(
  async function getTargets_filterBrowser({ client }) {
    const { Target } = client;

    await loadURL(PAGE_TEST);

    // Filter so only main process target is returned
    const { targetInfos } = await Target.getTargets({
      filter: [{ type: "browser" }],
    });

    is(
      targetInfos.length,
      1,
      "Got expected amount of targets with browser filter"
    );
    is(
      targetInfos[0].type,
      "browser",
      "Got expected type 'browser' of target from browser filter"
    );
  },
  { createTab: false }
);

add_task(
  async function getTargets_filterExcludePage({ client }) {
    const { Target } = client;

    await loadURL(PAGE_TEST);

    // Filter so page targets are excluded (so only main process target is returned)
    // A blank object ({}) means include everything else
    const { targetInfos } = await Target.getTargets({
      filter: [{ type: "page", exclude: true }, {}],
    });

    is(
      targetInfos.length,
      1,
      "Got expected amount of targets with exclude page filter"
    );
    is(
      targetInfos[0].type,
      "browser",
      "Got expected type 'browser' of target from exclude page filter"
    );
  },
  { createTab: false }
);

add_task(
  async function getTargets_filterExcludeBrowserIncludePage({ client }) {
    const { Target } = client;

    await loadURL(PAGE_TEST);

    // Filter so main process is excluded and only page types are returned explicitly
    const { targetInfos } = await Target.getTargets({
      filter: [{ type: "browser", exclude: true }, { type: "page" }],
    });

    is(
      targetInfos.length,
      1,
      "Got expected amount of targets with exclude browser include page filter"
    );
    is(
      targetInfos[0].type,
      "page",
      "Got expected type 'page' of target from exclude browser include page filter"
    );
  },
  { createTab: false }
);

add_task(
  async function getTargets_filterInvalid({ client }) {
    const { Target } = client;

    for (const filter of [null, true, 1, "foo", {}]) {
      info(`Checking filter with invalid value: ${filter}`);

      let errorThrown = "";
      try {
        await Target.getTargets({
          filter,
        });
      } catch (e) {
        errorThrown = e.message;
      }

      ok(
        errorThrown.match(/filter: array value expected/),
        `Filter fails for invalid type: ${filter}`
      );
    }

    for (const filterEntry of [null, true, 1, "foo", []]) {
      info(`Checking filter entry with invalid value: ${filterEntry}`);

      let errorThrown = "";
      try {
        await Target.getTargets({
          filter: [filterEntry],
        });
      } catch (e) {
        errorThrown = e.message;
      }

      ok(
        errorThrown.match(/filter: object values expected in array/),
        `Filter entry fails for invalid type: ${filterEntry}`
      );
    }

    for (const type of [null, true, 1, [], {}]) {
      info(`Checking filter entry with type as invalid value: ${type}`);

      let errorThrown = "";
      try {
        await Target.getTargets({
          filter: [{ type }],
        });
      } catch (e) {
        errorThrown = e.message;
      }

      ok(
        errorThrown.match(/filter: type: string value expected/),
        `Filter entry type fails for invalid type: ${type}`
      );
    }

    for (const exclude of [null, 1, "foo", [], {}]) {
      info(`Checking filter entry with exclude as invalid value: ${exclude}`);

      let errorThrown = "";
      try {
        await Target.getTargets({
          filter: [{ exclude }],
        });
      } catch (e) {
        errorThrown = e.message;
      }

      ok(
        errorThrown.match(/filter: exclude: boolean value expected/),
        `Filter entry exclude for invalid type: ${exclude}`
      );
    }
  },
  { createTab: false }
);