/
README.md
372 lines (259 loc) · 14.7 KB
/
README.md
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
<p align="center">
<img src="./logo.png"/>
</p>
<h1 align="center"> Cypress Real Events </h1>
<h3 align="center"> Fire native system events from Cypress </h3>
<p align="center">
<a href="https://www.npmjs.com/package/cypress-real-events">
<img src="https://img.shields.io/npm/v/cypress-real-events?style=for-the-badge" alt="npm version">
<a/>
<a href="https://www.npmjs.com/package/cypress-real-events">
<img src="https://img.shields.io/npm/dm/cypress-real-events?style=for-the-badge" alt="npm version">
<a/>
<a href="https://app.circleci.com/pipelines/github/dmtrKovalenko/cypress-real-events">
<img src="https://img.shields.io/circleci/build/github/dmtrKovalenko/cypress-real-events/main?style=for-the-badge" alt="npm version">
<a/>
<img src="https://img.shields.io/badge/made-with%20love-orange?style=for-the-badge"/>
<a href="https://twitter.com/goose_plus_plus">
<img src="https://img.shields.io/twitter/follow/goose_plus_plus?color=blue&style=for-the-badge">
<a/>
<p />
## Why?
Cypress default events are simulated. That means that all events like `cy.click` or `cy.type` are fired from javascript. That's why these events will be untrusted (`event.isTrusted` will be `false`) and they can behave a little different from real native events. But for some cases, it can be impossible to use simulated events, for example, to fill a native alert or copy to the clipboard. This plugin solves this problem.
<img src="https://forthebadge.com/images/badges/it-works-why.svg">
Thanks to [Chrome Devtools Protocol](https://chromedevtools.github.io/devtools-protocol/). Cypress is connecting to CDP for tasks like screenshots, setting viewport, and others. This project utilizes the same connection to fire system events. The event firing system works literally like in puppeteer. And as a result, unlocks such features like **hovering** and **native focus management via Tab**.
## Requirements
Cypress only. Really. Cypress itself can fire native events. The only limitation for real events – **they work only in the chromium-based browser**. That means that Firefox is not supported, at least for now.
## Quick overview
Here is a simple test that can be written with native events:
```js
it("tests real events", () => {
cy.get("input").realClick(); // perform a native real click on the field
cy.realType("cypress real event"); // fires native system keypress events and fills the field
cy.realPress("Tab"); // native tab click switches the focus
cy.get("input").realMouseDown(); // perform a native mouse press on the field
cy.get("input").realMouseUp(); // perform a native mouse release on the field
cy.focused().realHover(); // hovers over the new focused element
cy.contains("some text in the hovered popover");
});
```
## Installation
Install npm package:
```
npm install cypress-real-events
yarn add cypress-real-events
```
Register new commands by adding this to your `cypress/support/index.{js,ts}` file.
```js
import "cypress-real-events/support";
```
If you are using typescript, also add the following to `cypress/tsconfig.json`
```json
{
"compilerOptions": {
"types": ["cypress", "cypress-real-events"]
}
}
```
## API
The idea of the commands – they should be as similar as possible to cypress default commands (like `cy.type`), but starts with `real` – `cy.realType`.
Here is an overview of the available **real** event commands:
- [cy.realClick](#cyrealclick)
- [cy.realHover](#cyrealhover)
- [cy.realPress](#cyrealpress)
- [cy.realTouch](#cyrealtouch)
- [cy.realType](#cyrealtype)
- [cy.realSwipe](#cyrealswipe)
- [cy.realMouseDown](#cyrealmousedown)
- [cy.realMouseUp](#cyrealmouseup)
## cy.realClick
Fires native system click event.
```jsx
cy.get("button").realClick();
cy.get("button").realClick(options);
```
Example:
```js
cy.get("button").realClick({ position: "topLeft" }); // click on the top left corner of button
cy.get("body").realClick({ x: 100, y: 1240 }); // click by the x & y coordinates relative to the whole window
```
Options:
- `Optional` **button**: \"none\" \| \"left\" \| \"right\" \| \"middle\" \| \"back\" \| \"forward\"
- `Optional` **pointer**: \"mouse\" \| \"pen\"
- `Optional` **x**: undefined \| number ([more about coordinates](#coordinates))
- `Optional` **y**: undefined \| number ([more about coordinates](#coordinates))
- `Optional` **position**: "topLeft" | "top" | "topRight" | "left" | "center" | "right" | "bottomLeft" | "bottom" | "bottomRight"
- `Optional` **scrollBehavior**: "center" | "top" | "bottom" | "nearest" | false
- `Optional` **clickCount**: number
> Make sure that `x` and `y` have a bigger priority than `position`.
## cy.realHover
Fires a real native hover event. Yes, it can test `:hover` preprocessor.
```jsx
cy.get("button").realHover();
cy.get("button").realHover(options);
```
Example:
```js
cy.get("button").realHover({ position: "bottomLeft" }); // hovers over the bottom left corner of button
```
Options:
- `Optional` **pointer**: \"mouse\" \| \"pen\"
- `Optional` **position**: "topLeft" | "top" | "topRight" | "left" | "center" | "right" | "bottomLeft" | "bottom" | "bottomRight"
- `Optional` **scrollBehavior**: "center" | "top" | "bottom" | "nearest" | false
## cy.realPress
Fires native press event. It can fire one key event or the "shortcut" like Shift+Control+M.
Make sure that event is global, it means that it is required to **firstly** focus any control before firing this event.
```jsx
cy.realPress("Tab"); // switch the focus for a11y testing
cy.realPress(["Alt", "Meta", "P"]); // Alt+(Command or Control)+P
```
### Usage
```js
cy.realPress(key);
cy.realPress(key, options);
```
### Parameters:
| Name | Type | Default value | Description |
| --------- | -------------------- | ------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `key` | `string \| string[]` | - | key or keys to press. Should be the same as cypress's [type command argument](https://docs.cypress.io/api/commands/type.html#Arguments). All the keys available [here](https://github.com/dmtrKovalenko/cypress-real-events/blob/main/src/keyCodeDefinitions.ts) |
| `options` | Options | {} | |
Options:
- `Optional` **pointer**: \"mouse\" \| \"pen\"
### cy.realTouch
Fires native system touch event.
```jsx
cy.get("button").realTouch();
cy.get("button").realTouch(options);
```
##### Usage:
```js
cy.get("button").realTouch({ position: "topLeft" }); // touches the top left corner of button
cy.get("body").realTouch({ x: 100, y: 1240 }); // touches the x & y coordinates relative to the whole window
```
Options:
- `Optional` **x**: undefined \| number ([more about coordinates](#coordinates))
- `Optional` **y**: undefined \| number ([more about coordinates](#coordinates))
- `Optional` **position**: "topLeft" | "top" | "topRight" | "left" | "center" | "right" | "bottomLeft" | "bottom" | "bottomRight"
- `Optional` **radius**: undefined \| number **`default`** 1
- `Optional` **radiusX**: undefined \| number **`default`** 1
- `Optional` **radiusY**: undefined \| number **`default`** 1
### cy.realType
Runs a sequence of native press events (via `cy.realPress`). This can be used to simulate real typing.
Make sure that type of event is global. This means that it is not attached to any field.
```js
cy.realType("type any text"); // type any text on the page
cy.get("input").focus();
cy.realType("some text {enter}"); // type into focused field
```
#### Usage:
```js
cy.realType(text);
cy.realType(text, options);
```
#### Parameters:
| Name | Type | Default value | Description |
| --------- | ------- | ------------- | ------------------------------------------------------------------------------------------------------------------------------------- |
| `text` | string | - | text to type. Should be around the same as cypress's type command argument (https://docs.cypress.io/api/commands/type.html#Arguments) |
| `options` | Options | {} | |
Options:
- `Optional` **delay**: undefined \| number **`default`** 30
- `Optional` **log**: undefined \| false \| true **`default`** true
- `Optional` **pressDelay**: undefined \| number **`default`** 10
### cy.realSwipe
Runs native swipe events. It means that **touch events** will be fired. Actually a sequence of `touchStart` -> `touchMove` -> `touchEnd`. It can perfectly swipe drawers and other tools [like this one](https://csb-dhe0i-qj8xxmx8y.vercel.app/).
> Make sure to enable mobile viewport :)
```js
cy.get(".element").realSwipe("toLeft"); // swipes from right to left
cy.get(".element").realSwipe("toRight"); // inverted
```
#### Usage:
```js
cy.realSwipe(direction);
cy.realSwipe(direction, options);
```
#### Parameters:
| Name | Type | Default value | Description |
| ----------- | ------------------------------------------------- | ------------- | ------------------ |
| `direction` | `"toLeft" \| "toTop" \| "toRight" \| "toBottom";` | - | Direction of swipe |
| `options` | Options | {} | |
Options:
- `Optional` **length**: undefined \| number **`default`** 10
- `Optional` **x**: undefined \| number ([more about coordinates](#coordinates))
- `Optional` **y**: undefined \| number ([more about coordinates](#coordinates))
- `Optional` **touchPosition**: "topLeft" | "top" | "topRight" | "left" | "center" | "right" | "bottomLeft" | "bottom" | "bottomRight"
## cy.realMouseDown
Fires native system mouse down event.
```jsx
cy.get("button").realMouseDown();
cy.get("button").realMouseDown(options);
```
Example:
```js
cy.get("button").realMouseDown({ position: "topLeft" }); // click on the top left corner of button
```
Options:
- `Optional` **pointer**: \"mouse\" \| \"pen\"
- `Optional` **position**: "topLeft" | "top" | "topRight" | "left" | "center" | "right" | "bottomLeft" | "bottom" | "bottomRight"
- `Optional` **scrollBehavior**: "center" | "top" | "bottom" | "nearest" | false
- `Optional` **button**: "left" | "middle" | "right" | "back" | "forward" | "none"
## cy.realMouseUp
Fires native system mouse up event.
```jsx
cy.get("button").realMouseUp();
cy.get("button").realMouseUp(options);
```
Example:
```js
cy.get("button").realMouseUp({ position: "topLeft" }); // click on the top left corner of button
```
Options:
- `Optional` **pointer**: \"mouse\" \| \"pen\"
- `Optional` **position**: "topLeft" | "top" | "topRight" | "left" | "center" | "right" | "bottomLeft" | "bottom" | "bottomRight"
- `Optional` **scrollBehavior**: "center" | "top" | "bottom" | "nearest" | false
- `Optional` **button**: "left" | "middle" | "right" | "back" | "forward" | "none"
## cy.realMouseMove
Fires native system mouseMoved event. Moves mouse inside a subject to the provided amount of coordinates from top left corner (adjustable with position option.)
```jsx
cy.get("sector").realMouseMove(x, y);
cy.get("sector").realMouseMove(x, y, options);
```
Example:
```js
cy.get("sector").realMouseMove(50, 50, { position: "center" }); // moves by 50px x and y from center of sector
```
Options:
- `Optional` **position**: "topLeft" | "top" | "topRight" | "left" | "center" | "right" | "bottomLeft" | "bottom" | "bottomRight"
- `Optional` **scrollBehavior**: "center" | "top" | "bottom" | "nearest" | false
## Coordinates
Several commands from this plugin accept `{ x: number, y: number }` coordinates. There is an important note that these coordinates are relative to the whole tab to pass it right to the CDP. For regular elements, we calculate them automatically, but if you need to pass absolute coordinates you will need to provide them yourself.
The easiest way to debug coordinates is to run any real events command and check the logged coordinates by clicking on the command.
![checking coordinates example](./coordinates-log-screenshot.png)
## FAQ
### 1. Why `cy.realHover` hovering state does not show in the visual regression services?
Unfortunately, visual regression services like Happo and Percy do not solve this issue. Their architecture is based on saving dom snapshot, not the screenshot, and then rendering the snapshot on their machines. It means that the hover and focus state will be lost if it won't be serialized manually.
It means that if you will use plain `cy.screenshot` it will take a screenshot with a hovering state because using the browser itself to make a screenshot. Testing hovering state is possible with, for example, [Visual Regression Tracker](https://github.com/Visual-Regression-Tracker/Visual-Regression-Tracker) and [cypress-image-snapshot](https://github.com/jaredpalmer/cypress-image-snapshot).
### 2. When I am doing `cy.realHover` hovering state is not resetting after my checks
Let's take an example. If the real user needs to open menu popover then do check content of popover content and close it the algorithm will be:
1. Hover menu
2. Check the content
3. Put mouse away from the popover
To automate this with cypress, you can do the following
```tsx
cy.get("[aria-label='Test Button']")
.should("have.css", "background-color", "rgb(217, 83, 79)") // check the state before hover
.realHover()
.should("have.css", "background-color", "rgb(201, 48, 44"); // test hovering state
// reset hovering by putting mouse away (e.g. here top left corner of body)
cy.get("body").realHover({ position: "topLeft" });
// hover state reset 🎉
cy.get("[aria-label='Test Button']").should(
"have.css",
"background-color",
"rgb(217, 83, 79)"
);
```
## UX
One problem of the real native system events I need to mention – you will not get an error message if the event wasn't produced. Similar to selenium or playwright – if a javascript event was not fired you will not get a comprehensive error message.
So probably this package should not be used as a replacement for the cypress events, at least for the writing tests experience 🐨
## License
The project is licensed under the terms of [MIT license](./LICENSE)