summaryrefslogtreecommitdiffstats
path: root/remote/cdp/test/browser/runtime/browser_consoleAPICalled.js
blob: a9e6a1625c8c15a38f83a642e17c2d2690e84083 (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
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

const PAGE_CONSOLE_EVENTS =
  "https://example.com/browser/remote/cdp/test/browser/runtime/doc_console_events.html";

add_task(async function noEventsWhenRuntimeDomainDisabled({ client }) {
  await runConsoleTest(client, 0, async () => {
    ContentTask.spawn(gBrowser.selectedBrowser, {}, () => console.log("foo"));
  });
});

add_task(async function noEventsAfterRuntimeDomainDisabled({ client }) {
  const { Runtime } = client;

  await Runtime.enable();
  await Runtime.disable();

  await runConsoleTest(client, 0, async () => {
    ContentTask.spawn(gBrowser.selectedBrowser, {}, () => console.log("foo"));
  });
});

add_task(async function noEventsForJavascriptErrors({ client }) {
  await loadURL(PAGE_CONSOLE_EVENTS);
  const context = await enableRuntime(client);

  await runConsoleTest(client, 0, async () => {
    evaluate(client, context.id, () => {
      document.getElementById("js-error").click();
    });
  });
});

add_task(async function consoleAPI({ client }) {
  const context = await enableRuntime(client);

  const events = await runConsoleTest(client, 1, async () => {
    await evaluate(client, context.id, () => {
      console.log("foo");
    });
  });

  is(events[0].type, "log", "Got expected type");
  is(events[0].args[0].value, "foo", "Got expected argument value");
  is(
    events[0].executionContextId,
    context.id,
    "Got event from current execution context"
  );
});

add_task(async function consoleAPITypes({ client }) {
  const expectedEvents = ["dir", "error", "log", "timeEnd", "trace", "warning"];
  const levels = ["dir", "error", "log", "time", "timeEnd", "trace", "warn"];

  const context = await enableRuntime(client);

  const events = await runConsoleTest(
    client,
    expectedEvents.length,
    async () => {
      for (const level of levels) {
        await evaluate(
          client,
          context.id,
          level => {
            console[level]("foo");
          },
          level
        );
      }
    }
  );

  events.forEach((event, index) => {
    console.log(`Check for event type "${expectedEvents[index]}"`);
    is(event.type, expectedEvents[index], "Got expected type");
  });
});

add_task(async function consoleAPIArgumentsCount({ client }) {
  const argumentList = [[], ["foo"], ["foo", "bar", "cheese"]];

  const context = await enableRuntime(client);

  const events = await runConsoleTest(client, argumentList.length, async () => {
    for (const args of argumentList) {
      await evaluate(
        client,
        context.id,
        args => {
          console.log(...args);
        },
        args
      );
    }
  });

  events.forEach((event, index) => {
    console.log(`Check for event args "${argumentList[index]}"`);

    const argValues = event.args.map(arg => arg.value);
    Assert.deepEqual(argValues, argumentList[index], "Got expected args");
  });
});

add_task(async function consoleAPIArgumentsAsRemoteObject({ client }) {
  const context = await enableRuntime(client);

  const events = await runConsoleTest(client, 1, async () => {
    await evaluate(client, context.id, () => {
      console.log("foo", Symbol("foo"));
    });
  });

  Assert.equal(events[0].args.length, 2, "Got expected amount of arguments");

  is(events[0].args[0].type, "string", "Got expected argument type");
  is(events[0].args[0].value, "foo", "Got expected argument value");

  is(events[0].args[1].type, "symbol", "Got expected argument type");
  is(
    events[0].args[1].description,
    "Symbol(foo)",
    "Got expected argument description"
  );
  ok(!!events[0].args[1].objectId, "Got objectId for argument");
});

add_task(async function consoleAPIByContentInteraction({ client }) {
  await loadURL(PAGE_CONSOLE_EVENTS);
  const context = await enableRuntime(client);

  const events = await runConsoleTest(client, 1, async () => {
    evaluate(client, context.id, () => {
      document.getElementById("console-error").click();
    });
  });

  is(events[0].type, "error", "Got expected type");
  is(events[0].args[0].value, "foo", "Got expected argument value");
  is(
    events[0].executionContextId,
    context.id,
    "Got event from current execution context"
  );
});

async function runConsoleTest(client, eventCount, callback, options = {}) {
  const { Runtime } = client;

  const EVENT_CONSOLE_API_CALLED = "Runtime.consoleAPICalled";

  const history = new RecordEvents(eventCount);
  history.addRecorder({
    event: Runtime.consoleAPICalled,
    eventName: EVENT_CONSOLE_API_CALLED,
    messageFn: payload =>
      `Received ${EVENT_CONSOLE_API_CALLED} for ${payload.type}`,
  });

  const timeBefore = Date.now();
  await callback();

  const consoleAPIentries = await history.record();
  is(consoleAPIentries.length, eventCount, "Got expected amount of events");

  if (eventCount == 0) {
    return [];
  }

  const timeAfter = Date.now();

  // Check basic details for consoleAPICalled events
  consoleAPIentries.forEach(({ payload }) => {
    const timestamp = payload.timestamp;

    ok(
      timestamp >= timeBefore && timestamp <= timeAfter,
      `Timestamp ${timestamp} in expected range [${timeBefore} - ${timeAfter}]`
    );
  });

  return consoleAPIentries.map(event => event.payload);
}