PAPracticeAutomation
Cy

JavaScript ยท Cypress

Element handling with Cypress

15 copy-ready Cypress recipes, each linked to a live challenge page to run them against.

Conventions
// Uses the getByTestId custom command from the setup above; cy.visit('/<challenge>') first.

1Type into inputs & read output

Practise on /elements/text-box โ†’
cy.getByTestId('full-name-input').type('Ada Lovelace');
cy.getByTestId('submit-text-box').click();
cy.getByTestId('output-name').should('have.text', 'Name: Ada Lovelace');
cy.getByTestId('checkbox-select-all').check();
cy.getByTestId('checkbox-documents').should('be.checked');
cy.getByTestId('native-select').select('Mumbai');
cy.getByTestId('native-select-result').should('have.text', 'Selected: Mumbai');
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

Practise on /elements/web-tables โ†’
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

Practise on /elements/buttons โ†’
cy.getByTestId('double-click-me').dblclick();
cy.getByTestId('right-click-me').rightclick();
cy.getByTestId('right-click-result').should('be.visible');

7JS alert, confirm & prompt

Practise on /alerts โ†’
// 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

Practise on /frames โ†’
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

Practise on /windows โ†’
// 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');
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)');
cy.getByTestId('file-input').selectFile('cypress/fixtures/sample.txt');
cy.getByTestId('uploaded-file-info').should('contain', 'sample.txt');
// CSS :hover needs a real pointer event: npm i -D cypress-real-events
cy.getByTestId('tooltip-trigger').realHover();
cy.getByTestId('tooltip').should('be.visible');

13Wait for dynamically loaded content

Practise on /dynamic/dynamic-loading โ†’
cy.getByTestId('loading-1-start').click();
cy.getByTestId('loading-1-result', { timeout: 10000 }).should('contain', 'Hello World!');
cy.get('shadow-counter').shadow().find('#inc').click();
cy.get('shadow-counter').shadow().find('#count').should('have.text', 'Count: 1');
cy.getByTestId('key-input').type('{enter}');
cy.getByTestId('key-result').should('have.text', 'You entered: Enter');

Practising another tool?