Gathering detailed insights and metrics for cypress-real-events
Gathering detailed insights and metrics for cypress-real-events
Gathering detailed insights and metrics for cypress-real-events
Gathering detailed insights and metrics for cypress-real-events
npm install cypress-real-events
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
767 Stars
262 Commits
68 Forks
14 Watching
33 Branches
29 Contributors
Updated on 19 Nov 2024
Minified
Minified + Gzipped
HTML (76.14%)
TypeScript (23.21%)
JavaScript (0.66%)
Cumulative downloads
Total Downloads
Last day
-9.6%
164,328
Compared to previous day
Last week
7.8%
928,071
Compared to previous week
Last month
6.3%
3,751,368
Compared to previous month
Last year
49.7%
40,185,408
Compared to previous year
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.
Thanks to Chrome 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.
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.
Here is a simple test that can be written with native events:
1it("tests real events", () => { 2 cy.get("input").realClick(); // perform a native real click on the field 3 cy.realType("cypress real event"); // fires native system keypress events and fills the field 4 cy.realPress("Tab"); // native tab click switches the focus 5 cy.get("input").realMouseDown(); // perform a native mouse press on the field 6 cy.get("input").realMouseUp(); // perform a native mouse release on the field 7 cy.focused().realHover(); // hovers over the new focused element 8 cy.contains("some text in the hovered popover"); 9});
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.
1import "cypress-real-events";
To include TypeScript declarations, add "cypress-real-events"
to the types
section of your tsconfig.json file:
1{ 2 "compilerOptions": { 3 "types": ["cypress", "node", "cypress-real-events"] 4 } 5}
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:
Fires native system click event.
1cy.get("button").realClick(); 2cy.get("button").realClick(options);
Example:
1cy.get("button").realClick({ position: "topLeft" }); // click on the top left corner of button 2cy.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)Optional
y: undefined | number (more about coordinates)Optional
position: "topLeft" | "top" | "topRight" | "left" | "center" | "right" | "bottomLeft" | "bottom" | "bottomRight"Optional
scrollBehavior: "center" | "top" | "bottom" | "nearest" | falseOptional
clickCount: numberMake sure that
x
andy
have a bigger priority thanposition
.
Fires a real native hover event. Yes, it can test :hover
preprocessor.
1cy.get("button").realHover(); 2cy.get("button").realHover(options);
Example:
1cy.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" | falseFires 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.
1cy.realPress("Tab"); // switch the focus for a11y testing 2cy.realPress(["Alt", "Meta", "P"]); // Alt+(Command or Control)+P
1cy.realPress(key);
2cy.realPress(key, options);
Name | Type | Default value | Description |
---|---|---|---|
key | string | string[] | - | key or keys to press. Should be the same as cypress's type command argument. All the keys available here |
options | Options | {} |
Options:
Optional
pointer: "mouse" | "pen"Fires native system touch event.
1cy.get("button").realTouch(); 2cy.get("button").realTouch(options);
1cy.get("button").realTouch({ position: "topLeft" }); // touches the top left corner of button 2cy.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)Optional
y: undefined | number (more about coordinates)Optional
position: "topLeft" | "top" | "topRight" | "left" | "center" | "right" | "bottomLeft" | "bottom" | "bottomRight"Optional
radius: undefined | number default
1Optional
radiusX: undefined | number default
1Optional
radiusY: undefined | number default
1Runs 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.
1cy.realType("type any text"); // type any text on the page 2 3cy.get("input").focus(); 4cy.realType("some text {enter}"); // type into focused field
1cy.realType(text);
2cy.realType(text, options);
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. All the keys available here |
options | Options | {} |
Options:
Optional
delay: undefined | number default
30Optional
log: undefined | false | true default
trueOptional
pressDelay: undefined | number default
10Runs 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.
Make sure to enable mobile viewport :)
1cy.get(".element").realSwipe("toLeft"); // swipes from right to left 2cy.get(".element").realSwipe("toRight"); // inverted
1cy.realSwipe(direction);
2cy.realSwipe(direction, options);
Name | Type | Default value | Description |
---|---|---|---|
direction | "toLeft" | "toTop" | "toRight" | "toBottom"; | - | Direction of swipe |
options | Options | {} |
Options:
Optional
length: undefined | number default
10Optional
step: undefined | number default
10Optional
x: undefined | number (more about coordinates)Optional
y: undefined | number (more about coordinates)Optional
touchPosition: "topLeft" | "top" | "topRight" | "left" | "center" | "right" | "bottomLeft" | "bottom" | "bottomRight"Optional
touchMoveDelay: undefined | number default
0Fires native system mouse down event.
1cy.get("button").realMouseDown(); 2cy.get("button").realMouseDown(options);
Example:
1cy.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" | falseOptional
button: "left" | "middle" | "right" | "back" | "forward" | "none"Fires native system mouse up event.
1cy.get("button").realMouseUp(); 2cy.get("button").realMouseUp(options);
Example:
1cy.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" | falseOptional
button: "left" | "middle" | "right" | "back" | "forward" | "none"Fires native system mouseMoved event. Moves mouse inside a subject to the provided amount of coordinates from top left corner (adjustable with position option.)
1cy.get("sector").realMouseMove(x, y);
2cy.get("sector").realMouseMove(x, y, options);
Example:
1cy.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" | falseFires native system mouse wheel event, scrolls the page.
1cy.get("div").realMouseWheel(options);
Example:
1cy.get("div").realMouseWheel({ deltaY: 100 }) // Scroll down, mouse will be positioned at centered by default. 2cy.get("div").realMouseWheel({ deltaY: -100 }) // Scroll up, mouse will be positioned at centered by default. 3cy.get("div").realMouseWheel({ deltaX: 500 }) // Scroll right, mouse will be positioned at centered by default. 4cy.get("div").realMouseWheel({ deltaX: -500 }) // Scroll left, mouse will be positioned at centered by default. 5cy.get("div").realMouseWheel({ deltaY: 100, deltaX: 100 }) // Scroll right and down, mouse will be positioned at centered by default.
Options:
Optional
scrollBehavior: "center" | "top" | "bottom" | "nearest" | falseOptional
deltaX: X delta in CSS pixels for mouse wheel event (default: 0). Positive values emulate a scroll right and negative values emulate scroll left event.Optional
deltaY: Y delta in CSS pixels for mouse wheel event (default: 0). Positive values emulate a scroll down and negative values emulate a scroll up event.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.
cy.realHover
hovering state does not show in the visual regression services?Unfortunately, neither visual regression services like Happo and Percy nor plain cy.screenshot do not allow to test the hovering state. The hovering state is very different from any kind of js and css so it is not possible to capture it using dom snapshotting (like visual regression services do) and the screenshooting as well because cypress core itself is preventing hovering state in the screenshots.
cy.realHover
hovering state is not resetting after my checksLet'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:
To automate this with cypress, you can do the following
1cy.get("[aria-label='Test Button']") 2 .should("have.css", "background-color", "rgb(217, 83, 79)") // check the state before hover 3 .realHover() 4 .should("have.css", "background-color", "rgb(201, 48, 44"); // test hovering state 5 6// reset hovering by putting mouse away (e.g. here top left corner of body) 7cy.get("body").realHover({ position: "topLeft" }); 8// hover state reset 🎉 9cy.get("[aria-label='Test Button']").should( 10 "have.css", 11 "background-color", 12 "rgb(217, 83, 79)" 13);
Sometimes when there are unsaved changes in a webform, and you leave the page, the web application asks you due the onbeforeunload event
if you really want to leaf the page and loose the changes.
You can try it on this Demo Page.
In "normal" Cypress tests, these popup windows do not appear. This is because no "real" user actions are performed on the application. See this page for more information about the beforeunload event. But when they appear, they block the whole test execution, and you have to handle them explicitly. Gleb Bahmutov writes about this behaviour and possible solutions in this Blog Post.
Now when you use this real-events
plugin and perform a realEvent
on your application, the browser thinks there happened a real user interaction.
From now on your test is in an active interaction
state, which allows the application to use all the features listed here.
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 🐨
The project is licensed under the terms of MIT license
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
Found 4/12 approved changesets -- score normalized to 3
Reason
0 commit(s) and 2 issue activity found in the last 90 days -- score normalized to 1
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
14 existing vulnerabilities detected
Details
Score
Last Scanned on 2024-11-25
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More