Skip to content

Commit 0bc93f3

Browse files
committed
[cypress-cdp] add typings
1 parent 27c9274 commit 0bc93f3

5 files changed

Lines changed: 150 additions & 0 deletions

File tree

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import 'cypress';
2+
import 'cypress-cdp';
3+
4+
//#region CDP
5+
// $ExpectType Chainable<void>
6+
cy.CDP('Console.enable', {});
7+
8+
// $ExpectType Chainable<EnableResponse>
9+
cy.CDP('Debugger.enable', {});
10+
11+
// @ts-expect-error
12+
cy.CDP('Debugger.enable');
13+
14+
// @ts-expect-error
15+
cy.CDP('Non-existent.command', {});
16+
//#endregion
17+
18+
//#region getCDPNodeId
19+
// $ExpectType Chainable<number>
20+
cy.getCDPNodeId('body');
21+
22+
// @ts-expect-error
23+
cy.getCDPNodeId(1);
24+
//#endregion
25+
26+
//#region hasEventListeners
27+
// $ExpectType Chainable<GetEventListenersResponse>
28+
cy.hasEventListeners('button#one');
29+
30+
// @ts-expect-error
31+
cy.hasEventListeners(1);
32+
//#endregion

types/cypress-cdp/index.d.ts

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
// Type definitions for cypress-cdp 1.3
2+
// Project: https://github.com/bahmutov/cypress-cdp#readme
3+
// Definitions by: Anthony Froissant <https://github.com/froissant>
4+
// Louis Loiseau-Billon <https://github.com/LouisLoiseau>
5+
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
6+
7+
/// <reference types="cypress" />
8+
9+
import type Protocol from 'devtools-protocol/types/protocol';
10+
import type ProtocolMapping from 'devtools-protocol/types/protocol-mapping';
11+
12+
declare global {
13+
// Augment the Cypress namespace to include type definitions for cypress-cdp.
14+
namespace Cypress {
15+
namespace CDP {
16+
//#region CDP
17+
type RdpCommands = ProtocolMapping.Commands;
18+
type RdpCommandNames = keyof RdpCommands;
19+
20+
type CdpCommandFnParams<RdpCommandName extends RdpCommandNames> =
21+
RdpCommands[RdpCommandName]['paramsType'][number] extends never
22+
? Record<string, never>
23+
: RdpCommands[RdpCommandName]['paramsType'][number];
24+
type CdpCommandFnReturnType<RdpCommandName extends RdpCommandNames> =
25+
RdpCommands[RdpCommandName]['returnType'];
26+
interface CdpCommandFnOptions {
27+
log: LogConfig;
28+
}
29+
type CdpCommandFn = <RdpCommandName extends RdpCommandNames>(
30+
rdpCommand: RdpCommandName,
31+
params: CdpCommandFnParams<RdpCommandName>,
32+
options?: CdpCommandFnOptions,
33+
) => Chainable<CdpCommandFnReturnType<RdpCommandName>>;
34+
//#endregion
35+
36+
//#region getCDPNodeId
37+
type getCDPNodeIdFn = (selector: string) => Chainable<number>;
38+
//#endregion
39+
40+
//#region hasEventListeners
41+
interface hasEventListenersFnOptions {
42+
log: LogConfig;
43+
type: string;
44+
}
45+
type hasEventListenersFn = (
46+
selector: string,
47+
options?: hasEventListenersFnOptions,
48+
) => Chainable<Protocol.DOMDebugger.GetEventListenersResponse>;
49+
//#endregion
50+
}
51+
52+
interface Chainable<Subject = any> {
53+
/**
54+
* Custom command to send a RDP command to Chrome DevTools.
55+
* @example
56+
* const selector = 'button#one'
57+
* cy.CDP('Runtime.evaluate', {
58+
* expression: 'frames[0].document.querySelector("' + selector + '")',
59+
* }).should((v) => {
60+
* expect(v.result).to.have.property('objectId')
61+
* })
62+
*/
63+
CDP: CDP.CdpCommandFn;
64+
65+
/**
66+
* Custom command to return the internal element's Node Id that can be used to query the run-time properties, for example the rendered font.
67+
* @example
68+
* cy.getCDPNodeId('body').then((nodeId) => {
69+
* cy.CDP('CSS.getPlatformFontsForNode', {
70+
* nodeId
71+
* });
72+
* });
73+
*/
74+
getCDPNodeId: CDP.getCDPNodeIdFn;
75+
76+
/**
77+
* Custom command to get listeners attached to a DOM element.
78+
* @example
79+
* cy.hasEventListeners('button#one')
80+
* @example
81+
* cy.hasEventListeners('button#one', { type: 'click' })
82+
*/
83+
hasEventListeners: CDP.hasEventListenersFn;
84+
}
85+
}
86+
}

types/cypress-cdp/package.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"private": true,
3+
"dependencies": {
4+
"cypress": "9.7.0",
5+
"devtools-protocol": "0.0.1173815"
6+
}
7+
}

types/cypress-cdp/tsconfig.json

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"compilerOptions": {
3+
"module": "commonjs",
4+
"lib": [
5+
"es6",
6+
"DOM"
7+
],
8+
"noImplicitAny": true,
9+
"noImplicitThis": true,
10+
"strictFunctionTypes": true,
11+
"strictNullChecks": true,
12+
"baseUrl": "../",
13+
"typeRoots": [
14+
"../"
15+
],
16+
"types": [],
17+
"noEmit": true,
18+
"forceConsistentCasingInFileNames": true
19+
},
20+
"files": [
21+
"index.d.ts",
22+
"cypress-cdp-tests.ts"
23+
]
24+
}

types/cypress-cdp/tslint.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{ "extends": "@definitelytyped/dtslint/dt.json" }

0 commit comments

Comments
 (0)