How to use seededRandom method in Cypress

Best JavaScript code snippet using cypress

Run Cypress automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

3471.js

Source: 3471.js Github

copy
1SeededRandom = function(seed) { // seed may be a string or any type
2  if (! (this instanceof SeededRandom))
3    return new SeededRandom(seed);
4
5  seed = seed || "seed";
6  this.gen = Random.createWithSeeds(seed).alea; // from random.js
7};
8SeededRandom.prototype.next = function() {
9  return this.gen();
10};
11SeededRandom.prototype.nextBoolean = function() {
12  return this.next() >= 0.5;
13};
14SeededRandom.prototype.nextIntBetween = function(min, max) {
15  // inclusive of min and max
16  return Math.floor(this.next() * (max-min+1)) + min;
17};
18SeededRandom.prototype.nextIdentifier = function(optLen) {
19  var letters = [];
20  var len = (typeof optLen === "number" ? optLen : 12);
21  for(var i=0; i<len; i++)
22    letters.push(String.fromCharCode(this.nextIntBetween(97, 122)));
23  var x;
24  return letters.join('');
25};
26SeededRandom.prototype.nextChoice = function(list) {
27  return list[this.nextIntBetween(0, list.length-1)];
28};
29
Full Screen

simulate-random-gestures.js

Source: simulate-random-gestures.js Github

copy
1(()=>{
2
3let seed = 4; // chosen later
4
5const seededRandom = (max = 1, min = 0) => {
6	seed = (seed * 9301 + 49297) % 233280;
7	const rnd = seed / 233280;
8
9	return min + rnd * (max - min);
10};
11
12window.stopSimulatingGestures && window.stopSimulatingGestures();
13window.simulatingGestures = false;
14
15let gestureTimeoutID;
16let periodicGesturesTimeoutID;
17
18let choose = (array)=> array[~~(seededRandom() * array.length)];
19let isAnyMenuOpen = ()=> $(".menu-button.active").length > 0;
20
21let cursor_image = new Image();
22cursor_image.src = "images/cursors/default.png";
23
24const $cursor = $(cursor_image).addClass("user-cursor");
25$cursor.css({
26	position: "absolute",
27	left: 0,
28	top: 0,
29	opacity: 0,
30	zIndex: 5, // @#: z-index
31	pointerEvents: "none",
32	transition: "opacity 0.5s",
33});
34
35window.simulateRandomGesture = (callback, {shift, shiftToggleChance=0.01, secondary, secondaryToggleChance, target=main_canvas}) => {
36	let startWithinRect = target.getBoundingClientRect();
37	let canvasAreaRect = $canvas_area[0].getBoundingClientRect();
38
39	let startMinX = Math.max(startWithinRect.left, canvasAreaRect.left);
40	let startMaxX = Math.min(startWithinRect.right, canvasAreaRect.right);
41	let startMinY = Math.max(startWithinRect.top, canvasAreaRect.top);
42	let startMaxY = Math.min(startWithinRect.bottom, canvasAreaRect.bottom);
43	let startPointX = startMinX + seededRandom() * (startMaxX - startMinX);
44	let startPointY = startMinY + seededRandom() * (startMaxY - startMinY);
45
46	$cursor.appendTo($app);
47	let triggerMouseEvent = (type, point) => {
48		
49		if (isAnyMenuOpen()) {
50			return;
51		}
52
53		const clientX = point.x;
54		const clientY = point.y;
55		const el_over = document.elementFromPoint(clientX, clientY);
56		const do_nothing = !type.match(/move/) && (!el_over || !el_over.closest(".canvas-area"));
57		$cursor.css({
58			display: "block",
59			position: "absolute",
60			left: clientX,
61			top: clientY,
62			opacity: do_nothing ? 0.5 : 1,
63		});
64		if (do_nothing) {
65			return;
66		}
67
68		let event = new $.Event(type, {
69			view: window,
70			bubbles: true,
71			cancelable: true,
72			clientX,
73			clientY,
74			screenX: clientX,
75			screenY: clientY,
76			offsetX: point.x,
77			offsetY: point.y,
78			button: secondary ? 2 : 0,
79			buttons: secondary ? 2 : 1,
80			shiftKey: shift,
81		});
82		$(target).trigger(event);
83	};
84
85	let t = 0;
86	let gestureComponents = [];
87	let numberOfComponents = 5;
88	for (let i = 0; i < numberOfComponents; i += 1) {
89		gestureComponents.push({
90			rx:
91				(seededRandom() * Math.min(canvasAreaRect.width, canvasAreaRect.height)) /
92				2 /
93				numberOfComponents,
94			ry:
95				(seededRandom() * Math.min(canvasAreaRect.width, canvasAreaRect.height)) /
96				2 /
97				numberOfComponents,
98			angularFactor: seededRandom() * 5 - seededRandom(),
99			angularOffset: seededRandom() * 5 - seededRandom(),
100		});
101	}
102	const stepsInGesture = 50;
103	let pointForTimeWithArbitraryStart = (t) => {
104		let point = { x: 0, y: 0 };
105		for (let i = 0; i < gestureComponents.length; i += 1) {
106			let { rx, ry, angularFactor, angularOffset } = gestureComponents[i];
107			point.x +=
108				Math.sin(Math.PI * 2 * ((t / 2) * angularFactor + angularOffset)) *
109				rx;
110			point.y +=
111				Math.cos(Math.PI * 2 * ((t / 2) * angularFactor + angularOffset)) *
112				ry;
113		}
114		return point;
115	};
116	let pointForTime = (t) => {
117		let point = pointForTimeWithArbitraryStart(t);
118		let zeroPoint = pointForTimeWithArbitraryStart(0);
119		point.x -= zeroPoint.x;
120		point.y -= zeroPoint.y;
121		point.x += startPointX;
122		point.y += startPointY;
123		return point;
124	};
125	
126	triggerMouseEvent("pointerenter", pointForTime(t)); // so dynamic cursors follow the simulation cursor
127	triggerMouseEvent("pointerdown", pointForTime(t));
128	let move = () => {
129		t += 1 / stepsInGesture;
130		if (seededRandom() < shiftToggleChance) {
131			shift = !shift;
132		}
133		if (seededRandom() < secondaryToggleChance) {
134			secondary = !secondary;
135		}
136		if (t > 1) {
137			triggerMouseEvent("pointerup", pointForTime(t));
138			
139			$cursor.remove();
140
141			if (callback) {
142				callback();
143			}
144		} else {
145			triggerMouseEvent("pointermove", pointForTime(t));
146			gestureTimeoutID = setTimeout(move, 10);
147		}
148	};
149	triggerMouseEvent("pointerleave", pointForTime(t));
150	move();
151};
152
153window.simulateRandomGesturesPeriodically = () => {
154	window.simulatingGestures = true;
155
156	if (window.drawRandomlySeed != null) {
157		seed = window.drawRandomlySeed;
158	} else {
159		seed = ~~(Math.random() * 5000000);
160	}
161	window.console && console.log("Using seed:", seed);
162	window.console && console.log("Note: Seeds are not guaranteed to work with different versions of the app, but within the same version it should produce the same results given the same starting document & other state & NO interference... except for airbrush randomness");
163	window.console && console.log(`To use this seed:
164		
165		window.drawRandomlySeed = ${seed};
166		document.body.style.width = "${getComputedStyle(document.body).width}";
167		document.body.style.height = "${getComputedStyle(document.body).height}";
168		simulateRandomGesturesPeriodically();
169		delete window.drawRandomlySeed;
170		
171	`);
172
173	let delayBetweenGestures = 500;
174	let shiftStart = false;
175	let shiftStartToggleChance = 0.1;
176	let shiftToggleChance = 0.001;
177	let secondaryStart = false;
178	let secondaryStartToggleChance = 0.1;
179	let secondaryToggleChance = 0.001;
180	let switchToolsChance = 0.5;
181	let multiToolsChance = 0.8;
182	let pickColorChance = 0.5;
183	let pickToolOptionsChance = 0.8;
184	let scrollChance = 0.2;
185	let dragSelectionChance = 0.8;
186
187	// scroll randomly absolutely initially so the starting scroll doesn't play into whether a seed reproduces
188	$canvas_area.scrollTop($canvas_area.width() * seededRandom());
189	$canvas_area.scrollLeft($canvas_area.height() * seededRandom());
190	
191	let _simulateRandomGesture = (callback)=> {
192		window.simulateRandomGesture(callback, {
193			shift: shiftStart,
194			shiftToggleChance,
195			secondary: secondaryStart,
196			secondaryToggleChance
197		});
198	};
199	let waitThenGo = () => {
200		// @TODO: a button to stop it as well (maybe make "stop drawing randomly" a link button?)
201		$status_text.text("Press Esc to stop drawing randomly.");
202		if (isAnyMenuOpen()) {
203			periodicGesturesTimeoutID = setTimeout(waitThenGo, 50);
204			return;
205		}
206
207		if (seededRandom() < shiftStartToggleChance) {
208			shiftStart = !shiftStart;
209		}
210		if (seededRandom() < secondaryStartToggleChance) {
211			secondaryStart = !secondaryStart;
212		}
213		if (seededRandom() < switchToolsChance) {
214			let multiToolsPlz = seededRandom() < multiToolsChance;
215			$(choose($(".tool, tool-button"))).trigger($.Event("click", {shiftKey: multiToolsPlz}));
216		}
217		if (seededRandom() < pickToolOptionsChance) {
218			$(choose($(".tool-options *"))).trigger("click");
219		}
220		if (seededRandom() < pickColorChance) {
221			// @TODO: maybe these should respond to a normal click?
222			let secondary = seededRandom() < 0.5;
223			const colorButton = choose($(".swatch, .color-button"));
224			$(colorButton)
225				.trigger($.Event("pointerdown", {button: secondary ? 2 : 0}))
226				.trigger($.Event("click", {button: secondary ? 2 : 0}))
227				.trigger($.Event("pointerup", {button: secondary ? 2 : 0}));
228		}
229		if (seededRandom() < scrollChance) {
230			let scrollAmount = (seededRandom() * 2 - 1) * 700;
231			if (seededRandom() < 0.5) {
232				$canvas_area.scrollTop($canvas_area.scrollTop() + scrollAmount);
233			} else {
234				$canvas_area.scrollLeft($canvas_area.scrollLeft() + scrollAmount);
235			}
236		}
237		periodicGesturesTimeoutID = setTimeout(() => {
238			_simulateRandomGesture(()=> {
239				if (selection && seededRandom() < dragSelectionChance) {
240					window.simulateRandomGesture(waitThenGo, {
241						shift: shiftStart,
242						shiftToggleChance,
243						secondary: secondaryStart,
244						secondaryToggleChance,
245						target: selection.canvas
246					});
247				} else {
248					waitThenGo();
249				}
250			});
251		}, delayBetweenGestures);
252	};
253	_simulateRandomGesture(waitThenGo);
254};
255
256window.stopSimulatingGestures = () => {
257	if (window.simulatingGestures) {
258		clearTimeout(gestureTimeoutID);
259		clearTimeout(periodicGesturesTimeoutID);
260		window.simulatingGestures = false;
261		$status_text.default();
262		$cursor.remove();
263		cancel();
264	}
265	document.body.style.width = "";
266	document.body.style.height = "";
267};
268
269})();
270
Full Screen

seeded_random.js

Source: seeded_random.js Github

copy
1SeededRandom = function(seed) { // seed may be a string or any type
2  if (! (this instanceof SeededRandom))
3    return new SeededRandom(seed);
4
5  seed = seed || "seed";
6  this.gen = Random.createWithSeeds(seed).alea; // from random.js
7};
8SeededRandom.prototype.next = function() {
9  return this.gen();
10};
11SeededRandom.prototype.nextBoolean = function() {
12  return this.next() >= 0.5;
13};
14SeededRandom.prototype.nextIntBetween = function(min, max) {
15  // inclusive of min and max
16  return Math.floor(this.next() * (max-min+1)) + min;
17};
18SeededRandom.prototype.nextIdentifier = function(optLen) {
19  var letters = [];
20  var len = (typeof optLen === "number" ? optLen : 12);
21  for(var i=0; i<len; i++)
22    letters.push(String.fromCharCode(this.nextIntBetween(97, 122)));
23  var x;
24  return letters.join('');
25};
26SeededRandom.prototype.nextChoice = function(list) {
27  return list[this.nextIntBetween(0, list.length-1)];
28};
29
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Run JavaScript Tests on LambdaTest Cloud Grid

Execute automation tests with Cypress on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)