| | |

Cypress 08 ๐ŸŒฒ DOM Interactions โ€” click, type, select, and trigger

โฑ๏ธ Estimated read: 10 min | |

Cypress’s interaction commands are actions on the chain. click, type, select, and trigger each receive the subject (the element from the previous command), perform the action, and pass the subject to the next command. Every action is preceded by an actionability checkโ€”the element must exist, be visible, and be enabledโ€”and the command retries until the check passes. The event sequences the commands produce are the same sequences a real user would, with the exception of trigger, which dispatches a synthetic event without the browser’s default behavior.

This chapter covers the four interaction commands, the actionability checks that gate them, and the event sequences each one produces. The sections that follow walk through clicking and typing, selecting and the trigger escape hatch, and the .should() assertion that follows the interaction.

Key point: click, type, and select run the actionability checks and produce native event sequences; trigger dispatches a synthetic event without the default browser behavior and skips most of the checks, so it is the escape hatch for a custom event that the native commands do not produce.


a. click and the actionability checks

cy.get(selector).click() clicks the element. Before the click, Cypress checks that the element exists, is visible, is not disabled, is not covered by another element, and is not detached. If any check fails, the command retries until the check passes or the timeout expires.

// Basic click
cy.get('[data-cy="submit"]').click();

// Click with a modifier
cy.get('[data-cy="item"]').click({ ctrlKey: true });

// Right click
cy.get('[data-cy="menu"]').rightclick();

// Double click
cy.get('[data-cy="file"]').dblclick();

// Force: skip the checks
cy.get('[data-cy="hidden"]').click({ force: true });

The force: true option is the escape hatch for an element that is covered or hidden but must be clicked. It skips the visibility, coverage, and detachment checks, and it should be the exception. A test that uses force to click a button behind an overlay is a test that hides the overlay bug rather than catching it.

The position option clicks at a specific point inside the element, and the multiple: true option clicks every matched element. The scrollBehavior option controls whether the element is scrolled into view before the click; the default is "top", and false disables the scroll.


b. type and the keyboard events

cy.get(selector).type(text) focuses the element and types the text one character at a time, dispatching a keydown, keypress, keyup, and input event for each character. The command waits for the element to be focusable and not disabled before it types.

// Type into an input
cy.get('[data-cy="email"]').type('user@example.com');

// Type with a delay between characters
cy.get('[data-cy="search"]').type('playwright', { delay: 100 });

// Special keys
cy.get('[data-cy="search"]').type('{enter}');
cy.get('[data-cy="input"]').type('{selectall}{backspace}');

// Clear first, then type
cy.get('[data-cy="email"]').clear().type('new@example.com');

// Type without focusing
cy.get('[data-cy="email"]').type('hello', { force: true });

The special key sequences are the way to send the keys that are not characters: {enter}, {tab}, {esc}, {backspace}, {del}, {selectall}, {uparrow}, {downarrow}, {leftarrow}, {rightarrow}, {home}, {end}, {pageup}, {pagedown}. The {selectall} sequence selects the input’s content, and {backspace} or {del} clears it. The .clear() command is the simpler way to clear an input.

The .type() command does not work on every input type. A <select> is the select command’s job, a date input needs a value in the browser’s expected format, and a contenteditable element needs the type to focus it first. The force: true option bypasses the focus check but not the input’s own behavior.


c. select and the trigger escape hatch

cy.get(selector).select(value) selects an option in a <select>. The argument can be the option’s value, its text, its index, or an array for a multi-select. The command fires the change event that the application listens for.

// By value
cy.get('[data-cy="country"]').select('us');

// By text
cy.get('[data-cy="country"]').select('United States');

// By index
cy.get('[data-cy="country"]').select(1);

// Multiple
cy.get('[data-cy="tags"]').select(['a', 'b']);

The trigger command dispatches an event without the browser’s default behavior. It is the command for a custom event that the native commands do not produceโ€”a mousedown on a drag handle, a drop on a drop zone, a scroll on a container.

// Custom event
cy.get('[data-cy="dropzone"]').trigger('drop');

// With event properties
cy.get('[data-cy="item"]').trigger('mousedown', { which: 1, pageX: 100, pageY: 100 });

// Mouseover
cy.get('[data-cy="menu"]').trigger('mouseover');

The trigger skips the actionability checks by default, which is why it is the escape hatch and not the default. It does not scroll the element into view, it does not check the visibility, and it dispatches the event whether or not the browser would have produced it. A test that uses trigger('click') instead of click() is testing a synthetic event, not a user click, and the difference is the focus, the coordinates, and the default action.

The .should() assertion after the interaction is the verification. The interaction performs the action, and the assertion checks the result with retry.

cy.get('[data-cy="email"]').type('a@b.com').should('have.value', 'a@b.com');
cy.get('[data-cy="country"]').select('us').should('have.value', 'us');
cy.get('[data-cy="submit"]').click();
cy.get('[data-cy="status"]').should('contain', 'Saved');

Complete Example Session

// ============================================
// PART 1: click
// ============================================
describe("click", () => {
  it("clicks the submit button", () => {
    cy.visit("/form");
    cy.get('[data-cy="email"]').type('user@example.com');
    cy.get('[data-cy="submit"]').click();
    cy.get('[data-cy="status"]').should('contain', 'Saved');
  });
});

// ============================================
// PART 2: type
// ============================================
describe("type", () => {
  it("types and clears", () => {
    cy.visit("/form");
    cy.get('[data-cy="search"]').type('playwright{enter}');
    cy.get('[data-cy="search"]').clear().type('vitest');
    cy.get('[data-cy="search"]').should('have.value', 'vitest');
  });
});

// ============================================
// PART 3: select
// ============================================
describe("select", () => {
  it("selects a country", () => {
    cy.visit("/form");
    cy.get('[data-cy="country"]').select('us');
    cy.get('[data-cy="country"]').should('have.value', 'us');
  });
});

// ============================================
// PART 4: trigger
// ============================================
describe("trigger", () => {
  it("dispatches a custom event", () => {
    cy.visit("/drag");
    cy.get('[data-cy="item"]').trigger('mousedown', { which: 1 });
    cy.get('[data-cy="dropzone"]').trigger('drop');
    cy.get('[data-cy="dropzone"]').should('contain', 'item');
  });
});

The parts covered the click, the type with special keys, the select, and the custom trigger.


Quick Reference

Interaction Commands

CommandActionChecks
.click()ClickVisibility, coverage, enabled
.dblclick()Double clickSame
.rightclick()Right clickSame
.type(text)Keystroke per charFocusable, enabled
.clear()Clear inputFocusable
.select(value)Select optionEnabled
.trigger(event)Dispatch eventSkipped

Special Keys

SequenceKey
{enter}Enter
{tab}Tab
{esc}Escape
{backspace}Backspace
{selectall}Select all
{uparrow}Up arrow

Force Option

UseEffect
click({ force: true })Skip visibility/coverage
type(..., { force: true })Skip focus check

Best Practices

โœ… Do This:

// Use click and type
cy.get('[data-cy="submit"]').click();                          // โœ…

// Use select for a select element
cy.get('[data-cy="country"]').select('us');                    // โœ…

// Assert after the interaction
cy.get('[data-cy="email"]').should('have.value', 'a@b.com');   // โœ…

โŒ Don’t Do This:

// Use trigger for a normal click
cy.get('[data-cy="submit"]').trigger('click');                 // โŒ

// Use force to click a covered element
cy.get('[data-cy="submit"]').click({ force: true });           // โŒ

// Type into a select
cy.get('select').type('us');                                   // โŒ

Common Pitfalls

PitfallWhy It HappensFix
Click does nothingElement covered or disabledFix the overlay or the state
Select does not fire changeWrong value typeUse the option’s value
trigger does not behave like a clickSynthetic eventUse .click()
Type does not clearNo .clear().clear().type()

Real-World Examples

1. Click

cy.get('[data-cy="submit"]').click();

2. Type

cy.get('[data-cy="email"]').type('a@b.com');

3. Type with Enter

cy.get('[data-cy="search"]').type('query{enter}');

4. Clear

cy.get('[data-cy="email"]').clear();

5. Select by value

cy.get('[data-cy="country"]').select('us');

6. Select by text

cy.get('[data-cy="country"]').select('United States');

7. Trigger custom

cy.get('[data-cy="dropzone"]').trigger('drop');

8. Click with modifier

cy.get('[data-cy="item"]').click({ ctrlKey: true });

9. Assert after type

cy.get('[data-cy="email"]').should('have.value', 'a@b.com');

10. Scroll into view

cy.get('[data-cy="footer"]').scrollIntoView().click();

Visual

Event Sequence

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  .click()                                               โ”‚
โ”‚    mousedown โ†’ mouseup โ†’ click                          โ”‚
โ”‚                                                         โ”‚
โ”‚  .type('ab')                                            โ”‚
โ”‚    keydown a โ†’ keypress a โ†’ input a โ†’ keyup a           โ”‚
โ”‚    keydown b โ†’ keypress b โ†’ input b โ†’ keyup b           โ”‚
โ”‚                                                         โ”‚
โ”‚  .select('us')                                          โ”‚
โ”‚    change                                               โ”‚
โ”‚                                                         โ”‚
โ”‚  .trigger('drop')                                       โ”‚
โ”‚    drop (only)                                          โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Actionability Checks

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  click                                                  โ”‚
โ”‚         โ”‚                                               โ”‚
โ”‚         โ–ผ                                               โ”‚
โ”‚  exists? โ”€โ”€noโ”€โ”€โ–บ retry                                  โ”‚
โ”‚         โ”‚yes                                            โ”‚
โ”‚         โ–ผ                                               โ”‚
โ”‚  visible? โ”€โ”€noโ”€โ”€โ–บ retry                                 โ”‚
โ”‚         โ”‚yes                                            โ”‚
โ”‚         โ–ผ                                               โ”‚
โ”‚  covered? โ”€โ”€yesโ”€โ”€โ–บ retry                                โ”‚
โ”‚         โ”‚no                                             โ”‚
โ”‚         โ–ผ                                               โ”‚
โ”‚  disabled? โ”€โ”€yesโ”€โ”€โ–บ retry                               โ”‚
โ”‚         โ”‚no                                             โ”‚
โ”‚         โ–ผ                                               โ”‚
โ”‚  click                                                  โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Force and Trigger

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  click({ force: true })                                 โ”‚
โ”‚    โ†’ skips visibility and coverage checks               โ”‚
โ”‚    โ†’ still a real click event                           โ”‚
โ”‚                                                         โ”‚
โ”‚  trigger('click')                                       โ”‚
โ”‚    โ†’ synthetic event                                    โ”‚
โ”‚    โ†’ no default behavior, no focus, no coordinates      โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Select by Value vs Text

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  <select>                                               โ”‚
โ”‚    <option value="us">United States</option>            โ”‚
โ”‚                                                         โ”‚
โ”‚  select('us')            โ†’ by value                     โ”‚
โ”‚  select('United States') โ†’ by text                      โ”‚
โ”‚  select(0)               โ†’ by index                     โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Summary

ItemValue
Click.click()
Type.type(text)
Clear.clear()
Select.select(value | text | index)
Custom event.trigger(event)
Force{ force: true } skips checks
Special keys{enter}, {tab}, {selectall}

Key takeaways:

  • Every native command runs the actionability checks. The element must exist, be visible, not be covered, and be enabled before the command performs the action. The command retries until the checks pass.
  • click and type produce native event sequences. The click dispatches the mouse events, and the type dispatches a key sequence per character. The application’s handler receives the same events a real user would produce.
  • select fires the change event. The argument can be the value, the text, or the index, and the command waits for the element to be enabled before selecting.
  • trigger is the escape hatch. It dispatches a synthetic event without the browser’s default behavior and skips the actionability checks. Use it for a custom event the native commands do not produce, not for a normal click.
  • force: true skips the checks but keeps the real event. It is for an element that is covered or hidden but must be clicked, and it should be the exception because the check it skips is the bug it would catch.

Remember: The interaction commands are the user’s actions. click is a mouse click, type is the keystrokes, select is the dropdown choice, and trigger is the synthetic event that is not a user action. Use the native commands for the native interactions, the trigger for the custom events, and the assertion after the interaction to verify the result. The checks are what make the command wait for the page to be ready; bypassing them with force or trigger is the decision to test something other than what the user does.

Was this helpful?


Stop using slow, ad-bloated tool sites! ๐Ÿคฎ

๐Ÿ”Ž Search “KandZ Tools” on Google to use many professional utilities for free.

KandZ.me is the ultimate minimalist hub for:
โœ… Finance (Mortgage, Interest, Inflation)
โœ… Tech (Base64, JSON, Dev Suite, IP)
โœ… Health (BMI, BMR, TDEE)
โœ… Productivity (Timer, Workspace, QR)

โšก๏ธ Fast & Private
๐Ÿ”’ No data leaves your device
๐Ÿ’Ž 100% Free

๐Ÿ”— Use it now: https://tools.kandz.me
๐Ÿ”– Bookmark itโ€”youโ€™ll need it later!