Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to go to custom commands implementation in Cypress?

I'm using custom commands in cypress and it it working fine. I'm using visual studio code as editor. I was looking for how to let intelliSence to recognize custom commands and found that in https://github.com/cypress-io/cypress-example-todomvc#cypress-intellisense

I added cypress/index.d.ts file:

/// <reference types="cypress" />
declare namespace Cypress {
interface Chainable<Subject> {
  /**
   * Do something
   * @example
   * cy.doSomething()
   */
  doSomething(): Chainable<any>
}}

now when clicking on doSomething in the spec file it opens the declaration in index.d.ts, is there a way to let vscode open the actual command implementation under support/commands.js?

like image 367
Emad Aldalu Avatar asked Oct 20 '25 16:10

Emad Aldalu


1 Answers

To answer straight, opening/ peeking the direct declaration of custom commands is not supported (May be, some one can correct if this supports). I would usually follow grouping of custom commands in separate files. For instance,

File: cypress/support/sample_command.ts

/// <reference types="Cypress" />

import * as PageElements from "../resources/selectors.json";
import * as Pages from "../resources/urls.json";

let xml: XMLDocument
let data: HTMLCollection

Cypress.Commands.add(
  "getWorkflowXML",
  (wfPath: string): Cypress.Chainable<HTMLCollection> => {
    var url = Cypress.env("url") + wfPath;

    return cy.request({
      log: true,
      url: url,
      auth: {
        user: Cypress.env("userName"),
        pass: Cypress.env("password")
      }
    }).then(response => {
      expect(response)
        .property("status")
        .to.equal(200);
      xml = Cypress.$.parseXML(response.body);
      data = xml.getElementsByTagName("java");
      return data;
    });
  }
);

declare global {
  namespace Cypress {
    interface Chainable {
      /**
       * Get Workflow XML through XHR call
       * @memberof Cypress.Chainable
       * @param wfPath
       */
      getWorkflowXML: (wfPath: string) => Cypress.Chainable<HTMLCollection>;
    }
  }
}

Then, in the the cypress/support/index.js file I would import the command file,

import './sample_command'

This way gives me better traceability, instead of declaring all the commands straight under index.d.ts.

Reference:

  1. https://docs.cypress.io/api/cypress-api/custom-commands.html#Syntax
  2. https://github.com/cypress-io/add-cypress-custom-command-in-typescript
like image 142
Kondasamy Jayaraman Avatar answered Oct 23 '25 05:10

Kondasamy Jayaraman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!