-
-
Notifications
You must be signed in to change notification settings - Fork 68
/
realSwipe.ts
156 lines (141 loc) · 3.92 KB
/
realSwipe.ts
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
import { fireCdpCommand } from "../fireCdpCommand";
import {
getCypressElementCoordinates,
Position,
} from "../getCypressElementCoordinates";
export type SwipeDirection = "toLeft" | "toTop" | "toRight" | "toBottom";
export interface RealSwipeOptions {
/**
* The point of the element where touch event will be executed
* @example cy.realSwipe({ position: "topLeft" })
*/
touchPosition?: Position;
/** X coordinate to click, relative to the Element. Overrides `position`.
* @example
* cy.get("canvas").realSwipe({ x: 100, y: 115 })
* cy.get("body").realSwipe({ x: 11, y: 12 }) // global touch by coordinates
*/
x?: number;
/** X coordinate to click, relative to the Element. Overrides `position`.
* @example
* cy.get("canvas").realSwipe({ x: 100, y: 115 })
* cy.get("body").realSwipe({ x: 11, y: 12 }) // global touch by coordinates
*/
y?: number;
/** Length of swipe (in pixels)
* @default 10
* @example
* cy.get(".drawer").realSwipe("toLeft", { length: 50 })
*/
length?: number;
/**
* Swipe step (how often new touch move will be generated).
* Must be less than or equal options.length
* @default 10
* cy.get(".drawer").realSwipe("toLeft", { step: 5 })
*/
step?: number;
/**
* Delay between touchStart and touchMove events (ms)
* @default 0
* cy.get(".drawer").realSwipe("toLeft", { touchMoveDelay: 300 })
*/
touchMoveDelay?: number;
}
async function forEachSwipePosition(
{
length,
step,
startPosition,
direction,
}: {
length: number;
step: number;
direction: SwipeDirection;
startPosition: { x: number; y: number };
},
onStep: (pos: { x: number; y: number }) => Promise<void>,
) {
if (length < step) {
throw new Error(
"cy.realSwipe: options.length can not be smaller than options.step",
);
}
const getPositionByDirection: Record<
SwipeDirection,
(step: number) => { x: number; y: number }
> = {
toTop: (step) => ({
x: startPosition.x,
y: startPosition.y - step,
}),
toBottom: (step) => ({
x: startPosition.x,
y: startPosition.y + step,
}),
toLeft: (step) => ({
x: startPosition.x - step,
y: startPosition.y,
}),
toRight: (step) => ({
x: startPosition.x + step,
y: startPosition.y,
}),
};
for (let i = 0; i <= length; i += step) {
await onStep(getPositionByDirection[direction](i));
}
}
export async function realSwipe(
subject: JQuery,
direction: SwipeDirection,
options: RealSwipeOptions = {},
) {
const position =
options.x && options.y
? { x: options.x, y: options.y }
: options.touchPosition;
const length = options.length ?? 10;
const step = options.step ?? 10;
const elementCoordinates = getCypressElementCoordinates(subject, position);
const startPosition = { x: elementCoordinates.x, y: elementCoordinates.y };
const log = Cypress.log({
$el: subject,
name: "realSwipe",
consoleProps: () => ({
"Applied To": subject.get(0),
"Swipe Length": length,
"Swipe Step": step,
}),
});
log.snapshot("before");
await fireCdpCommand("Input.dispatchTouchEvent", {
type: "touchStart",
touchPoints: [startPosition],
});
if (options.touchMoveDelay) {
// cy.wait() can't be used here since we are in a command that returns a promise.
// Read more: https://on.cypress.io/returning-promise-and-commands-in-another-command
await wait(options.touchMoveDelay);
}
await forEachSwipePosition(
{
length,
step,
direction,
startPosition,
},
(position) =>
fireCdpCommand("Input.dispatchTouchEvent", {
type: "touchMove",
touchPoints: [position],
}),
);
await fireCdpCommand("Input.dispatchTouchEvent", {
type: "touchEnd",
touchPoints: [],
});
log.snapshot("after").end();
return subject;
}
const wait = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));