1/**
2 * Copyright 2018 Google Inc. All rights reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17import { waitEvent } from './utils.js';
18import expect from 'expect';
19import {
20  getTestState,
21  setupTestBrowserHooks,
22  setupTestPageAndContextHooks,
23  describeChromeOnly,
24} from './mocha-utils'; // eslint-disable-line import/extensions
25
26describeChromeOnly('Target.createCDPSession', function () {
27  setupTestBrowserHooks();
28  setupTestPageAndContextHooks();
29
30  it('should work', async () => {
31    const { page } = getTestState();
32
33    const client = await page.target().createCDPSession();
34
35    await Promise.all([
36      client.send('Runtime.enable'),
37      client.send('Runtime.evaluate', { expression: 'window.foo = "bar"' }),
38    ]);
39    const foo = await page.evaluate(() => globalThis.foo);
40    expect(foo).toBe('bar');
41  });
42  it('should send events', async () => {
43    const { page, server } = getTestState();
44
45    const client = await page.target().createCDPSession();
46    await client.send('Network.enable');
47    const events = [];
48    client.on('Network.requestWillBeSent', (event) => events.push(event));
49    await page.goto(server.EMPTY_PAGE);
50    expect(events.length).toBe(1);
51  });
52  it('should enable and disable domains independently', async () => {
53    const { page } = getTestState();
54
55    const client = await page.target().createCDPSession();
56    await client.send('Runtime.enable');
57    await client.send('Debugger.enable');
58    // JS coverage enables and then disables Debugger domain.
59    await page.coverage.startJSCoverage();
60    await page.coverage.stopJSCoverage();
61    // generate a script in page and wait for the event.
62    const [event] = await Promise.all([
63      waitEvent(client, 'Debugger.scriptParsed'),
64      page.evaluate('//# sourceURL=foo.js'),
65    ]);
66    // expect events to be dispatched.
67    expect(event.url).toBe('foo.js');
68  });
69  it('should be able to detach session', async () => {
70    const { page } = getTestState();
71
72    const client = await page.target().createCDPSession();
73    await client.send('Runtime.enable');
74    const evalResponse = await client.send('Runtime.evaluate', {
75      expression: '1 + 2',
76      returnByValue: true,
77    });
78    expect(evalResponse.result.value).toBe(3);
79    await client.detach();
80    let error = null;
81    try {
82      await client.send('Runtime.evaluate', {
83        expression: '3 + 1',
84        returnByValue: true,
85      });
86    } catch (error_) {
87      error = error_;
88    }
89    expect(error.message).toContain('Session closed.');
90  });
91  it('should throw nice errors', async () => {
92    const { page } = getTestState();
93
94    const client = await page.target().createCDPSession();
95    const error = await theSourceOfTheProblems().catch((error) => error);
96    expect(error.stack).toContain('theSourceOfTheProblems');
97    expect(error.message).toContain('ThisCommand.DoesNotExist');
98
99    async function theSourceOfTheProblems() {
100      // @ts-expect-error This fails in TS as it knows that command does not
101      // exist but we want to have this tests for our users who consume in JS
102      // not TS.
103      await client.send('ThisCommand.DoesNotExist');
104    }
105  });
106
107  it('should expose the underlying connection', async () => {
108    const { page } = getTestState();
109
110    const client = await page.target().createCDPSession();
111    expect(client.connection()).toBeTruthy();
112  });
113});
114