Cypress 08 ๐ฒ DOM Interactions โ click, type, select, and trigger
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
| Command | Action | Checks |
|---|---|---|
.click() | Click | Visibility, coverage, enabled |
.dblclick() | Double click | Same |
.rightclick() | Right click | Same |
.type(text) | Keystroke per char | Focusable, enabled |
.clear() | Clear input | Focusable |
.select(value) | Select option | Enabled |
.trigger(event) | Dispatch event | Skipped |
Special Keys
| Sequence | Key |
|---|---|
{enter} | Enter |
{tab} | Tab |
{esc} | Escape |
{backspace} | Backspace |
{selectall} | Select all |
{uparrow} | Up arrow |
Force Option
| Use | Effect |
|---|---|
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
| Pitfall | Why It Happens | Fix |
|---|---|---|
| Click does nothing | Element covered or disabled | Fix the overlay or the state |
| Select does not fire change | Wrong value type | Use the option’s value |
trigger does not behave like a click | Synthetic event | Use .click() |
| Type does not clear | No .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
| Item | Value |
|---|---|
| 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.
clickandtypeproduce native event sequences. Theclickdispatches the mouse events, and thetypedispatches a key sequence per character. The application’s handler receives the same events a real user would produce.selectfires thechangeevent. The argument can be the value, the text, or the index, and the command waits for the element to be enabled before selecting.triggeris 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: trueskips 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.
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!