Skip to content

Commit

Permalink
add method which return current cdp
Browse files Browse the repository at this point in the history
  • Loading branch information
PavelMor25 committed Mar 21, 2024
1 parent 4a30f1c commit 1c127b7
Show file tree
Hide file tree
Showing 16 changed files with 117 additions and 4 deletions.
8 changes: 8 additions & 0 deletions src/api/test-controller/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import {
OpenWindowCommand,
CloseWindowCommand,
GetCurrentWindowCommand,
GetCurrentCDPClientCommand,
SwitchToWindowCommand,
SwitchToWindowByPredicateCommand,
SwitchToParentWindowCommand,
Expand Down Expand Up @@ -518,6 +519,13 @@ export default class TestController {
return this.enqueueCommand(GetCurrentWindowCommand);
}

[delegatedAPI(GetCurrentCDPClientCommand.methodName)] () {
const callsite = getCallsiteForMethod(GetCurrentCDPClientCommand.methodName);
const command = this._createCommand(GetCurrentCDPClientCommand, {}, callsite);

return this.testRun.executeCommand(command, callsite);
}

[delegatedAPI(SwitchToWindowCommand.methodName)] (windowSelector) {
this._validateMultipleWindowCommand(SwitchToWindowCommand.methodName);

Expand Down
5 changes: 5 additions & 0 deletions src/browser/connection/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import { Proxy } from 'testcafe-hammerhead';
import { NextTestRunInfo, OpenBrowserAdditionalOptions } from '../../shared/types';
import { EventType } from '../../native-automation/types';
import { NativeAutomationBase } from '../../native-automation';
import remoteChrome from 'chrome-remote-interface';

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

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

public async getCurrentCDPClient (): Promise<remoteChrome.ProtocolApi | null> {
return this.provider.getCurrentCDPClient(this.id);
}

public resetActiveWindowId (): void {
this.provider.resetActiveWindowId(this.id);
}
Expand Down
8 changes: 4 additions & 4 deletions src/browser/provider/built-in/dedicated/chrome/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export default {
return getConfig(name);
},

async _getActiveCDPClient (browserId) {
async getCurrentCDPClient (browserId) {
const { browserClient } = this.openedBrowsers[browserId];
const cdpClient = await browserClient.getActiveClient();

Expand Down Expand Up @@ -187,19 +187,19 @@ export default {
},

async openFileProtocol (browserId, url) {
const cdpClient = await this._getActiveCDPClient(browserId);
const cdpClient = await this.getCurrentCDPClient(browserId);

await navigateTo(cdpClient, url);
},

async dispatchNativeAutomationEvent (browserId, type, options) {
const cdpClient = await this._getActiveCDPClient(browserId);
const cdpClient = await this.getCurrentCDPClient(browserId);

await dispatchNativeAutomationEvent(cdpClient, type, options);
},

async dispatchNativeAutomationEventSequence (browserId, eventSequence) {
const cdpClient = await this._getActiveCDPClient(browserId);
const cdpClient = await this.getCurrentCDPClient(browserId);

for (const event of eventSequence) {
if (event.type === EventType.Delay)
Expand Down
5 changes: 5 additions & 0 deletions src/browser/provider/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import getLocalOSInfo, { OSInfo } from 'get-os-info';
import { OpenBrowserAdditionalOptions } from '../../shared/types';
import { EventType } from '../../native-automation/types';
import { NativeAutomationBase } from '../../native-automation';
import remoteChrome from 'chrome-remote-interface';


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

public async getCurrentCDPClient (browserId: string): Promise<remoteChrome.ProtocolApi | null> {
return this.plugin.getCurrentCDPClient(browserId);
}

public getNewWindowIdInNativeAutomation (browserId: string, windowId: string): Promise<void> {
return this.plugin.getNewWindowIdInNativeAutomation(browserId, windowId);
}
Expand Down
4 changes: 4 additions & 0 deletions src/browser/provider/plugin-host.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,4 +178,8 @@ export default class BrowserProviderPluginHost {
getNewWindowIdInNativeAutomation (/*browserId, windowId*/) {
return Promise.resolve();
}

async getCurrentCDPClient (/*browserId*/) {
return Promise.resolve();
}
}
4 changes: 4 additions & 0 deletions src/test-run/commands/actions.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ export class GetCurrentWindowsCommand extends ActionCommandBase {
public constructor(obj: object, testRun: TestRun, validateProperties?: boolean);
}

export class GetCurrentCDPClientCommand extends ActionCommandBase {
public constructor(obj: object, testRun: TestRun, validateProperties?: boolean);
}

export class SwitchToWindowCommand extends ActionCommandBase {
public constructor(obj: object, testRun: TestRun, validateProperties?: boolean);
public windowId: string;
Expand Down
8 changes: 8 additions & 0 deletions src/test-run/commands/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,14 @@ export class GetCurrentWindowsCommand extends ActionCommandBase {
}
}

export class GetCurrentCDPClientCommand extends ActionCommandBase {
static methodName = camelCase(TYPE.getCurrentCDPClient);

constructor (obj, testRun, validateProperties) {
super(obj, testRun, TYPE.getCurrentCDPClient, validateProperties);
}
}

export class SwitchToWindowCommand extends ActionCommandBase {
static methodName = camelCase(TYPE.switchToWindow);

Expand Down
1 change: 1 addition & 0 deletions src/test-run/commands/type.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export default {
closeWindow: 'close-window',
getCurrentWindow: 'get-current-window',
getCurrentWindows: 'get-current-windows',
getCurrentCDPClient: 'get-current-c-d-p-client',
switchToWindow: 'switch-to-window',
switchToWindowByPredicate: 'switch-to-window-by-predicate',
switchToParentWindow: 'switch-to-parent-window',
Expand Down
8 changes: 8 additions & 0 deletions src/test-run/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ import {
import NativeAutomationRequestPipeline from '../native-automation/request-pipeline';
import { NativeAutomationBase } from '../native-automation';
import ReportDataLog from '../reporter/report-data-log';
import remoteChrome from 'chrome-remote-interface';

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

public async getCurrentCDPClient (): Promise<remoteChrome.ProtocolApi | null> {
return this.browserConnection.getCurrentCDPClient();
}

private _addClientScriptContentWarningsIfNecessary (): void {
const { empty, duplicatedContent } = findProblematicScripts(this.test.clientScripts as ClientScript[]);

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

if (command.type === COMMAND_TYPE.getCurrentCDPClient)
return this.getCurrentCDPClient();

return this._enqueueCommand(command, callsite as CallsiteRecord);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>new window</h1>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1><a href="child.html" target="_blank">Open new window</a></h1>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const { onlyInNativeAutomation } = require('../../../../utils/skip-in');

describe('[API] Get current CDP client', function () {
onlyInNativeAutomation('Should return current CPD', function () {
return runTests('./testcafe-fixtures/index.js', 'Get current CDP client', { experimentalMultipleWindows: true });
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@

fixture `Get current CDP client`
.page `http://localhost:3000/fixtures/regression/gh-479-private/pages/index.html`;

test(`Get current CDP client`, async t => {
const mainWindowId = await t.testRun.activeWindowId;

let clientCDP = await t.getCurrentCDPClient();

await t.expect(clientCDP.webSocketUrl).contains(mainWindowId);

await t.click('a').click('h1');

const childWindowId = await t.testRun.activeWindowId;

clientCDP = await t.getCurrentCDPClient();

await t.expect(clientCDP.webSocketUrl).contains(childWindowId);
});
18 changes: 18 additions & 0 deletions test/server/data/test-controller-reporter-expected/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -649,6 +649,24 @@ module.exports = {
},
browser: { alias: 'test-browser', headless: false },
},
{
testRunId: 'test-run-id',
name: 'getCurrentCDPClient',
command: {
type: 'get-current-c-d-p-client',
actionId: 'GetCurrentCDPClientCommand',
},
test: {
id: 'test-id',
name: 'test-name',
phase: 'initial',
},
fixture: {
id: 'fixture-id',
name: 'fixture-name',
},
browser: { alias: 'test-browser', headless: false },
},
{
testRunId: 'test-run-id',
name: 'switchToParentWindow',
Expand Down
1 change: 1 addition & 0 deletions test/server/test-controller-events-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ const actions = {
switchToWindow: [{ id: 'window-id' }],
closeWindow: [{ id: 'window-id' }],
getCurrentWindow: [],
getCurrentCDPClient: [],
switchToParentWindow: [],
switchToPreviousWindow: [],
setNativeDialogHandler: [() => true],
Expand Down
5 changes: 5 additions & 0 deletions ts-defs-src/test-api/test-controller.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,11 @@ interface TestController {
* Retrieves a `window` object that corresponds to the currently open window.
*/
getCurrentWindow(): WindowDescriptorPromise;

/**
* Retrieves a `Chrome DevTools Protocol` object that corresponds to the currently open window in native automation.
*/
getCurrentCDPClient(): TestControllerPromise;

/**
* Activates the window that corresponds to the `window` object.
Expand Down

0 comments on commit 1c127b7

Please sign in to comment.