0
0
Cypresstesting~5 mins

Dual commands in Cypress

Choose your learning style9 modes available
Introduction

Dual commands let you do two things in one step, like finding an element and then clicking it. This saves time and makes tests easier to read.

When you want to find a button and click it in one step.
When you need to get a text field and type into it quickly.
When checking if an element exists and then interacting with it.
When you want to chain actions to keep tests simple and clear.
Syntax
Cypress
cy.get('selector').command()

cy.get('selector') finds the element on the page.

.command() is an action like click(), type(), or should().

Examples
This finds the button with id 'submit-button' and clicks it.
Cypress
cy.get('#submit-button').click()
This finds the input box with class 'input-email' and types an email address.
Cypress
cy.get('.input-email').type('hello@example.com')
This finds the first <h1> heading and checks if it contains the text 'Welcome'.
Cypress
cy.get('h1').should('contain', 'Welcome')
Sample Program

This test opens a page, finds a link by its href, clicks it, and then checks if the URL changed correctly.

Cypress
describe('Dual commands example', () => {
  it('Finds and clicks a button', () => {
    cy.visit('https://example.cypress.io')
    cy.get('a[href="/commands/actions"]').click()
    cy.url().should('include', '/commands/actions')
  })
})
OutputSuccess
Important Notes

Always use clear and unique selectors to avoid mistakes.

Chaining commands keeps tests short and easy to understand.

Commands like click() or type() only work after get() finds the element.

Summary

Dual commands combine finding and acting on elements in one step.

They make tests shorter and easier to read.

Use them to click, type, or check elements quickly.