How to write chain command with parameter in Cypress?

I'm trying to find an expander with its caption and click it.

When I'm doing it, it works:

cy.get('[caption="Radio Button"] > .expander-container').click();

I want to make this one as command.

Cypress.Commands.add('findExpanderByName', (Caption: string) => {
    cy.get('[caption=Caption] > .expander-container').click();
    cy.wait(2000);
});

And use it like this

cy.findExpanderByName("Radio Button");

However, it does not work cuz it Caption is not used as a variable.

How to get Caption variable value in command ?


You can do something like this:

Cypress.Commands.add('findExpanderByName', (Caption: string) => {
    cy.get(`[caption=${Caption}] > .expander-container`).click();
    cy.wait(2000);
});