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:
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");
});
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.
import "cypress-real-events/support";
If you are using typescript, also add the following to cypress/tsconfig.json
{
"compilerOptions": {
"types": ["cypress", "cypress-real-events"]
}
}
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
- cy.realHover
- cy.realPress
- cy.realTouch
- cy.realType
- cy.realSwipe
- cy.realMouseDown
- cy.realMouseUp
Fires native system click event.
cy.get("button").realClick();
cy.get("button").realClick(options);
Example:
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)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: number
Make sure that
x
andy
have a bigger priority thanposition
.
Fires a real native hover event. Yes, it can test :hover
preprocessor.
cy.get("button").realHover();
cy.get("button").realHover(options);
Example:
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
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.
cy.realPress("Tab"); // switch the focus for a11y testing
cy.realPress(["Alt", "Meta", "P"]); // Alt+(Command or Control)+P
cy.realPress(key);
cy.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.
cy.get("button").realTouch();
cy.get("button").realTouch(options);
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)Optional
y: undefined | number (more about coordinates)Optional
position: "topLeft" | "top" | "topRight" | "left" | "center" | "right" | "bottomLeft" | "bottom" | "bottomRight"Optional
radius: undefined | numberdefault
1Optional
radiusX: undefined | numberdefault
1Optional
radiusY: undefined | numberdefault
1
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.
cy.realType("type any text"); // type any text on the page
cy.get("input").focus();
cy.realType("some text {enter}"); // type into focused field
cy.realType(text);
cy.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) |
options |
Options | {} |
Options:
Optional
delay: undefined | numberdefault
30Optional
log: undefined | false | truedefault
trueOptional
pressDelay: undefined | numberdefault
10
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.
Make sure to enable mobile viewport :)
cy.get(".element").realSwipe("toLeft"); // swipes from right to left
cy.get(".element").realSwipe("toRight"); // inverted
cy.realSwipe(direction);
cy.realSwipe(direction, options);
Name | Type | Default value | Description |
---|---|---|---|
direction |
"toLeft" | "toTop" | "toRight" | "toBottom"; |
- | Direction of swipe |
options |
Options | {} |
Options:
Optional
length: undefined | numberdefault
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"
Fires native system mouse down event.
cy.get("button").realMouseDown();
cy.get("button").realMouseDown(options);
Example:
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" | falseOptional
button: "left" | "middle" | "right" | "back" | "forward" | "none"
Fires native system mouse up event.
cy.get("button").realMouseUp();
cy.get("button").realMouseUp(options);
Example:
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" | 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.)
cy.get("sector").realMouseMove(x, y);
cy.get("sector").realMouseMove(x, y, options);
Example:
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
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.
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 and cypress-image-snapshot.
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:
- Hover menu
- Check the content
- Put mouse away from the popover
To automate this with cypress, you can do the following
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)"
);
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