top of page
Search

Custom Commands in Cypress

You can add your own command or overwrite an existing command using custom commands in cypress.


You can see two '.js' files under support folder,




In command.js file you can add your own commands. To add a new command, you can use,


Cypress.Commands.add('commandName',()=>{
        //Implementation
})

In your test file, you can use


cy.commandName()

to call your own command in test file.


To overwrite an existing command,


Cypress.Commands.overwrite('commandName',(arguments)=>{
        //Implementation
})

to use that overwritten function, write the command as usual and add the options parameter as you custom your command.

Code:

Cypress.Commands.add('contact',()=>{
 cy.visit('https://coderscamp.wixsite.com/codeit')
 cy.contains('Contact').click()
 cy.get('#input_comp-kntvy6i7').type('coderscamp')
 cy.get('#input_comp-kntvy6ir').type('coderscampindia@gmail.com')
 cy.get('#input_comp-kntvy6j4').type('testing custom commands')
 cy.get('#textarea_comp-kntvy6jb').type('All the best',{sensitive:true})
 cy.get('[data-testid=buttonElement]').click()
})

Cypress.Commands.overwrite('type',(originalFn,element,text,options)=>{
if(options && options.sensitive){
 options.log = false

 Cypress.log({
 $el: element,
 name: 'type',
 message: '*'.repeat(text.length),
    })
}

return originalFn(element,text,options)
})

So you have learned how to add or overwrite custom commands in cypress. Happy Testing!!!

































121 views0 comments

Recent Posts

See All

Minimum Deletions to Make Character Frequencies Unique

A string s is called good if there are no two different characters in s that have the same frequency. Given a string s, return the minimum number of characters you need to delete to make s good. The f

Smallest String With A Given Numeric Value

The numeric value of a lowercase character is defined as its position (1-indexed) in the alphabet, so the numeric value of a is 1, the numeric value of b is 2, the numeric value of c is 3, and so on.

bottom of page