Best JavaScript code snippet using cypress
simulate-random-gestures.js
Source:simulate-random-gestures.js
...8window.stopSimulatingGestures && window.stopSimulatingGestures();9window.simulatingGestures = false;10let gestureTimeoutID;11let periodicGesturesTimeoutID;12let choose = (array)=> array[~~(seededRandom() * array.length)];13let isAnyMenuOpen = ()=> $(".menu-button.active").length > 0;14let cursor_image = new Image();15cursor_image.src = "images/cursors/default.png";16const $cursor = $(cursor_image).addClass("user-cursor");17$cursor.css({18 position: "absolute",19 left: 0,20 top: 0,21 opacity: 0,22 zIndex: 5, // @#: z-index23 pointerEvents: "none",24 transition: "opacity 0.5s",25});26window.simulateRandomGesture = (callback, {shift, shiftToggleChance=0.01, secondary, secondaryToggleChance, target=main_canvas}) => {27 let startWithinRect = target.getBoundingClientRect();28 let canvasAreaRect = $canvas_area[0].getBoundingClientRect();29 let startMinX = Math.max(startWithinRect.left, canvasAreaRect.left);30 let startMaxX = Math.min(startWithinRect.right, canvasAreaRect.right);31 let startMinY = Math.max(startWithinRect.top, canvasAreaRect.top);32 let startMaxY = Math.min(startWithinRect.bottom, canvasAreaRect.bottom);33 let startPointX = startMinX + seededRandom() * (startMaxX - startMinX);34 let startPointY = startMinY + seededRandom() * (startMaxY - startMinY);35 $cursor.appendTo($app);36 let triggerMouseEvent = (type, point) => {37 38 if (isAnyMenuOpen()) {39 return;40 }41 const clientX = point.x;42 const clientY = point.y;43 const el_over = document.elementFromPoint(clientX, clientY);44 const do_nothing = !type.match(/move/) && (!el_over || !el_over.closest(".canvas-area"));45 $cursor.css({46 display: "block",47 position: "absolute",48 left: clientX,49 top: clientY,50 opacity: do_nothing ? 0.5 : 1,51 });52 if (do_nothing) {53 return;54 }55 let event = new $.Event(type, {56 view: window,57 bubbles: true,58 cancelable: true,59 clientX,60 clientY,61 screenX: clientX,62 screenY: clientY,63 offsetX: point.x,64 offsetY: point.y,65 button: secondary ? 2 : 0,66 buttons: secondary ? 2 : 1,67 shiftKey: shift,68 });69 $(target).trigger(event);70 };71 let t = 0;72 let gestureComponents = [];73 let numberOfComponents = 5;74 for (let i = 0; i < numberOfComponents; i += 1) {75 gestureComponents.push({76 rx:77 (seededRandom() * Math.min(canvasAreaRect.width, canvasAreaRect.height)) /78 2 /79 numberOfComponents,80 ry:81 (seededRandom() * Math.min(canvasAreaRect.width, canvasAreaRect.height)) /82 2 /83 numberOfComponents,84 angularFactor: seededRandom() * 5 - seededRandom(),85 angularOffset: seededRandom() * 5 - seededRandom(),86 });87 }88 const stepsInGesture = 50;89 let pointForTimeWithArbitraryStart = (t) => {90 let point = { x: 0, y: 0 };91 for (let i = 0; i < gestureComponents.length; i += 1) {92 let { rx, ry, angularFactor, angularOffset } = gestureComponents[i];93 point.x +=94 Math.sin(Math.PI * 2 * ((t / 2) * angularFactor + angularOffset)) *95 rx;96 point.y +=97 Math.cos(Math.PI * 2 * ((t / 2) * angularFactor + angularOffset)) *98 ry;99 }100 return point;101 };102 let pointForTime = (t) => {103 let point = pointForTimeWithArbitraryStart(t);104 let zeroPoint = pointForTimeWithArbitraryStart(0);105 point.x -= zeroPoint.x;106 point.y -= zeroPoint.y;107 point.x += startPointX;108 point.y += startPointY;109 return point;110 };111 112 triggerMouseEvent("pointerenter", pointForTime(t)); // so dynamic cursors follow the simulation cursor113 triggerMouseEvent("pointerdown", pointForTime(t));114 let move = () => {115 t += 1 / stepsInGesture;116 if (seededRandom() < shiftToggleChance) {117 shift = !shift;118 }119 if (seededRandom() < secondaryToggleChance) {120 secondary = !secondary;121 }122 if (t > 1) {123 triggerMouseEvent("pointerup", pointForTime(t));124 125 $cursor.remove();126 if (callback) {127 callback();128 }129 } else {130 triggerMouseEvent("pointermove", pointForTime(t));131 gestureTimeoutID = setTimeout(move, 10);132 }133 };134 triggerMouseEvent("pointerleave", pointForTime(t));135 move();136};137window.simulateRandomGesturesPeriodically = () => {138 window.simulatingGestures = true;139 if (window.drawRandomlySeed != null) {140 seed = window.drawRandomlySeed;141 } else {142 seed = ~~(Math.random() * 5000000);143 }144 window.console && console.log("Using seed:", seed);145 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");146 window.console && console.log(`To use this seed:147 148 window.drawRandomlySeed = ${seed};149 document.body.style.width = "${getComputedStyle(document.body).width}";150 document.body.style.height = "${getComputedStyle(document.body).height}";151 simulateRandomGesturesPeriodically();152 delete window.drawRandomlySeed;153 154 `);155 let delayBetweenGestures = 500;156 let shiftStart = false;157 let shiftStartToggleChance = 0.1;158 let shiftToggleChance = 0.001;159 let secondaryStart = false;160 let secondaryStartToggleChance = 0.1;161 let secondaryToggleChance = 0.001;162 let switchToolsChance = 0.5;163 let multiToolsChance = 0.8;164 let pickColorChance = 0.5;165 let pickToolOptionsChance = 0.8;166 let scrollChance = 0.2;167 let dragSelectionChance = 0.8;168 // scroll randomly absolutely initially so the starting scroll doesn't play into whether a seed reproduces169 $canvas_area.scrollTop($canvas_area.width() * seededRandom());170 $canvas_area.scrollLeft($canvas_area.height() * seededRandom());171 172 let _simulateRandomGesture = (callback)=> {173 window.simulateRandomGesture(callback, {174 shift: shiftStart,175 shiftToggleChance,176 secondary: secondaryStart,177 secondaryToggleChance178 });179 };180 let waitThenGo = () => {181 // @TODO: a button to stop it as well (maybe make "stop drawing randomly" a link button?)182 $status_text.text("Press Esc to stop drawing randomly.");183 if (isAnyMenuOpen()) {184 periodicGesturesTimeoutID = setTimeout(waitThenGo, 50);185 return;186 }187 if (seededRandom() < shiftStartToggleChance) {188 shiftStart = !shiftStart;189 }190 if (seededRandom() < secondaryStartToggleChance) {191 secondaryStart = !secondaryStart;192 }193 if (seededRandom() < switchToolsChance) {194 let multiToolsPlz = seededRandom() < multiToolsChance;195 $(choose($(".tool, tool-button"))).trigger($.Event("click", {shiftKey: multiToolsPlz}));196 }197 if (seededRandom() < pickToolOptionsChance) {198 $(choose($(".tool-options *"))).trigger("click");199 }200 if (seededRandom() < pickColorChance) {201 // @TODO: maybe these should respond to a normal click?202 let secondary = seededRandom() < 0.5;203 const colorButton = choose($(".swatch, .color-button"));204 $(colorButton)205 .trigger($.Event("pointerdown", {button: secondary ? 2 : 0}))206 .trigger($.Event("click", {button: secondary ? 2 : 0}))207 .trigger($.Event("pointerup", {button: secondary ? 2 : 0}));208 }209 if (seededRandom() < scrollChance) {210 let scrollAmount = (seededRandom() * 2 - 1) * 700;211 if (seededRandom() < 0.5) {212 $canvas_area.scrollTop($canvas_area.scrollTop() + scrollAmount);213 } else {214 $canvas_area.scrollLeft($canvas_area.scrollLeft() + scrollAmount);215 }216 }217 periodicGesturesTimeoutID = setTimeout(() => {218 _simulateRandomGesture(()=> {219 if (selection && seededRandom() < dragSelectionChance) {220 window.simulateRandomGesture(waitThenGo, {221 shift: shiftStart,222 shiftToggleChance,223 secondary: secondaryStart,224 secondaryToggleChance,225 target: selection.canvas226 });227 } else {228 waitThenGo();229 }230 });231 }, delayBetweenGestures);232 };233 _simulateRandomGesture(waitThenGo);...
random.test.js
Source:random.test.js
1import _ from "lodash";2import { SeededRandom } from "./random";3describe("SeededRandom", () => {4 it("should generate ints", () => {5 const rng = new SeededRandom(123);6 _.times(100, () => {7 const next = rng.next();8 expect(Number.isInteger(next)).toEqual(true);9 });10 });11 it("should generate floats", () => {12 const rng = new SeededRandom(123);13 _.times(100, () => {14 const next = rng.nextFloat();15 expect(Number.isInteger(next)).toEqual(false);16 expect(next > 0).toEqual(true);17 expect(next < 1).toEqual(true);18 });19 });20 it("should generate number given the same seed", () => {21 const rng1 = new SeededRandom(123);22 const rng2 = new SeededRandom(123);23 expect(rng1.next() === rng2.next()).toEqual(true);24 });25 it("should generate number given different seeds", () => {26 const rng1 = new SeededRandom(123);27 const rng2 = new SeededRandom(456);28 expect(rng1.next() !== rng2.next()).toEqual(true);29 });...
3471.js
Source:3471.js
1SeededRandom = function(seed) { // seed may be a string or any type2 if (! (this instanceof SeededRandom))3 return new SeededRandom(seed);4 seed = seed || "seed";5 this.gen = Random.createWithSeeds(seed).alea; // from random.js6};7SeededRandom.prototype.next = function() {8 return this.gen();9};10SeededRandom.prototype.nextBoolean = function() {11 return this.next() >= 0.5;12};13SeededRandom.prototype.nextIntBetween = function(min, max) {14 // inclusive of min and max15 return Math.floor(this.next() * (max-min+1)) + min;16};17SeededRandom.prototype.nextIdentifier = function(optLen) {18 var letters = [];19 var len = (typeof optLen === "number" ? optLen : 12);20 for(var i=0; i<len; i++)21 letters.push(String.fromCharCode(this.nextIntBetween(97, 122)));22 var x;23 return letters.join('');24};25SeededRandom.prototype.nextChoice = function(list) {26 return list[this.nextIntBetween(0, list.length-1)];...
seeded_random.js
Source:seeded_random.js
1SeededRandom = function(seed) { // seed may be a string or any type2 if (! (this instanceof SeededRandom))3 return new SeededRandom(seed);4 seed = seed || "seed";5 this.gen = Random.createWithSeeds(seed).alea; // from random.js6};7SeededRandom.prototype.next = function() {8 return this.gen();9};10SeededRandom.prototype.nextBoolean = function() {11 return this.next() >= 0.5;12};13SeededRandom.prototype.nextIntBetween = function(min, max) {14 // inclusive of min and max15 return Math.floor(this.next() * (max-min+1)) + min;16};17SeededRandom.prototype.nextIdentifier = function(optLen) {18 var letters = [];19 var len = (typeof optLen === "number" ? optLen : 12);20 for(var i=0; i<len; i++)21 letters.push(String.fromCharCode(this.nextIntBetween(97, 122)));22 var x;23 return letters.join('');24};25SeededRandom.prototype.nextChoice = function(list) {26 return list[this.nextIntBetween(0, list.length-1)];...
Using AI Code Generation
1describe('test', () => {2 it('test', () => {3 cy.get('#tsf > div:nth-child(2) > div > div.RNNXgb > div > div.a4bIc > input').type('test')4 cy.get('#tsf > div:nth-child(2) > div > div.FPdoLc.VlcLAe > center > input.gNO89b').click()5 })6})7Cypress.Commands.add('seededRandom', (seed) => {8 const x = Math.sin(seed++) * 100009 return x - Math.floor(x)10})11import './commands'12{13 "env": {14 }15}16module.exports = (on, config) => {17 on('task', {18 seededRandom () {19 }20 })21}22describe('test', () => {23 it('test', () => {24 cy.visit('/')25 cy.get('#tsf > div:nth-child(2) > div > div.RNNXgb > div > div.a4bIc > input').type('test')26 cy.get('#tsf > div:nth-child(2) > div > div.FPdoLc.VlcLAe > center > input.gNO89b').click()27 })28})29import '@cypress/code-coverage/support'30"jest": {31 "src/**/*.{js,jsx,ts,tsx}",
Using AI Code Generation
1describe('Testing seededRandom', () => {2 it('Testing seededRandom', () => {3 cy.get('#seed-btn').click()4 cy.get('#random-number').should('have.text', '0.7151893663724195')5 })6})
Using AI Code Generation
1cy.then(() => {2 cy.window().then((win) => {3 win.Math.random = () => {4 return 0.5;5 };6 });7});8cy.then(() => {9 cy.window().then((win) => {10 win.Math.random = () => {11 return 0.5;12 };13 });14});15cy.then(() => {16 cy.window().then((win) => {17 win.Math.random = () => {18 return 0.5;19 };20 });21});22cy.then(() => {23 cy.window().then((win) => {24 win.Math.random = () => {25 return 0.5;26 };27 });28});29cy.then(() => {30 cy.window().then((win) => {31 win.Math.random = () => {32 return 0.5;33 };34 });35});36cy.then(() => {37 cy.window().then((win) => {38 win.Math.random = () => {39 return 0.5;40 };41 });42});43cy.then(() => {44 cy.window().then((win) => {45 win.Math.random = () => {46 return 0.5;47 };48 });49});50cy.then(() => {51 cy.window().then((win) => {52 win.Math.random = () => {53 return 0.5;54 };55 });56});57cy.then(() => {58 cy.window().then((win) => {59 win.Math.random = () => {60 return 0.5;61 };62 });63});64cy.then(() => {65 cy.window().then((win) => {66 win.Math.random = () => {67 return 0.5;68 };69 });70});71cy.then(() => {72 cy.window().then((win) => {73 win.Math.random = () => {74 return 0.5;75 };76 });77});
Using AI Code Generation
1cy.log(cy.seededRandom(1, 10));2cy.log(cy.seededRandom(1, 10));3cy.log(cy.seededRandom(1, 10));4cy.log(cy.seededRandom(1, 10));5cy.log(cy.seededRandom(1, 10));6cy.log(cy.seededRandom(1, 10));7cy.log(cy.seededRandom(1, 10));8cy.log(cy.seededRandom(1, 10));9cy.log(cy.seededRandom(1, 10));10cy.log(cy.seededRandom(1, 10));11cy.log(cy.seededRandom(1, 10));12cy.log(cy.seededRandom(1, 10));13cy.log(cy.seededRandom(1, 10));14cy.log(cy.seededRandom(1, 10));
Using AI Code Generation
1cy.seededRandom().then((seed) => {2 let random = Math.random(seed);3 console.log(random);4});5cy.seededRandom().then((seed) => {6 let random = Math.random(seed);7 console.log(random);8});9cy.seededRandomInt().then((seed) => {10 let random = Math.random(seed);11 console.log(random);12});13cy.seededRandomFloat().then((seed) => {14 let random = Math.random(seed);15 console.log(random);16});17cy.seededRandomString().then((seed) => {18 let random = Math.random(seed);19 console.log(random);20});21cy.seededRandomDate().then((seed) => {22 let random = Math.random(seed);23 console.log(random);24});25cy.seededRandomEmail().then((seed) => {26 let random = Math.random(seed);27 console.log(random);28});29cy.seededRandomColor().then((seed) => {30 let random = Math.random(seed);31 console.log(random);32});33cy.seededRandomBoolean().then((seed) => {34 let random = Math.random(seed);35 console.log(random);36});37cy.seededRandomArray().then((seed) => {38 let random = Math.random(seed);39 console.log(random);40});
Using AI Code Generation
1cy.then(() => {2 const seededRandom = Cypress._.random(0, 1000, true);3 const randomNumber = seededRandom();4 const randomNumber2 = seededRandom();5 const randomNumber3 = seededRandom();6 const randomNumber4 = seededRandom();7 const randomNumber5 = seededRandom();8 const randomNumber6 = seededRandom();9 const randomNumber7 = seededRandom();10 const randomNumber8 = seededRandom();11 const randomNumber9 = seededRandom();12 const randomNumber10 = seededRandom();13 const randomNumber11 = seededRandom();14 const randomNumber12 = seededRandom();15 const randomNumber13 = seededRandom();16 const randomNumber14 = seededRandom();17 const randomNumber15 = seededRandom();18 const randomNumber16 = seededRandom();19 const randomNumber17 = seededRandom();20 const randomNumber18 = seededRandom();21 const randomNumber19 = seededRandom();22 const randomNumber20 = seededRandom();23 const randomNumber21 = seededRandom();24 const randomNumber22 = seededRandom();
Cypress is a renowned Javascript-based open-source, easy-to-use end-to-end testing framework primarily used for testing web applications. Cypress is a relatively new player in the automation testing space and has been gaining much traction lately, as evidenced by the number of Forks (2.7K) and Stars (42.1K) for the project. LambdaTest’s Cypress Tutorial covers step-by-step guides that will help you learn from the basics till you run automation tests on LambdaTest.
You can elevate your expertise with end-to-end testing using the Cypress automation framework and stay one step ahead in your career by earning a Cypress certification. Check out our Cypress 101 Certification.
Watch this 3 hours of complete tutorial to learn the basics of Cypress and various Cypress commands with the Cypress testing at LambdaTest.
Get 100 minutes of automation test minutes FREE!!