Amir Rustamzadeh
Head of Developer Experience @ Cypress.io
@amirrustam
Our mission is to build a thriving, open source ecosystem that enhances productivity, makes testing an enjoyable experience, and generates developer happiness. We hold ourselves accountable to champion a testing process that actually works.
$ npm install -D cypress
$ npm install -D cypress
$ npx cypress open
cy.<command>
cy.get('button')
cy.get('button')
Β .click()
.should('have.class', 'active')
cy.request('/users/1')
Β .its('body')
.should('deep.eql',{ name:'Amir'})
cy.get('button')
Β .click()
.should('have.class', 'active')
it('send email with contact form', () => {
cy.get('#name-input').type('Amir')
cy.get('#email-input').type('amir@cypress.io')
cy.get('form').submit()
cy.get('#success-message').should('be.visible')
})
β | |
---|---|
β | |
β | |
β |
cy.get('#name-input').type('Amir') cy.get('#email-input').type('amir@cypress.io') cy.get('form').submit() cy.get('#success-message').should('be.visible')
cy.get('button')
Β .click()
.should('have.class', 'active')
it('signup and login user', () => {
cy.visit('http://localhost:8080/signup')
cy.get('input[name="email"]').type('amir@cypress.io')
cy.get('input[name="password"]').type('1234')
cy.get('input[name="confirm-password"]').type('1234')
cy.get('#signup-button').click()
cy.location('pathname').should('eq', '/login')
cy.get('input[name="email"]').type('amir@cypress.io')
cy.get('input[name="password"]').type('1234')
cy.get('#login-button').click()
cy.location('pathname').should('eq', '/board')
})
cy.task
// cypress/plugins/index.js
const { clearDatabase } = require('../../server/db')
module.exports = (on, config) => {
on('task', {
'clear:db': () => {
return clearDatabase()
}
})
}
context('User setup', () => {
beforeEach(() => {
cy.task('clear:db')
})
it('signup and login user', () => {
cy.visit('http://localhost:8080/signup')
cy.get('input[name="email"]').type('amir@cypress.io')
cy.get('input[name="password"]').type('1234')
cy.get('input[name="confirm-password"]').type('1234')
cy.get('#signup-button').click()
cy.location('pathname').should('eq', '/login')
cy.get('input[name="email"]').type('amir@cypress.io')
cy.get('input[name="password"]').type('1234')
cy.get('#login-button').click()
cy.location('pathname').should('eq', '/board')
})
})
context('User setup', () => {
beforeEach(() => {
cy.task('clear:db')
})
it('signup and login user', () => {
cy.visit('http://localhost:8080/signup')
cy.get('input[name="email"]').type('amir@cypress.io')
cy.get('input[name="password"]').type('1234')
cy.get('input[name="confirm-password"]').type('1234')
cy.get('#signup-button').click()
cy.location('pathname').should('eq', '/login')
cy.get('input[name="email"]').type('amir@cypress.io')
cy.get('input[name="password"]').type('1234')
cy.get('#login-button').click()
cy.location('pathname').should('eq', '/board')
})
})
context('User setup', () => {
beforeEach(() => {
cy.task('clear:db')
})
it('signup and login user', () => {
cy.visit('http://localhost:8080/signup')
cy.get('input[name="email"]').type('amir@cypress.io')
cy.get('input[name="password"]').type('1234')
cy.get('input[name="confirm-password"]').type('1234')
cy.get('#signup-button').click()
cy.location('pathname').should('eq', '/login')
cy.login('amir@cypress.io', '1234')
cy.location('pathname').should('eq', '/board')
})
})
// cypress/support/commands.js
Cypress.Commands.add('login', (email, password) => {
cy.get('input[name="email"]').type(email)
cy.get('input[name="password"]').type(password)
cy.get('#login-button').click()
})
context('User setup', () => {
beforeEach(() => {
cy.task('clear:db')
})
it('signup and login user', () => {
cy.visit('http://localhost:8080/signup')
cy.get('input[name="email"]').type('amir@cypress.io')
cy.get('input[name="password"]').type('1234')
cy.get('input[name="confirm-password"]').type('1234')
cy.get('#signup-button').click()
cy.location('pathname').should('eq', '/login')
cy.login('amir@cypress.io', '1234')
cy.location('pathname').should('eq', '/board')
})
})
// cypress/plugins/index.js
const { clearDatabase, seedDatabase } = require('../../server/db')
module.exports = (on, config) => {
on('task', {
'clear:db': () => {
return clearDatabase()
}
})
on('task', {
'seed:db': (data) => {
return seedDatabase(data)
}
})
}
const userSeed = require('../../server/seed/users')
context('User setup', () => {
beforeEach(() => {
cy.task('clear:db')
cy.task('seed:db', userSeed.data)
})
it('login user', () => {
cy.visit('http://localhost:8080/login')
cy.login('amir@cypress.io', '1234')
cy.location('pathname').should('eq', '/board')
})
})
// cypress/support/commands.js
Cypress.Commands.add('loginWithUI', (email, password) => {
cy.get('input[name="email"]').type(email)
cy.get('input[name="password"]').type(password)
cy.get('#login-button').click()
})
Cypress.Commands.add('login', (email, password) => {
return cy.window().then(win => {
return win.app.$store.dispatch('login', {
email: 'amir@cypress.io',
password: '1234'
})
})
})
const userSeed = require('../../server/seed/users')
context('BirdBoard', () => {
beforeEach(() => {
cy.task('clear:db')
cy.task('seed:db', userSeed.data)
cy.visit('http://localhost:8080/login')
cy.login('amir@cypress.io', '1234')
})
it('load tweets for selected hashtags', () => {
cy.server()
// Fixture is stored in cypress/fixtures/tweets.json
cy.route('GET', '/tweets*', 'fixture:tweets')
.as('tweets')
cy.get('#hashtags')
.type('javascript{enter}')
.type('cypressio{enter}')
cy.window().then(win => {
cy.wait('@tweets')
.its('response.body.tweets')
.should('have.length', win.app.$store.state.tweets.length)
})
})
})
const userSeed = require('../../server/seed/users')
context('BirdBoard', () => {
// ....
it('load tweets for selected hashtags', () => {
cy.server()
// Fixture is stored in cypress/fixtures/tweets.json
cy.fixture('tweets').then((tweets) => {
cy.route({
url: '/tweets*',
response: tweets,
delay: 3000, // simulate slow response
status: 404 // simulate error scenarios
})
.as('tweets')
})
cy.get('#hashtags')
.type('javascript{enter}')
.type('cypressio{enter}')
cy.window().then(win => {
cy.wait('@tweets')
.its('response.body.tweets')
.should('have.length', win.app.$store.state.tweets.length)
})
})
})
$ npx cypress run
$ npx cypress run
$ npx cypress run --record
$ npx cypress run --record --parallel
on.cypress.io/chat
@amirrustam
Feel free to ask questions on Twitter
or via email amir@cypress.io
By Amir Rustamzadeh
A live webcast to see a bird's eye view of the Cypress landscape, and to help you get up and running with testing a real application with Cypress.