What is Cypress?
Cypress is a next generation front end testing tool built for the modern web. Learn about Cypress.io and its features.
Cypress bundles the popular Chai assertion library, as well as helpful extensions for Sinon and jQuery, bringing you dozens of powerful assertions for free.
Cypress bundles chai no need to add as a dependency to your project.
Pre-requisites
- Install Node.js and npm https://www.npmjs.com/get-npm
Setup
- create a directory for the project and cd to it:
mkdir cypress-assertions && cd cypress-assertions
- Run
npm init --y
to setup a new npm package project. - Install Cypress via npm
npm i cypress --save-dev
. - Verify Cypress by running
npx cypress open
. - Now cypress folder along with cypress.json file will be created in the project directory.
- "integration" folder contains cypress test examples.
- Ignore the examples folder by adding as ignored test is cypress.json
{
"ignoreTestFiles": "**/examples/*.js"
}
Creating and Run Tests
Under the “integration” folder create a new file. Name it “sample_assert.js”
/// <reference types="cypress" />
describe('Sample assert', () => {
it('first test', () => {
expect(2).to.equal(2);
})
})
In beforeEach block specify the url that needs to be navigated before each test.
/// <reference types="cypress" />
describe('Sample assert', () => {
beforeEach(() =>{
cy.visit(`https://example.cypress.io/commands/actions`);
})
it('first test', () => {
expect(2).to.equal(2);
})
})
Check for Visibility of element.
it(`Assert - Visibility of element`,()=>{
cy.get(`[id="email1"]`).should(`be.visible`);
})
it(`Assert - Disabled element`,()=>{
cy.get(`textarea`).should(`be.disabled`);
})
it(`Assert - Focused element`, () => {
cy.get('.action-focus').focus()
.should(`be.focused`)
})
it(`Assert - Blank Input box`, () => {
cy.get(`[id="email1"]`).should(`have.value`,``);
})
it(`Assert - Checkbox is checked`, ()=>{
cy.get(`[value="checkbox1"]`).first().click().should(`be.checked`)
})
it(`Assert - Object assertions`,() => {
let obj = {
name: 'Bharath'
}
expect(obj).to.deep.equal({ name: 'Bharath' })
})
Git repo: https://github.com/Bharath-Kumar-S/cypress-assertions
I hope this was helpful. Please leave your feedback.
Top comments (0)