Skip to content

Commit fe4fedd

Browse files
committed
add method which return current cdp
1 parent 4a30f1c commit fe4fedd

File tree

14 files changed

+98
-4
lines changed

14 files changed

+98
-4
lines changed

src/api/test-controller/index.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import {
3535
OpenWindowCommand,
3636
CloseWindowCommand,
3737
GetCurrentWindowCommand,
38+
GetCurrentCDPClientCommand,
3839
SwitchToWindowCommand,
3940
SwitchToWindowByPredicateCommand,
4041
SwitchToParentWindowCommand,
@@ -518,6 +519,13 @@ export default class TestController {
518519
return this.enqueueCommand(GetCurrentWindowCommand);
519520
}
520521

522+
[delegatedAPI(GetCurrentCDPClientCommand.methodName)] () {
523+
const callsite = getCallsiteForMethod(GetCurrentCDPClientCommand.methodName);
524+
const command = this._createCommand(GetCurrentCDPClientCommand, {}, callsite);
525+
526+
return this.testRun.executeCommand(command, callsite);
527+
}
528+
521529
[delegatedAPI(SwitchToWindowCommand.methodName)] (windowSelector) {
522530
this._validateMultipleWindowCommand(SwitchToWindowCommand.methodName);
523531

src/browser/connection/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import { Proxy } from 'testcafe-hammerhead';
3636
import { NextTestRunInfo, OpenBrowserAdditionalOptions } from '../../shared/types';
3737
import { EventType } from '../../native-automation/types';
3838
import { NativeAutomationBase } from '../../native-automation';
39+
import remoteChrome from 'chrome-remote-interface';
3940

4041
const getBrowserConnectionDebugScope = (id: string): string => `testcafe:browser:connection:${id}`;
4142

@@ -660,6 +661,10 @@ export default class BrowserConnection extends EventEmitter {
660661
return this.provider.getNewWindowIdInNativeAutomation(this.id, windowId);
661662
}
662663

664+
public async getCurrentCDPClient (): Promise<remoteChrome.ProtocolApi | null> {
665+
return this.provider.getCurrentCDPClient(this.id);
666+
}
667+
663668
public resetActiveWindowId (): void {
664669
this.provider.resetActiveWindowId(this.id);
665670
}

src/browser/provider/built-in/dedicated/chrome/index.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export default {
2525
return getConfig(name);
2626
},
2727

28-
async _getActiveCDPClient (browserId) {
28+
async getCurrentCDPClient (browserId) {
2929
const { browserClient } = this.openedBrowsers[browserId];
3030
const cdpClient = await browserClient.getActiveClient();
3131

@@ -187,19 +187,19 @@ export default {
187187
},
188188

189189
async openFileProtocol (browserId, url) {
190-
const cdpClient = await this._getActiveCDPClient(browserId);
190+
const cdpClient = await this.getCurrentCDPClient(browserId);
191191

192192
await navigateTo(cdpClient, url);
193193
},
194194

195195
async dispatchNativeAutomationEvent (browserId, type, options) {
196-
const cdpClient = await this._getActiveCDPClient(browserId);
196+
const cdpClient = await this.getCurrentCDPClient(browserId);
197197

198198
await dispatchNativeAutomationEvent(cdpClient, type, options);
199199
},
200200

201201
async dispatchNativeAutomationEventSequence (browserId, eventSequence) {
202-
const cdpClient = await this._getActiveCDPClient(browserId);
202+
const cdpClient = await this.getCurrentCDPClient(browserId);
203203

204204
for (const event of eventSequence) {
205205
if (event.type === EventType.Delay)

src/browser/provider/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import getLocalOSInfo, { OSInfo } from 'get-os-info';
1717
import { OpenBrowserAdditionalOptions } from '../../shared/types';
1818
import { EventType } from '../../native-automation/types';
1919
import { NativeAutomationBase } from '../../native-automation';
20+
import remoteChrome from 'chrome-remote-interface';
2021

2122

2223
const DEBUG_LOGGER = debug('testcafe:browser:provider');
@@ -479,6 +480,10 @@ export default class BrowserProvider {
479480
return this.plugin.getNativeAutomation(browserId);
480481
}
481482

483+
public async getCurrentCDPClient (browserId: string): Promise<remoteChrome.ProtocolApi | null> {
484+
return this.plugin.getCurrentCDPClient(browserId);
485+
}
486+
482487
public getNewWindowIdInNativeAutomation (browserId: string, windowId: string): Promise<void> {
483488
return this.plugin.getNewWindowIdInNativeAutomation(browserId, windowId);
484489
}

src/browser/provider/plugin-host.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,4 +178,8 @@ export default class BrowserProviderPluginHost {
178178
getNewWindowIdInNativeAutomation (/*browserId, windowId*/) {
179179
return Promise.resolve();
180180
}
181+
182+
async getCurrentCDPClient (/*browserId*/) {
183+
return Promise.resolve();
184+
}
181185
}

src/test-run/commands/actions.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,10 @@ export class GetCurrentWindowsCommand extends ActionCommandBase {
6767
public constructor(obj: object, testRun: TestRun, validateProperties?: boolean);
6868
}
6969

70+
export class GetCurrentCDPClientCommand extends ActionCommandBase {
71+
public constructor(obj: object, testRun: TestRun, validateProperties?: boolean);
72+
}
73+
7074
export class SwitchToWindowCommand extends ActionCommandBase {
7175
public constructor(obj: object, testRun: TestRun, validateProperties?: boolean);
7276
public windowId: string;

src/test-run/commands/actions.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -524,6 +524,14 @@ export class GetCurrentWindowsCommand extends ActionCommandBase {
524524
}
525525
}
526526

527+
export class GetCurrentCDPClientCommand extends ActionCommandBase {
528+
static methodName = camelCase(TYPE.getCurrentCDPClient);
529+
530+
constructor (obj, testRun, validateProperties) {
531+
super(obj, testRun, TYPE.getCurrentCDPClient, validateProperties);
532+
}
533+
}
534+
527535
export class SwitchToWindowCommand extends ActionCommandBase {
528536
static methodName = camelCase(TYPE.switchToWindow);
529537

src/test-run/commands/type.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ export default {
4141
closeWindow: 'close-window',
4242
getCurrentWindow: 'get-current-window',
4343
getCurrentWindows: 'get-current-windows',
44+
getCurrentCDPClient: 'get-current-c-d-p-client',
4445
switchToWindow: 'switch-to-window',
4546
switchToWindowByPredicate: 'switch-to-window-by-predicate',
4647
switchToParentWindow: 'switch-to-parent-window',

src/test-run/index.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ import {
136136
import NativeAutomationRequestPipeline from '../native-automation/request-pipeline';
137137
import { NativeAutomationBase } from '../native-automation';
138138
import ReportDataLog from '../reporter/report-data-log';
139+
import remoteChrome from 'chrome-remote-interface';
139140

140141
const lazyRequire = require('import-lazy')(require);
141142
const ClientFunctionBuilder = lazyRequire('../client-functions/client-function-builder');
@@ -445,6 +446,10 @@ export default class TestRun extends AsyncEventEmitter {
445446
: this.testExecutionTimeout || null;
446447
}
447448

449+
public async getCurrentCDPClient (): Promise<remoteChrome.ProtocolApi | null> {
450+
return this.browserConnection.getCurrentCDPClient();
451+
}
452+
448453
private _addClientScriptContentWarningsIfNecessary (): void {
449454
const { empty, duplicatedContent } = findProblematicScripts(this.test.clientScripts as ClientScript[]);
450455

@@ -1227,6 +1232,9 @@ export default class TestRun extends AsyncEventEmitter {
12271232
if (command.type === COMMAND_TYPE.removeRequestHooks)
12281233
return Promise.all((command as RemoveRequestHooksCommand).hooks.map(hook => this._removeRequestHook(hook)));
12291234

1235+
if (command.type === COMMAND_TYPE.getCurrentCDPClient)
1236+
return this.getCurrentCDPClient();
1237+
12301238
return this._enqueueCommand(command, callsite as CallsiteRecord);
12311239
}
12321240

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>Title</title>
6+
</head>
7+
<body>
8+
<h1>new window</h1>
9+
</body>
10+
</html>

0 commit comments

Comments
 (0)