You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
describe('This is my test',()=>{before(()=>{console.log('Runs once before all tests')})beforeEach(()=>{console.log('Runs before each test')cy.visit('http://www.google.com')})it('works fine',()=>{cy.get('input[name="q"]').type('hello')})afterEach(()=>{console.log('Runs after each test')})after(()=>{console.log('Runs once after all tests')})})
Visit page
cy.visit('http://www.google.com')
Selectors
Select element by css selector
cy.get('button.green')
Select element containing text
cy.contains('Submit')
Select element using the "data-cy" attribute (cypress-recommended tag)
cy.get('h1').invoke('text').should('equal','Test')cy.get('h1').eq(2).should('have.text','Test')/* Use 'eq(n)' to pick the (n-1)th matching element */cy.get('h1').eq(2).invoke('text').should('equal','Test')cy.get('h1').should('contain','Test')cy.get('h1').should('not.contain','Junk')
cy.get('h1').should('have.attr','value','Test')cy.get('h1').eq(1).should('have.attr','value','Test')/*If we want to use the 2nd h1 when there are more than one h1's*/
/* Prefix the environment variable name with 'CYPRESS_'. For eg, CYPRESS_TEST_VAR=Test */Cypress.env('TEST_VAR')
Simulate a logged-in user by manually setting a cookie to have a populated token
/* This assumes that a cookie 'user_token' is set on successful login */cy.setCookie('user_token','<value of token of a loggen-in user>')/* Cypress clears cookies by default between tests. To NOT clear a specific cookie, add the below in `support/index.js` */Cypress.Cookies.defaults({preserve: 'trello_token'})/* To preserve cookies only for tests in a specifc spec file, add this to the `beforeEach` in that spec file, and then do a setCookie in the first test */Cypress.Cookies.preserveOnce('trello_token')
Overwrite the "cy.log" command to log to terminal instead of browser console
/* Define a "log" task in the "plugins/index.js" file as below */module.exports=(on,config)=>{on('task',{log: message=>{console.log(message)returnnull},})}/* Overwrite the "cy.log" command in the "support/commands.js" file as below */Cypress.Commands.overwrite('log',(subject,message)=>cy.task('log',message))/* Once the above 2 changes are done, any call to "cy.log" will now log to terminal */cy.log('Test')
cy.request({method: 'POST',url: '/url/endpoint',headers: {'x-api-key': Cypress.env('apiKey'),'Content-Type': 'application/json',},body: (parameter1: 'First parameter'),failOnStatusCode: false,// set this only if you are expecting a error response (ie 4xx or 5xx http response codes)}).then(response=>{expect(response.status).to.eq(403)expect(response.body.data.attributes).to.have.property('authorization_url')expect(response.body.data).to.have.property('id')expect(response.body.data.attributes).to.have.property('result','Success')expect(response.body.message).to.eq('Failed')expect(response.body.data.id).to.be.a('string')})
Intercept a browser request, and wait for that request to complete before moving on
cy.intercept({method: 'GET',url: '/path/endpoint'}).as('requestToWaitFor')// alias this cy.wait('@requestToWaitFor')// wait for the alised intercept.its('response.statusCode')// assert status code.should('eq',200)//ORcy.wait('@requestToWaitFor')// wait for the alised intercept.then((importantRequest)=>{// make multiple assertionsexpect(importantRequest.response.statusCode).to.eq(200)expect(importantRequest.request.body.parameter1).to.eq('First parameter')})cy.get('#elementId').click()// This will be run only after the wait above
Intercept a GraphQL request sent by the browser and extract something from its response
/* Add a command as below in the "support/commands.js" file */Cypress.Commands.add('interceptMyGraphqlRequest',()=>{cy.intercept('POST',/^https:\/\/api.*\/mywebsite\/graphql$/,req=>{if(req.body.operationName.includes('myOperationName')){req.alias='myGraphqlRequest'}})})/* And in your spec where you want to start intercepting, add the below line right before the step that results in the graphQL request being sent out */cy.interceptMyGraphqlRequest()/* To wait for the request and process its response */cy.wait('@myGraphqlRequest').then(graphqlRequestDetails=>{constmyField=graphqlRequestDetails.response.body.data.result.fieldThatICareAbout;expect(myField).to.match(/\d+/)})
Intercepting and sending a stubbed response
// Add this intercept statement before performing an action that results in this requestcy.intercept({method: 'GET',url: '/path/endpoint'},{body: []// return empty array as body// ORfixture: 'name_of_fixture_json_file_in_fixtures_folder'// Note: dont add '.json' at end// ORforceNetworkError: true// to force a network error for this request}).as('requestToSendAStubbedResponseFor')// alias this // If more manipulation is needed for the stubbing, use thiscy.intercept({method: 'GET',url: '/path/endpoint'},(request)=>{console.log(request)request.reply((response)=>{console.log(response)// Log actual responseresponse.body[1].parameter1='Manipulated parameter 1 in response'returnresponse})}).as('requestToSendAStubbedResponseFor')// alias this
Run a task, for example, initialize a database before the test
// Create a file in the `plugins` folder, with the implementation of the reset in a file `initializeDatabase.js`module.exports.initData=(parameter=empty)=>{// Do stuff to DB using 'parameter'};// Inside the `plugins/index.js` file, add a task as below:const{ initializeData }=require('./initializeDatabase')module.exports=(on,config)=>{on('task',{initData: initializeData})}// Now in the spec file, run this task as belowcy.task('initData',{myData: 'Test'})
Misc
Cypress code completion in VS Code in file
/// <reference types="Cypress" />
Cypress code completion in VS Code for whole project
/*In file jsconfig.json in root folder of project*/{"Include": ["./node_modules/cypress","cypress/**/*.js"]}