Cy
JavaScript Β· Cypress
Element handling with Cypress
23 copy-ready Cypress recipes in JavaScript and TypeScript, each linked to a live challenge page to run them against.
Language
Cypress runs inside the browser, so it only supports JavaScript and TypeScript β there is no Python, Java or C# version of Cypress. For those languages, see Selenium or Playwright.
Conventions Β· JavaScript
// Uses the getByTestId custom command from the setup above; cy.visit('/<challenge>') first.1Type into inputs & read output
Practice on /elements/text-box βJavaScript
cy.getByTestId('full-name-input').type('Ada Lovelace');
cy.getByTestId('submit-text-box').click();
cy.getByTestId('output-name').should('have.text', 'Name: Ada Lovelace');2Checkboxes & select-all
Practice on /elements/check-box βJavaScript
cy.getByTestId('checkbox-select-all').check();
cy.getByTestId('checkbox-documents').should('be.checked');3Native <select> dropdown
Practice on /widgets/selects-autocomplete βJavaScript
cy.getByTestId('native-select').select('Mumbai');
cy.getByTestId('native-select-result').should('have.text', 'Selected: Mumbai');4Autocomplete suggestions
Practice on /widgets/selects-autocomplete βJavaScript
cy.getByTestId('autocomplete-input').type('Ben');
cy.getByTestId('autocomplete-option-bengaluru').click();
cy.getByTestId('autocomplete-result').should('have.text', 'Picked: Bengaluru');5Find a table row by its text
Practice on /elements/web-tables βJavaScript
cy.contains('[data-testid="table-row"]', 'Priya')
.find('[data-testid^="delete-row-"]').click();
cy.getByTestId('table-row').should('have.length', 2);6Double-click & right-click
Practice on /elements/buttons βJavaScript
cy.getByTestId('double-click-me').dblclick();
cy.getByTestId('right-click-me').rightclick();
cy.getByTestId('right-click-result').should('be.visible');7JS alert, confirm & prompt
Practice on /alerts βJavaScript
// Cypress auto-accepts alerts & confirms; stub prompt yourself.
cy.window().then((win) => cy.stub(win, 'prompt').returns('Ada'));
cy.getByTestId('prompt-button').click();
cy.getByTestId('prompt-result').should('have.text', 'You entered Ada');
cy.on('window:confirm', () => false); // click "Cancel"
cy.getByTestId('confirm-button').click();
cy.getByTestId('confirm-result').should('have.text', 'You selected Cancel');8Iframes & nested iframes
Practice on /frames βJavaScript
cy.getByTestId('single-frame')
.its('0.contentDocument.body').should('not.be.empty')
.then(cy.wrap)
.find('[data-testid="single-frame-button"]').click();9New tabs & windows
Practice on /windows βJavaScript
// Cypress drives a single tab β drop target="_blank" and stay in it.
cy.getByTestId('new-tab-button').invoke('removeAttr', 'target').click();
cy.getByTestId('new-tab-heading').should('be.visible');10HTML5 drag & drop
Practice on /interactions/droppable βJavaScript
const dataTransfer = new DataTransfer();
cy.getByTestId('draggable-box').trigger('dragstart', { dataTransfer });
cy.getByTestId('accept-zone').trigger('drop', { dataTransfer });
cy.getByTestId('accept-zone').should('contain', 'Accept (1)');11File upload
Practice on /elements/upload-download βJavaScript
cy.getByTestId('file-input').selectFile('cypress/fixtures/sample.txt');
cy.getByTestId('uploaded-file-info').should('contain', 'sample.txt');12Hover tooltips
Practice on /widgets/tooltips-menus βJavaScript
// CSS :hover needs a real pointer event: npm i -D cypress-real-events
// Scroll to the centre: Cypress scrolls to the top by default, where a sticky
// header can sit on top of the element and receive the hover instead.
cy.getByTestId('tooltip-trigger').realHover({ scrollBehavior: 'center' });
cy.getByTestId('tooltip').should('be.visible');13Wait for dynamically loaded content
Practice on /dynamic/dynamic-loading βJavaScript
cy.getByTestId('loading-1-start').click();
cy.getByTestId('loading-1-result', { timeout: 10000 }).should('contain', 'Hello World!');14Shadow DOM
Practice on /shadow-dom βJavaScript
cy.get('shadow-counter').shadow().find('#inc').click();
cy.get('shadow-counter').shadow().find('#count').should('have.text', 'Count: 1');15Nested & closed shadow roots
Practice on /shadow-dom βJavaScript
cy.get('[data-testid=nested-outer-host]').shadow().find('qa-nested-inner')
.shadow().find('[data-testid=nested-input]').type('Ada');
cy.get('[data-testid=nested-outer-host]').shadow().find('qa-nested-inner')
.shadow().find('[data-testid=nested-submit]').click();
// Closed root: force it open before the app loads.
cy.visit('/shadow-dom', {
onBeforeLoad(win) {
const orig = win.Element.prototype.attachShadow;
win.Element.prototype.attachShadow = function (init) { return orig.call(this, { ...init, mode: 'open' }); };
},
});
cy.get('[data-testid=closed-host]').shadow().find('[data-testid=closed-input]').type('Ada');16Shadow DOM inside an iframe
Practice on /shadow-dom βJavaScript
cy.get('[data-testid=shadow-in-iframe]').its('0.contentDocument.body').should('not.be.empty')
.then(cy.wrap)
.find('frame-shadow').shadow()
.find('frame-shadow-inner').shadow()
.find('[data-testid=frame-nested-input]').type('Ada');17Cross-origin iframe
Practice on /frames βJavaScript
// Cypress runs inside the page, so the same-origin policy applies:
// contentDocument of a cross-origin frame is null. Test that frame's page
// directly (cy.visit its URL) or assert only what the parent page shows.
cy.getByTestId('probe-frame').click();
cy.getByTestId('probe-result').should('contain', 'cross-origin');18Element covered by an overlay
Practice on /elements/hidden-elements βJavaScript
// cy.click() fails: "is being covered by another element".
cy.getByTestId('remove-overlay').click();
cy.getByTestId('covered-button').click();
cy.getByTestId('covered-result').should('be.visible');19Tick a table row by another cell's text
Practice on /elements/web-tables βJavaScript
cy.contains('tr', 'Farah Siddiqui').find('input[type=checkbox]').check();
cy.getByTestId('selected-users').should('contain', 'EMP-1043');20Verify a column sorts correctly (numbers, not text)
Practice on /elements/web-tables βJavaScript
cy.getByTestId('sort-amount').click();
cy.getByTestId('sort-th-amount').should('have.attr', 'aria-sort', 'ascending');
cy.get('[data-testid="sort-row"] [data-col="amount"]').then(($cells) => {
const nums = [...$cells].map((c) => c.innerText).filter((t) => t !== 'β')
.map((t) => Number(t.replace(/[^\d.]/g, '')));
expect(nums).to.deep.equal([...nums].sort((a, b) => a - b));
});21normalize-space(), pseudo-elements & DOM comments
Practice on /elements/xpath-functions βJavaScript
cy.contains('label', 'Delivery Address') // cy.contains ignores extra whitespace
.invoke('attr', 'for').then((id) => cy.get('#' + id).type('12 MG Road'));
cy.window().then((win) => {
cy.getByTestId('pseudo-price').then(($el) => {
expect(win.getComputedStyle($el[0], '::before').content).to.contain('βΉ');
});
});
cy.document().then((doc) => {
const code = doc.evaluate("//span[@data-testid='comment-target']/following-sibling::comment()",
doc, null, 2 /* STRING_TYPE */, null).stringValue.trim();
expect(code).to.eq('secret-code: QA-2026');
});22Find a record across pages
Practice on /elements/breadcrumbs-pagination βJavaScript
function findOrder(id) {
cy.get('[data-testid=orders-table]').then(($t) => {
if ($t.find(`[data-order-id="${id}"]`).length) return;
cy.getByTestId('orders-next').should('not.be.disabled').click();
findOrder(id); // recurse β no loops in Cypress chains
});
}
findOrder('ORD-1042');
cy.get('[data-order-id="ORD-1042"]').should('be.visible');23Keyboard keys
Practice on /interactions/key-presses βJavaScript
cy.getByTestId('key-input').type('{enter}');
cy.getByTestId('key-result').should('have.text', 'You entered: Enter');