Git Product home page Git Product logo

cypress-pom-example's Introduction

cypress-pom-example

A sample Cypress implementation with Page Object Model

Pre-requisites

Node.js must be installed

๐Ÿ—๏ธ Install

Install project dependencies by running command:

npm install

๐Ÿš€ Run tests

  • Open Cypress interface by running command:
npm run cy:open
  • To run tests in headless mode, use command:
npm run cy:test:po
  • To run tests created dynamically from a fixture file, more explanation here:
npm run cy:test:dy

or

npm run cy:test:dy:smoke

๐Ÿ“– Code Structure

For this example of Page Object Model we have organize files as follows:

cypress.config.js
First generated by Cypress, this file contains configuration you do to set Cypress and your test environment

Here we have only define the app url:

module.exports = defineConfig({
  e2e: {
    env: {
      url: 'https://example.cypress.io'
    }
  }
});

cypress/e2e/*
This folder contains all spec file for end-to-end tests.

cypress/support/page/*
Contains page object files. Each file contains the description of a specific page of the app to be tested. There a is 'special' file, 'base.page.js' which is a description of all the common actions available to any other page of the app. In our example, all actions related the navbar header.

cypress/fixures/*
Fixtures files use to define data set used

๐Ÿ”ง Dynamic Test from fixture

Often we need to use the same test but with different data. To avoid to write repetitive code, we use a fixture file to define data. Then we import it in the spec file using:

const suites = require('../fixtures/dynamic.json')

We can then apply a filter on testsuiteID

suites.forEach((suite) => {
  if (suite.testsuiteID === Cypress.env('testsuiteID')) {
    context(`Test for testsuite '${suite.testsuiteID}'`, () => {
      // implement test here
    })
  })
})

testsuiteID -is set in the 'cypress.config.js' file but can also be set through the command line with option -env testsuiteID=smoke

Finally, for the suite we create a test for each data.

suite.data.forEach((testcase) => {
  it(`login for user ${testcase.name}`, () => {
    queryingPage.visit()
    queryingPage.getNameField().type(testcase.name)
    queryingPage.getMailField().type(testcase.mail)
    queryingPage.getPwdField().type(testcase.pwd)
  })
})

๐Ÿ”— Links

https://docs.cypress.io/

cypress-pom-example's People

Contributors

kristenjoie avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.