Skip to main content

Cypress rightclick Command

Right-click command performs the right-click operation on the provided element. Note that it does not open up the right-click context menu native to the browser.

Instead, this one is used to test the element functionality when it has right-click and operations done.

Syntax

.rightclick()
.rightclick(options)
.rightclick(position)
.rightclick(position, options)
.rightclick(x, y)
.rightclick(x, y, options)

Arguments Used In Right Click

  • Options: Can pass in objects to change the default behavior of .rightclick() Here is the list of some of the options that cypress can use with the right-click command:
  • altKey: Activates alt key in windows and options key in mac. Default is false.
  • waitForAnimations: Waits for the animations to finish before right-clicking. Default is waitForAnimations.
  • animationDistanceThreshold: Used to consider the distance between the pixels and the element and that mist exceeds overtime. Default is animationDistanceThreshold.
  • timeout: Used to input time to wait for the right click to resolve before timing out. Default is defaultCommandTimeout.
  • ctrlKey: This activates the control key. Default is false.
  • log: Displays the record in command log in output. Default is true.
  • Force: Forces right clickability. Default is false.
  • MetaKey: Used to activate windows key in windows and command key in mac. Default is false.
  • Multiple: This is used to clock on multiple elements serially. Default is false.
  • ScrollBehaviour: This is used to viewport position where an element should be scrolled before the command execution. Default is ScrollBehaviour.
  • ShiftKey: This activates the sift key. Default is false.
  • Position: Various arguments based on the positions can also be passed onto where the right click command should perform. Here are some of the positions topLeft, top, top right, left, center, right, bottomLeft, bottom, and bottomRight.
  • X and Y: Can mention the pixel distances coordinates on where it should be right-clicked on.

Right click command correct use

y.get('.btn').rightclick() 
  • right clicks the btn element.
cy.focused().rightclick() 
  • right clicks with more focus.
cy.contains('Welcome').rightclick() 
  • right clicks text containing 'Welcome.'

Wrong use

cy.rightclick()
cy.window().rightclick()

Example

describe('The RightClick Command',() => {
    it('visit the site, perform various clicks',()=>{
        cy.visit('https://www.programsbuzz.com/')
        cy.contains('Start Learning').rightclick()
    })
})

This one right-clicks the text containing 'Start Learning.'

describe('The RightClick Command',() => {
    it('visit the site, perform various clicks',()=>{
        cy.visit('https://www.programsbuzz.com/')
        cy.get('page-img').rightclick('bottomLeft')
    })
})

Right clicks the bottomLeft element after getting the entire page.

describe('The RightClick Command',() => {
    it('visit the site, perform various clicks',()=>{
        cy.visit('https://www.programsbuzz.com/')
        cy.get('#id').rightclick(10, 100)
    })
})

Right clicks on the provided id after getting the id with coordinates of CSS px distance.

describe('The RightClick Command',() => {
    it('visit the site, perform various clicks',()=>{
        cy.visit('https://www.programsbuzz.com/')
        cy.get('#id').as('id')
        cy.get('@id').rightclick('topRight', {force: true})
    })
})

Aliases the element and uses the same alias to right click on the element present on the top right of the page with force.

describe('The RightClick Command',() => {
    it('visit the site, perform various clicks',()=>{
        cy.visit('https://www.programsbuzz.com/')
        cy.get('#id').rightclick({multiple: true})
    })
})

Right clicks on the id element, which is present everywhere.

describe('The RightClick Command',() => {
    it('visit the site, perform various clicks',()=>{
        cy.visit('https://www.programsbuzz.com/')
        cy.get('#id').rightclick({shiftKey: true})
    })
})

Activates the shift key and rightclicks along with it. Identical syntaxes go for other keypress arguments.

Tags
Submitted by arilio666 on February 8, 2022

Ashwin is working with iVagus as a Technical Lead. He has experience in various technologies like Cypress, JavaScript, TypeScript, Rest Assured, Selenium, Cucumber, and Java.

About

At ProgramsBuzz, you can learn, share and grow with millions of techie around the world from different domain like Data Science, Software Development, QA and Digital Marketing. You can ask doubt and get the answer for your queries from our experts.