Skip to content

Commit ee0b20c

Browse files
.rightclick command (#2174)
* Add rightclick doc * Update the contributing docs to be clearer about how to add a new doc (with new auto-generation for translations) * Update source/api/commands/rightclick.md Co-Authored-By: Ben Kucera <[email protected]> * Update id of DOM element to open content menu to make more sense * Add some more places where rightclick should be mentioned
1 parent ac3b9b0 commit ee0b20c

File tree

14 files changed

+213
-2
lines changed

14 files changed

+213
-2
lines changed

CONTRIBUTING.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,9 @@ To add a page such as a new guide or API documentation:
101101

102102
- Add the new page to the relevant directory under [`source`](/source).
103103
- Link to your new page in the [`sidebar.yml`](/source/_data/sidebar.yml).
104-
- Add translations for the sidebar link (for English, this is located in [`en.yml`](/themes/cypress/languages/en.yml)).
104+
- Add translations for the sidebar link for each supported language (for English, this is located in [`en.yml`](/themes/cypress/languages/en.yml)).
105105
- Build the documentation site locally so that you can visually inspect your new page and the links to it.
106-
- Copy over the new page to other language translations - Japanese docs in [`source/ja`](/source/ja), Chinese docs in [`source/zh-cn`](/source/zh-cn).
106+
- Commit the new file using git - we auto-generate the doc to display within each supported language, this auto-generation depends on the file existing in git.
107107
- Submit a [pull request](#Pull-Requests) for your change.
108108

109109
#### A Worked Example
@@ -167,6 +167,8 @@ Our currently supported languages can be found at [`/source/_data/languages.yml`
167167

168168
Translate existing documentation then submit a [pull request](#Pull-Requests) for your change.
169169

170+
**Note:** When adding a new doc file to the English source, the English file will need to be commited to git before the translated file is auto-generated.
171+
170172
If a page does not have a translation, then a pre-start step copies the English file to the language folder. These copies should NOT be committed into the source code. Only when the file has been translated you can add it to the source code with `git add --force source/<language>/.../file.md` and this file will not be overwritten by the English file.
171173

172174
## Committing Code

source/_data/sidebar.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ api:
111111
readfile: readfile.html
112112
reload: reload.html
113113
request: request.html
114+
rightclick: rightclick.html
114115
root: root.html
115116
route: route.html
116117
screenshot: screenshot.html

source/api/commands/click.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,7 @@ When clicking on `click` within the command log, the console outputs the followi
228228

229229
- {% url `.check()` check %}
230230
- {% url `.dblclick()` dblclick %}
231+
- {% url `.rightclick()` rightclick %}
231232
- {% url `.select()` select %}
232233
- {% url `.submit()` submit %}
233234
- {% url `.type()` type %}

source/api/commands/dblclick.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,3 +94,4 @@ When clicking on `dblclick` within the command log, the console outputs the foll
9494
# See also
9595

9696
- {% url `.click()` click %}
97+
- {% url `.rightclick()` rightclick %}

source/api/commands/rightclick.md

Lines changed: 199 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,199 @@
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 %}

source/api/commands/trigger.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,7 @@ When clicking on `trigger` within the command log, the console outputs the follo
236236
- {% url `.check()` check %}
237237
- {% url `.click()` click %}
238238
- {% url `.focus()` focus %}
239+
- {% url `.rightclick()` rightclick %}
239240
- {% url `.select()` select %}
240241
- {% url `.submit()` submit %}
241242
- {% url `.type()` type %}

source/guides/core-concepts/interacting-with-elements.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ Some commands in Cypress are for interacting with the DOM such as:
1717

1818
- {% url `.click()` click %}
1919
- {% url `.dblclick()` dblclick %}
20+
- {% url `.rightclick()` rightclick %}
2021
- {% url `.type()` type %}
2122
- {% url `.clear()` clear %}
2223
- {% url `.check()` check %}

source/guides/core-concepts/introduction-to-cypress.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,7 @@ Here are even more action commands Cypress provides to interact with your app:
237237
- {% url `.uncheck()` uncheck %} - Uncheck checkbox(es).
238238
- {% url `.select()` select %} - Select an `<option>` within a `<select>`.
239239
- {% url `.dblclick()` dblclick %} - Double-click a DOM element.
240+
- {% url `.rightclick()` rightclick %} - Right-click a DOM element.
240241

241242
These commands ensure {% url "some guarantees" interacting-with-elements %} about what the state of the elements should be prior to performing their actions.
242243

themes/cypress/languages/en.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ sidebar:
126126
readfile: readFile
127127
reload: reload
128128
request: request
129+
rightclick: rightclick
129130
root: root
130131
route: route
131132
screenshot: screenshot

themes/cypress/languages/ja.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ sidebar:
126126
readfile: readFile
127127
reload: reload
128128
request: request
129+
rightclick: rightclick
129130
root: root
130131
route: route
131132
screenshot: screenshot

0 commit comments

Comments
 (0)