Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add method which return current cdp #8159

Merged
merged 3 commits into from
Apr 1, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
GetCurrentCDPSessionCommand,
SwitchToWindowCommand,
SwitchToWindowByPredicateCommand,
SwitchToParentWindowCommand,
Expand Down Expand Up @@ -518,6 +519,13 @@ export default class TestController {
return this.enqueueCommand(GetCurrentWindowCommand);
}

[delegatedAPI(GetCurrentCDPSessionCommand.methodName)] () {
const callsite = getCallsiteForMethod(GetCurrentCDPSessionCommand.methodName);
const command = this._createCommand(GetCurrentCDPSessionCommand, {}, 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 getCurrentCDPSession (): Promise<remoteChrome.ProtocolApi | null> {
return this.provider.getCurrentCDPSession(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 getCurrentCDPSession (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.getCurrentCDPSession(browserId);

await navigateTo(cdpClient, url);
},

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

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

async dispatchNativeAutomationEventSequence (browserId, eventSequence) {
const cdpClient = await this._getActiveCDPClient(browserId);
const cdpClient = await this.getCurrentCDPSession(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 getCurrentCDPSession (browserId: string): Promise<remoteChrome.ProtocolApi | null> {
return this.plugin.getCurrentCDPSession(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 getCurrentCDPSession (/*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 GetCurrentCDPSessionCommand 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 GetCurrentCDPSessionCommand extends ActionCommandBase {
static methodName = camelCase(TYPE.getCurrentCDPSession);

constructor (obj, testRun, validateProperties) {
super(obj, testRun, TYPE.getCurrentCDPSession, 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',
getCurrentCDPSession: 'get-current-c-d-p-session',
Aleksey28 marked this conversation as resolved.
Show resolved Hide resolved
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 getCurrentCDPSession (): Promise<remoteChrome.ProtocolApi | null> {
return this.browserConnection.getCurrentCDPSession();
}

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.getCurrentCDPSession)
return this.getCurrentCDPSession();

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 session', function () {
onlyInNativeAutomation('Should return current CPD', function () {
return runTests('./testcafe-fixtures/index.js', 'Get current CDP session', { experimentalMultipleWindows: true });
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@

fixture `Get current CDP session`
.page `http://localhost:3000/fixtures/api/es-next/get-current-cdp-session/pages/index.html`;

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

let clientCDP = await t.getCurrentCDPSession();

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

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

const childWindowId = await t.testRun.activeWindowId;

clientCDP = await t.getCurrentCDPSession();

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: 'getCurrentCDPSession',
command: {
type: 'get-current-c-d-p-session',
actionId: 'GetCurrentCDPSessionCommand',
},
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: [],
getCurrentCDPSession: [],
switchToParentWindow: [],
switchToPreviousWindow: [],
setNativeDialogHandler: [() => true],
Expand Down
10 changes: 10 additions & 0 deletions ts-defs-src/test-api/test-controller.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ interface Browser {

type WindowDescriptor = unknown;

type CDPSession = unknown;

interface WindowFilterData {
/**
* The window title.
Expand Down Expand Up @@ -413,6 +415,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.
PavelMor25 marked this conversation as resolved.
Show resolved Hide resolved
*/
getCurrentCDPSession(): CDPSessionPromise;
PavelMor25 marked this conversation as resolved.
Show resolved Hide resolved

/**
* Activates the window that corresponds to the `window` object.
Expand Down Expand Up @@ -576,3 +583,6 @@ interface TestControllerPromise<T=any> extends TestController, Promise<T> {
interface WindowDescriptorPromise extends TestControllerPromise<WindowDescriptor> {
}

interface CDPSessionPromise extends TestControllerPromise<CDPSession> {
}

Loading