|
| 1 | +--- |
| 2 | +title: rightclick |
| 3 | +--- |
| 4 | + |
| 5 | +Right click a DOM element. |
| 6 | + |
| 7 | +{% note warning %} |
| 8 | +`.rightclick()` will not open context menus native to the browser. `.rightclick()` should be used to test your app's handling of right click related events such as `contextmenu`. |
| 9 | +{% endnote %} |
| 10 | + |
| 11 | +# Syntax |
| 12 | + |
| 13 | +```javascript |
| 14 | +.rightclick() |
| 15 | +.rightclick(options) |
| 16 | +.rightclick(position) |
| 17 | +.rightclick(position, options) |
| 18 | +.rightclick(x, y) |
| 19 | +.rightclick(x, y, options) |
| 20 | +``` |
| 21 | + |
| 22 | +## Usage |
| 23 | + |
| 24 | +**{% fa fa-check-circle green %} Correct Usage** |
| 25 | + |
| 26 | +```javascript |
| 27 | +cy.get('.menu').rightclick() // Right click on .menu |
| 28 | +cy.focused().rightclick() // Right click on el with focus |
| 29 | +cy.contains('Today').rightclick() // Right click on first el containing 'Today' |
| 30 | +``` |
| 31 | + |
| 32 | +**{% fa fa-exclamation-triangle red %} Incorrect Usage** |
| 33 | + |
| 34 | +```javascript |
| 35 | +cy.rightclick('button') // Errors, cannot be chained off 'cy' |
| 36 | +cy.window().rightclick() // Errors, 'window' does not yield DOM element |
| 37 | +``` |
| 38 | + |
| 39 | +## Arguments |
| 40 | + |
| 41 | +**{% fa fa-angle-right %} position** ***(String)*** |
| 42 | + |
| 43 | +The position where the right click should be issued. The `center` position is the default position. Valid positions are `topLeft`, `top`, `topRight`, `left`, `center`, `right`, `bottomLeft`, `bottom`, and `bottomRight`. |
| 44 | + |
| 45 | +{% imgTag "/img/api/coordinates-diagram.jpg" "cypress-command-positions-diagram" %} |
| 46 | + |
| 47 | +**{% fa fa-angle-right %} x** ***(Number)*** |
| 48 | + |
| 49 | +The distance in pixels from the element's left to issue the right click. |
| 50 | + |
| 51 | +**{% fa fa-angle-right %} y** ***(Number)*** |
| 52 | + |
| 53 | +The distance in pixels from the element's top to issue the right click. |
| 54 | + |
| 55 | +**{% fa fa-angle-right %} options** ***(Object)*** |
| 56 | + |
| 57 | +Pass in an options object to change the default behavior of `.rightclick()`. |
| 58 | + |
| 59 | +Option | Default | Description |
| 60 | +--- | --- | --- |
| 61 | +`log` | `true` | {% usage_options log %} |
| 62 | +`force` | `false` | {% usage_options force rightclick %} |
| 63 | +`multiple` | `false` | {% usage_options multiple rightclick %} |
| 64 | +`timeout` | {% url `defaultCommandTimeout` configuration#Timeouts %} | {% usage_options timeout .rightclick %} |
| 65 | + |
| 66 | +## Yields {% helper_icon yields %} |
| 67 | + |
| 68 | +{% yields same_subject .rightclick %} |
| 69 | + |
| 70 | +# Examples |
| 71 | + |
| 72 | +## No Args |
| 73 | + |
| 74 | +### Right click the menu |
| 75 | + |
| 76 | +```javascript |
| 77 | +cy.get('#open-menu').rightclick() |
| 78 | +``` |
| 79 | + |
| 80 | +## Position |
| 81 | + |
| 82 | +### Specify a corner of the element to right click |
| 83 | + |
| 84 | +Right click the top right corner of the DOM element. |
| 85 | + |
| 86 | +```javascript |
| 87 | +cy.get('#open-menu').rightclick('topRight') |
| 88 | +``` |
| 89 | + |
| 90 | +## Coordinates |
| 91 | + |
| 92 | +### Specify explicit coordinates relative to the top left corner |
| 93 | + |
| 94 | +The right click below will be issued inside of the element (15px from the left and 40px from the top). |
| 95 | + |
| 96 | +```javascript |
| 97 | +cy.get('#open-menu').rightclick(15, 40) |
| 98 | +``` |
| 99 | + |
| 100 | +## Options |
| 101 | + |
| 102 | +### Force a right click regardless of its actionable state |
| 103 | + |
| 104 | +Forcing a right click overrides the {% url 'actionable checks' interacting-with-elements#Forcing %} Cypress applies and will automatically fire the events. |
| 105 | + |
| 106 | +```javascript |
| 107 | +cy.get('#open-menu').rightclick({ force: true }) |
| 108 | +``` |
| 109 | + |
| 110 | +### Force a right click with position argument |
| 111 | + |
| 112 | +```javascript |
| 113 | +cy.get('#open-menu').rightclick('bottomLeft', { force: true }) |
| 114 | +``` |
| 115 | + |
| 116 | +### Force a right click with relative coordinates |
| 117 | + |
| 118 | +```javascript |
| 119 | +cy.get('#open-menu').rightclick(5, 60, { force: true }) |
| 120 | +``` |
| 121 | + |
| 122 | +### Right click all buttons found on the page |
| 123 | + |
| 124 | +By default, Cypress will error if you're trying to right click multiple elements. By passing `{ multiple: true }` Cypress will iteratively apply the right click to each element and will also log to the {% url 'Command Log' test-runner#Command-Log %} multiple times. |
| 125 | + |
| 126 | +```javascript |
| 127 | +cy.get('.open-menu').rightclick({ multiple: true }) |
| 128 | +``` |
| 129 | + |
| 130 | +## Right click with key combinations |
| 131 | + |
| 132 | +The `.rightclick()` command may also be fired with key modifiers in combination with the {% url "`.type()`" type %} command in order to simulate character sequences while right clicking, such as `ALT + rightclick`. In order to keep the modifier key active, `{release: false}` should be passed to the options of the {% url "`.type()`" type %} command. |
| 133 | + |
| 134 | +The following modifiers can be combined with `.rightclick()`. |
| 135 | + |
| 136 | +Sequence | Notes |
| 137 | +--- | --- |
| 138 | +`{alt}` | Activates the `altKey` modifier. Aliases: `{option}` |
| 139 | +`{ctrl}` | Activates the `ctrlKey` modifier. Aliases: `{control}` |
| 140 | +`{meta}` | Activates the `metaKey` modifier. Aliases: `{command}`, `{cmd}` |
| 141 | +`{shift}` | Activates the `shiftKey` modifier. |
| 142 | + |
| 143 | +### Command right click |
| 144 | + |
| 145 | +```js |
| 146 | +// execute a CMD + right click on the .menu-item |
| 147 | +// { release: false } is necessary so that |
| 148 | +// CMD will not be released after the type command |
| 149 | +cy.get('body').type('{cmd}', { release: false }) |
| 150 | +cy.get('.menu-item').rightclick() |
| 151 | +``` |
| 152 | + |
| 153 | +# Notes |
| 154 | + |
| 155 | +## Actionability |
| 156 | + |
| 157 | +### The element must first reach actionability |
| 158 | + |
| 159 | +`.rightclick()` is an "action command" that follows all the rules {% url 'defined here' interacting-with-elements %}. |
| 160 | + |
| 161 | +# Rules |
| 162 | + |
| 163 | +## Requirements {% helper_icon requirements %} |
| 164 | + |
| 165 | +{% requirements dom .rightclick %} |
| 166 | + |
| 167 | +## Assertions {% helper_icon assertions %} |
| 168 | + |
| 169 | +{% assertions actions .rightclick %} |
| 170 | + |
| 171 | +## Timeouts {% helper_icon timeout %} |
| 172 | + |
| 173 | +{% timeouts actions .rightclick %} |
| 174 | + |
| 175 | +# Command Log |
| 176 | + |
| 177 | +***Right click the button in the form that has text "Create User"*** |
| 178 | + |
| 179 | +```javascript |
| 180 | +cy.get('form').find('button').contains('Create User').rightclick() |
| 181 | +``` |
| 182 | + |
| 183 | +The commands above will display in the Command Log as: |
| 184 | + |
| 185 | +{% imgTag /img/api/rightclick/rightclick-dom-element-in-command-log.png "Command log for right click" %} |
| 186 | + |
| 187 | +When clicking on `rightclick` within the command log, the console outputs the following: |
| 188 | + |
| 189 | +{% imgTag /img/api/rightclick/rightclick-console-log-with-mouse-events.png "console.log for right click" %} |
| 190 | + |
| 191 | +{% history %} |
| 192 | +{% url "3.5.0" changelog#3-5-0 %} | `.rightclick()` command added |
| 193 | +{% endhistory %} |
| 194 | + |
| 195 | +# See also |
| 196 | + |
| 197 | +- {% url `.click()` click %} |
| 198 | +- {% url `.dblclick()` dblclick %} |
| 199 | +- {% url `.trigger()` trigger %} |
0 commit comments