From 905585f80d123069b4e3b81c0efda561ebaa98c5 Mon Sep 17 00:00:00 2001 From: Mark Date: Sun, 9 Jul 2023 13:44:31 -0400 Subject: [PATCH] feat(actions): add "When I make a request" https://docs.cypress.io/api/commands/request --- src/actions/index.ts | 1 + src/actions/request.ts | 79 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 80 insertions(+) create mode 100644 src/actions/request.ts diff --git a/src/actions/index.ts b/src/actions/index.ts index bdb8c9fb9..8b9c35215 100644 --- a/src/actions/index.ts +++ b/src/actions/index.ts @@ -12,6 +12,7 @@ export * from './local-storage'; export * from './log'; export * from './pause'; export * from './reload'; +export * from './request'; export * from './right-click'; export * from './screenshot'; export * from './scroll'; diff --git a/src/actions/request.ts b/src/actions/request.ts new file mode 100644 index 000000000..e2a6087f7 --- /dev/null +++ b/src/actions/request.ts @@ -0,0 +1,79 @@ +import { DataTable, When } from '@badeball/cypress-cucumber-preprocessor'; + +import { getOptions, setCypressElement } from '../utils'; + +/** + * When I make an HTTP [request](https://docs.cypress.io/api/commands/request): + * + * ```gherkin + * When I make a {string} request to {string} + * ``` + * + * @example + * + * Make a `GET` request to `http://dev.local/seed`: + * + * ```gherkin + * When I make a "GET" request to "http://dev.local/seed" + * ``` + * + * Make an `OPTIONS` request to `/api`: + * + * ```gherkin + * When I make an "OPTIONS" request to "/api" + * ``` + * + * @remarks + * + * If you make a request after visiting a page, Cypress assumes the URL used for the visit is the host: + * + * ```gherkin + * When I visit "http://localhost:8080/app" + * And I make a "POST" request to "users/1.json" + * # URL is http://localhost:8080/users/1.json + * ``` + * + * If you make a request prior to visiting a page, Cypress assumes the host is the `baseUrl` property configured inside of of your [configuration file](https://docs.cypress.io/guides/references/configuration#e2e). + */ +export function When_I_make_a_request( + method: + | 'GET' + | 'POST' + | 'PUT' + | 'DELETE' + | 'PATCH' + | 'HEAD' + | 'OPTIONS' + | 'TRACE' + | 'COPY' + | 'LOCK' + | 'MKCOL' + | 'MOVE' + | 'PURGE' + | 'PROPFIND' + | 'PROPPATCH' + | 'UNLOCK' + | 'REPORT' + | 'MKACTIVITY' + | 'CHECKOUT' + | 'MERGE' + | 'M-SEARCH' + | 'NOTIFY' + | 'SUBSCRIBE' + | 'UNSUBSCRIBE' + | 'SEARCH' + | 'CONNECT', + url: string, + options?: DataTable +) { + setCypressElement( + cy.request({ + ...getOptions(options), + method, + url, + }) + ); +} + +When('I make a {string} request to {string}', When_I_make_a_request); +When('I make an {string} request to {string}', When_I_make_a_request);