To install Cypress, run the following command in your project directory:
npm install cypress --save-dev
To open the Cypress Test Runner:
npx cypress open
Create a new file in the cypress/integration
folder with a .spec.js
extension:
touch cypress/integration/my_first_test.spec.js
cy.visit('https://example.com')
cy.get('button').click()
cy.get('input[name="username"]').type('testuser')
cy.get('input[name="username"]').clear()
cy.get('form').submit()
cy.get('#myElementId')
cy.get('.myClassName')
cy.get('button')
cy.get('[data-test="submit-button"]')
cy.get('form').find('input[type="email"]')
cy.get('h1').should('have.text', 'Welcome')
cy.get('.error').should('not.be.visible')
cy.get('input').should('have.value', 'Hello')
cy.get('button')
.should('be.visible')
.and('have.text', 'Submit')
.and('not.be.disabled')
cy.get('body').then(($body) => {
expect($body).to.contain('Hello, World!')
})
cy.get('button', { timeout: 10000 }).should('be.visible')
cy.intercept('GET', '/api/users').as('getUsers')
cy.wait('@getUsers')
cy.get('button').should('be.enabled')
cy.fixture('users.json').then((users) => {
cy.get('input[name="username"]').type(users[0].username)
})
In cypress/support/commands.js
:
Cypress.Commands.add('login', (email, password) => {
cy.visit('/login')
cy.get('#email').type(email)
cy.get('#password').type(password)
cy.get('form').submit()
})
Using the custom command:
cy.login('user@example.com', 'password123')
cy.pause()
cy.log('This is a debug message')
cy.get('button').then(($el) => {
debugger
// Browser will pause here, allowing you to inspect $el
})
Use data-* attributes for test selectors
<button data-test="submit-button">Submit</button>
cy.get('[data-test="submit-button"]')
Avoid using cy.wait() with arbitrary time values Instead of:
cy.wait(5000)
Use:
cy.get('element-to-wait-for', { timeout: 5000 }).should('be.visible')
Use cy.intercept() to stub network requests
cy.intercept('GET', '/api/users', { fixture: 'users.json' })
Group related tests using describe() and context()
describe('User Authentication', () => {
context('Valid Credentials', () => {
it('should log in successfully', () => {
// Test code here
})
})
context('Invalid Credentials', () => {
it('should show an error message', () => {
// Test code here
})
})
})
Use before() and beforeEach() hooks for setup
beforeEach(() => {
cy.visit('/')
cy.login('testuser', 'password123')
})
Avoid sharing state between tests Instead of using global variables, use closures or Cypress aliases:
it('creates a user and verifies it', () => {
let userId
cy.request('POST', '/api/users', { name: 'John Doe' })
.then((response) => {
userId = response.body.id
cy.wrap(userId).as('userId')
})
cy.get('@userId').then((userId) => {
cy.visit(`/users/${userId}`)
cy.get('h1').should('contain', 'John Doe')
})
})
Use cy.intercept() to test loading states
cy.intercept('GET', '/api/slow-endpoint', (req) => {
req.on('response', (res) => {
res.setDelay(2000)
})
}).as('slowRequest')
cy.visit('/page-with-slow-request')
cy.get('#loading-indicator').should('be.visible')
cy.wait('@slowRequest')
cy.get('#loading-indicator').should('not.exist')
Use environment variables for sensitive data
In cypress.json
:
{
"env": {
"api_key": "CYPRESS_API_KEY"
}
}
In your test:
cy.request({
url: '/api/protected-endpoint',
headers: {
'Authorization': `Bearer ${Cypress.env('api_key')}`
}
})
Remember to never commit sensitive data to version control. Use environment variables or CI/CD secrets management instead.
2024 © All rights reserved - buraxta.com