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

Add types for cypress-cdp #66237

Merged
merged 1 commit into from Aug 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
32 changes: 32 additions & 0 deletions types/cypress-cdp/cypress-cdp-tests.ts
@@ -0,0 +1,32 @@
import 'cypress';
import 'cypress-cdp';

//#region CDP
// $ExpectType Chainable<void>
cy.CDP('Console.enable', {});

// $ExpectType Chainable<EnableResponse>
cy.CDP('Debugger.enable', {});

// @ts-expect-error
cy.CDP('Debugger.enable');

// @ts-expect-error
cy.CDP('Non-existent.command', {});
//#endregion

//#region getCDPNodeId
// $ExpectType Chainable<number>
cy.getCDPNodeId('body');

// @ts-expect-error
cy.getCDPNodeId(1);
//#endregion

//#region hasEventListeners
// $ExpectType Chainable<GetEventListenersResponse>
cy.hasEventListeners('button#one');

// @ts-expect-error
cy.hasEventListeners(1);
//#endregion
86 changes: 86 additions & 0 deletions types/cypress-cdp/index.d.ts
@@ -0,0 +1,86 @@
// Type definitions for cypress-cdp 1.3
// Project: https://github.com/bahmutov/cypress-cdp#readme
// Definitions by: Anthony Froissant <https://github.com/froissant>
// Louis Loiseau-Billon <https://github.com/LouisLoiseau>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped

/// <reference types="cypress" />

import type Protocol from 'devtools-protocol/types/protocol';
import type ProtocolMapping from 'devtools-protocol/types/protocol-mapping';

declare global {
// Augment the Cypress namespace to include type definitions for cypress-cdp.
namespace Cypress {
namespace CDP {
//#region CDP
type RdpCommands = ProtocolMapping.Commands;
type RdpCommandNames = keyof RdpCommands;

type CdpCommandFnParams<RdpCommandName extends RdpCommandNames> =
RdpCommands[RdpCommandName]['paramsType'][number] extends never
? Record<string, never>
: RdpCommands[RdpCommandName]['paramsType'][number];
type CdpCommandFnReturnType<RdpCommandName extends RdpCommandNames> =
RdpCommands[RdpCommandName]['returnType'];
interface CdpCommandFnOptions {
log: LogConfig;
}
type CdpCommandFn = <RdpCommandName extends RdpCommandNames>(
rdpCommand: RdpCommandName,
params: CdpCommandFnParams<RdpCommandName>,
options?: CdpCommandFnOptions,
) => Chainable<CdpCommandFnReturnType<RdpCommandName>>;
//#endregion

//#region getCDPNodeId
type getCDPNodeIdFn = (selector: string) => Chainable<number>;
//#endregion

//#region hasEventListeners
interface hasEventListenersFnOptions {
log: LogConfig;
type: string;
}
type hasEventListenersFn = (
selector: string,
options?: hasEventListenersFnOptions,
) => Chainable<Protocol.DOMDebugger.GetEventListenersResponse>;
//#endregion
}

interface Chainable<Subject = any> {
/**
* Custom command to send a RDP command to Chrome DevTools.
* @example
* const selector = 'button#one'
* cy.CDP('Runtime.evaluate', {
* expression: 'frames[0].document.querySelector("' + selector + '")',
* }).should((v) => {
* expect(v.result).to.have.property('objectId')
* })
*/
CDP: CDP.CdpCommandFn;

/**
* 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.
* @example
* cy.getCDPNodeId('body').then((nodeId) => {
* cy.CDP('CSS.getPlatformFontsForNode', {
* nodeId
* });
* });
*/
getCDPNodeId: CDP.getCDPNodeIdFn;

/**
* Custom command to get listeners attached to a DOM element.
* @example
* cy.hasEventListeners('button#one')
* @example
* cy.hasEventListeners('button#one', { type: 'click' })
*/
hasEventListeners: CDP.hasEventListenersFn;
}
}
}
7 changes: 7 additions & 0 deletions types/cypress-cdp/package.json
@@ -0,0 +1,7 @@
{
"private": true,
"dependencies": {
"cypress": "9.7.0",
"devtools-protocol": "0.0.1173815"
froissant marked this conversation as resolved.
Show resolved Hide resolved
}
}
24 changes: 24 additions & 0 deletions types/cypress-cdp/tsconfig.json
@@ -0,0 +1,24 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": [
"es6",
"DOM"
],
"noImplicitAny": true,
"noImplicitThis": true,
"strictFunctionTypes": true,
"strictNullChecks": true,
"baseUrl": "../",
"typeRoots": [
"../"
],
"types": [],
"noEmit": true,
"forceConsistentCasingInFileNames": true
},
"files": [
"index.d.ts",
"cypress-cdp-tests.ts"
]
}
1 change: 1 addition & 0 deletions types/cypress-cdp/tslint.json
@@ -0,0 +1 @@
{ "extends": "@definitelytyped/dtslint/dt.json" }