Cypress

JavaScript Test Package Cypress

Install

npm install cypress --save-dev
yarn add cypress --dev

Open

npx cypress open
yarn run cypress open

Setting

BaseUrl

// cypress.config.js
const { defineConfig } = require("cypress");

module.exports = defineConfig({
  e2e: {
    baseUrl: 'https://example.com',
    setupNodeEvents(on, config) {
      // implement node event listeners here
    },
  },
});

Testing

describe('run some test scenario', () => {

  beforeEach(() => {
    cy.visits('http://www.google.com');
  });

  it('visit page test', () => {

  });

});

iframe

cy.get('iframe').its('0.contentDocument').then(cy.log);

let getIframe = () => {
  return cy.get('iframe').its('0.contentDocument').should('not.be.empty').then(cy.wrap);
}

Cypress.Commands.add('getIframe', () => {
  return cy.get('iframe').its('0.contentDocument').should('not.be.empty').then(cy.wrap);
});

cy.getIframe();

Cypress.Commands.add('enterStripeCard', (options = {}) => {
  options = {
    ...{ approved: true },
    .. options,
  };
  
  cy.getIframe().within(() => {
    cy.get('[name=cardnumber]')
      .type(options.approved ? '4242424242424242' : '4000000000000002')
      .wait(300);
    
    cy.get('[name=exp-date]').type('0424').wait(300);
    cy.get('[name=cvc]').type('242').wait(300);
    cy.get('[name=postal]').type('42424');
  });

});

Reference


Command

JavaScript Test Package Cypress Command

Assertion

JavaScript Test Package Cypress Assertion