/// import { StringMatcher } from "cypress/types/net-stubbing"; import { CallStack, IMockResponse, IMockResponseOptions, InterceptorOptions, IRouteMatcher, IThrottleRequestOptions, OnRequestError, WaitUntilRequestOptions, WriteStatsOptions } from "./Interceptor.types"; import { RequestProxy } from "./src/RequestProxy"; declare global { namespace Cypress { interface Chainable { /** * Destroy the interceptor by restoring the original fetch and * XMLHttpRequest implementations. This command removes all proxy * functionality and restores the browser's native implementations. */ destroyInterceptor(): void; /** * Destroy all Interceptors registered inside IFRAMEs */ destroyInterceptorInsideIframe(): void; /** * Enable Interceptor inside an IFRAME * * @param selector A selector or an element */ enableInterceptorInsideIframe(selector: string | JQuery | JQuery | Chainable> | Chainable>): void; /** * Get an instance of the Interceptor * * @returns An instance of the Interceptor */ interceptor(): Chainable; /** * Get the last call matching the provided route matcher. * * @param routeMatcher A route matcher * @returns The last call information or `undefined` if none matches. */ interceptorLastRequest(routeMatcher?: IRouteMatcher): Chainable; /** * Set the Interceptor options. This must be called before a request occurs. * * @param options Options * @returns The current Interceptor options */ interceptorOptions(options?: InterceptorOptions): Chainable; /** * Get the number of requests matching the provided route matcher. * * @param routeMatcher A route matcher * @returns The number of requests matching the provided route matcher since the current test started. */ interceptorRequestCalls(routeMatcher?: IRouteMatcher): Chainable; /** * Get the statistics for all requests matching the provided route matcher since the beginning * of the current test. * * @param routeMatcher A route matcher * @returns It returns all requests matching the provided route matcher with detailed information. * If none match, it returns an empty array. */ interceptorStats(routeMatcher?: IRouteMatcher): Chainable; /** * Mock the response of requests matching the provided route matcher. By default, it mocks the * first matching request, and then the mock is removed. Set `times` in the options to change * how many times the matching requests should be mocked. * * @param routeMatcher A route matcher * @param mock The response mock * @param options The mock options * @returns The ID of the created mock. This is needed if you want to remove the mock manually. */ mockInterceptorResponse(routeMatcher: IRouteMatcher, mock: IMockResponse, options?: IMockResponseOptions): Chainable; /** * Recreate the Interceptor instance. */ recreateInterceptor(): void; /** * Reset the Interceptor's watch. It sets the pointer to the last call. Resetting the pointer * is necessary when you want to wait for certain requests. * * Example: On a site, there are multiple requests to api/getUser, but we want to wait for the * specific one that occurs after clicking a button. Since we cannot know which api/getUser call * to wait for, calling this method sets the exact point from which we want to check the next requests. */ resetInterceptorWatch: VoidFunction; /** * Start the time measurement (a helper function) * * @returns performance.now() when the code is executed */ startTiming(): Chainable; /** * Stop the time measurement (a helper function) * * @returns If `cy.startTiming` was called, it returns the time difference since startTiming was * called (in ms); otherwise, it returns `undefined`. */ stopTiming(): Chainable; /** * Throttle requests matching the provided route matcher by setting a delay. By default, it throttles * the first matching request, and then the throttle is removed. Set times in the options to change * how many times the matching requests should be throttled. * * @param routeMatcher A route matcher * @param delay The delay in ms * @param options The throttle options (which can include mocking the response). * @returns The ID of the created throttle. This is needed if you want to remove the throttle manually. */ throttleInterceptorRequest(routeMatcher: IRouteMatcher, delay: number, options?: IThrottleRequestOptions): Chainable; /** * The method will wait until all requests matching the provided route matcher are finished or until * the maximum waiting time (`timeout` in options) is reached. * * By default, there must be at least one match. Otherwise, it waits until a request matches the * provided route matcher or until the maximum waiting time is reached. This behavior can be changed * by setting `enforceCheck` to `false` in the options. * * @param action An action which should trigger a request * @param stringMatcherOrOptions A string matcher OR options with a route matcher * @param errorMessage An error message when the maximum waiting time is reached * @returns The result from the action */ waitUntilRequestIsDone(action: () => Cypress.Chainable, stringMatcherOrOptions?: StringMatcher | WaitUntilRequestOptions, errorMessage?: string): Chainable; /** * @param action An action which should trigger a request * @param stringMatcherOrOptions A string matcher OR options with a route matcher * @param errorMessage An error message when the maximum waiting time is reached * @returns The result from the action */ waitUntilRequestIsDone(action: () => T, stringMatcherOrOptions?: StringMatcher | WaitUntilRequestOptions, errorMessage?: string): Chainable; /** * @param stringMatcherOrOptions A string matcher OR options with a route matcher * @param errorMessage An error message when the maximum waiting time is reached * @returns An instance of the Interceptor */ waitUntilRequestIsDone(stringMatcherOrOptions?: StringMatcher | WaitUntilRequestOptions, errorMessage?: string): Chainable; /** * Write the logged requests' information (or those filtered by the provided route matcher) to a file * * @example cy.writeInterceptorStatsToLog("./out") => the output file will be "./out/[Description] It.stats.json" * @example cy.writeInterceptorStatsToLog("./out", { fileName: "file_name" }) => the output file will be "./out/file_name.stats.json" * @example cy.writeInterceptorStatsToLog("./out", { routeMatcher: { method: "GET" } }) => write only "GET" requests to the output file * @example cy.writeInterceptorStatsToLog("./out", { mapper: (callStack) => ({ type: callStack.type, url: callStack.url }) }) => map the output that will be written to the output file * * @param outputDir The path for the output folder * @param options Options */ writeInterceptorStatsToLog(outputDir: string, options?: WriteStatsOptions & Partial): Chainable; } } } export declare class Interceptor { private startTime; private _callStack; private _mock; private _mockId; private _onRequestError; private _options; private _skip; private _throttle; private _throttleId; private win; constructor(requestProxy: RequestProxy, startTime: number); get requestTimeoutByEnv(): any; /** * Return a copy of all logged requests since the Interceptor was created (the Interceptor is created in `beforeEach`). */ get callStack(): CallStack[]; private filterItemsByMatcher; private getMock; /** * Get the last call that matches the provided route matcher. * * @param routeMatcher A route matcher * @returns The last call information or `undefined` if none match */ getLastRequest(routeMatcher?: IRouteMatcher): CallStack | undefined; private getRuntimeString; /** * Get statistics for all requests matching the provided route matcher since the beginning of the current test. * * @param routeMatcher A route matcher * @returns All requests matching the provided route matcher with detailed information, * if none match, returns an empty array */ getStats(routeMatcher?: IRouteMatcher): CallStack[]; private getThrottle; /** * Get the number of requests matching the provided route matcher. * * @param routeMatcher A route matcher * @returns The number of requests matching the provided route matcher since the current test started */ requestCalls(routeMatcher?: IRouteMatcher): number; private isThereRequestPending; /** * Mock the response of requests matching the provided route matcher. By default, it mocks the * first matching request, and then the mock is removed. Set `times` in the options to change * how many times the matching requests should be mocked. * * @param routeMatcher A route matcher * @param mock The response mock * @param options The mock options * @returns The ID of the created mock. This is needed if you want to remove the mock manually. */ mockResponse(routeMatcher: IRouteMatcher, mock: IMockResponse, options?: IMockResponseOptions): number; /** * Function called when a request is cancelled or fails * * @param func A function called on error/cancel */ onRequestError(func: OnRequestError): void; /** * Remove the mock entry by ID * * @param id A unique id received from `mockResponse` or `cy.mockInterceptorResponse` */ removeMock(id: number): boolean; /** * Remove the throttle entry by ID * * @param id A unique id received from `throttleRequest` or `cy.throttleInterceptorRequest` */ removeThrottle(id: number): boolean; /** * Reset the Interceptor's watch. It sets the pointer to the last call. Resetting the pointer * is necessary when you want to wait for certain requests. * * Example: On a site, there are multiple requests to api/getUser, but we want to wait for the * specific one that occurs after clicking a button. Since we cannot know which api/getUser call * to wait for, calling this method sets the exact point from which we want to check the next requests. */ resetWatch(): void; /** * Set the Interceptor options. This must be called before a request occurs. * * @param options Options * @returns The current Interceptor options */ setOptions(options?: InterceptorOptions): InterceptorOptions; /** * Throttle requests matching the provided route matcher by setting a delay. By default, it throttles * the first matching request, and then the throttle is removed. Set times in the options to change * how many times the matching requests should be throttled. * * @param routeMatcher A route matcher * @param delay The delay in ms * @param options The throttle options (which can include mocking the response). * @returns The ID of the created throttle. This is needed if you want to remove the throttle manually. */ throttleRequest(routeMatcher: IRouteMatcher, delay: number, options?: IThrottleRequestOptions): number; /** * The method will wait until all requests matching the provided route matcher are finished or until * the maximum waiting time (`timeout` in options) is reached. * * By default, there must be at least one match. Otherwise, it waits until a request matches the * provided route matcher or until the maximum waiting time is reached. This behavior can be changed * by setting `enforceCheck` to `false` in the options. * * @param action An action which should trigger a request * @param stringMatcherOrOptions A string matcher OR options with a route matcher * @param errorMessage An error message when the maximum waiting time is reached * @returns The result from the action */ waitUntilRequestIsDone(action: () => Cypress.Chainable, stringMatcherOrOptions?: StringMatcher | WaitUntilRequestOptions, errorMessage?: string): Cypress.Chainable; /** * @param action An action which should trigger a request * @param stringMatcherOrOptions A string matcher OR options with a route matcher * @param errorMessage An error message when the maximum waiting time is reached * @returns The result from the action */ waitUntilRequestIsDone(action: () => T, stringMatcherOrOptions?: StringMatcher | WaitUntilRequestOptions, errorMessage?: string): Cypress.Chainable; /** * @param stringMatcherOrOptions A string matcher OR options with a route matcher * @param errorMessage An error message when the maximum waiting time is reached * @returns An instance of the Interceptor */ waitUntilRequestIsDone(stringMatcherOrOptions?: StringMatcher | WaitUntilRequestOptions, errorMessage?: string): Cypress.Chainable; private waitUntilRequestIsDone_withWait; /** * Write the logged requests' information (or those filtered by the provided route matcher) to a file * * @example writeStatsToLog("./out") => the output file will be "./out/Description - It.stats.json" * @example writeStatsToLog("./out", { fileName: "file_name" }) => the output file will be "./out/file_name.stats.json" * @example writeStatsToLog("./out", { routeMatcher: { method: "GET" } }) => write only "GET" requests to the output file * @example writeStatsToLog("./out", { mapper: (callStack) => ({ type: callStack.type, url: callStack.url }) }) => map the output that will be written to the output file * * @param outputDir The path for the output folder * @param options Options */ writeStatsToLog(outputDir: string, options?: WriteStatsOptions & Partial): Cypress.Chainable; }