How to use body method in Playwright Internal

Best JavaScript code snippet using playwright-internal

Run Playwright Internal automation tests on LambdaTest cloud grid

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

worker.js

Source: worker.js Github

copy
1/**
2 * Web worker containing a JigLibJS rigid-body physics system.
3 *
4 * @author xeolabs / http://xeolabs.com
5 *
6 * This worker accepts various commands to configure the system, add or
7 * remove bodies, and integrate (which means run the system for one frame).
8 *
9 * After each integration, this worker posts back an array buffer containing
10 * an updated position and direction for each body.
11 *
12 *
13 * Input Commands
14 * --------------------------------------------------------------------------
15 *
16 * Configure the system:
17 * {
18 *      cmd: "setConfigs",
19 *      //..configs
20 * }
21 *
22 * Create a body:
23 * {
24 *      cmd: "createBody",
25 *      bodyId: Number,
26 *      bodyCfg: {
27 *          shape: "plane" | "box" | "sphere",
28 *          movable: true | false,
29 *          pos: [Number, Number, Number],
30 *          mass: Number,
31 *          restitution: Number,
32 *          friction: Number,
33 *          velocity: [Number, Number, Number]
34 *      }
35 *  }
36 *
37 * Remove a body:
38 * {
39 *      cmd: "removeBody",
40 *      bodyId: Number
41 * }
42 *
43 * Update a body:
44 * {
45 *      cmd: "updateBody",
46 *      bodyId: Number,
47 *      bodyCfg: {
48 *          movable: true | false,
49 *          pos: [Number, Number, Number],
50 *          mass: Number,
51 *          restitution: Number,
52 *          friction: Number,
53 *          velocity: [Number, Number, Number]
54 *      }
55 * }
56 *
57 * Integrate the phsycis system:
58 * {
59 *      cmd: "integrate"
60 * }
61 *
62 *
63 * For efficiency, the physics system manages bodies in an array. The "bodyId"
64 * parameter on the "createBody" command is the index for that body in the array.
65 *
66 * The "removeBody" command will delete a body from the array, leaving a hole.
67 *
68 * The worker can handle holes in the array OK, but in order to keep the array
69 * from getting too sparse, it's the reponsibility of the worker client to make
70 * its next "createBody" command specify a "bodyId" that indexes that hole, to plug
71 * the gap with the next new body.
72 *
73 *
74 * Output Buffer
75 * --------------------------------------------------------------------------
76 *
77 * The output buffer contains a 20-element portion for each physics body, each of
78 * which contains the body ID, a new position, and a 16-element rotation matrix:
79 *
80 * [
81 *      bodyId, xPos, yPos, zPos, mat0, ... mat15,
82 *      bodyId, xPos, yPos, zPos, mat0, ... mat15,
83 *      ...
84 * ]
85 *
86 */
87importScripts("jiglib.all.min.js");
88
89var bodies = [];
90var numBodies = 0;
91
92// Array in which this worker posts back
93// an updated position and direction for each body
94var output;
95
96// Physics engine system
97var system = jigLib.PhysicsSystem.getInstance();
98
99// Set initial default configuration for physics system
100setConfigs();
101
102/** Configures JigLibJS
103 */
104function setConfigs(params) {
105    params = params || {};
106    system.setGravity(params.gravity || [0, -9.8, 0, 0]); //-120
107    system.setSolverType(params.solver || 'ACCUMULATED'); //FAST, NORMAL, ACCUMULATED
108}
109
110// System starts immediately
111var then = (new Date()).getTime();
112
113// Handle command from worker owner
114addEventListener("message",
115    function (e) {
116
117        var data = e.data;
118
119        switch (data.cmd) {
120
121            // Configure the physics system
122            case "setConfigs":
123                setConfigs(data.configs);
124                break;
125
126            // Create a physics body
127            case "createBody":
128
129                var bodyId = data.bodyId;
130                var bodyCfg = data.bodyCfg;
131                var shape = bodyCfg.shape;
132                var body;
133
134                switch (shape) {
135
136                    case "plane":
137                        body = new jigLib.JPlane(null, bodyCfg.dir || [0, 1, 0]);
138                        break;
139
140                    case "box":
141                        body = new jigLib.JBox(null, bodyCfg.width || 1.0, bodyCfg.depth || 1.0, bodyCfg.height || 1.0);
142                        break;
143
144                    case "sphere":
145                        body = new jigLib.JSphere(null, bodyCfg.radius || 1.0);
146                        break;
147
148                    default:
149                        // Unsupported body type
150                        return;
151                }
152
153                bodies[bodyId] = {
154                    body:body,
155                    spherical:shape == "sphere"
156                };
157
158                system.addBody(body);
159
160                if (bodyCfg.movable != undefined) {
161                    body.set_movable(!!bodyCfg.movable);
162                }
163
164                if (bodyCfg.pos) {
165                    body.moveTo(bodyCfg.pos);
166                }
167
168                if (bodyCfg.mass != undefined) {
169                    body.set_mass(bodyCfg.mass);
170                }
171
172                if (bodyCfg.restitution != undefined) {
173                    body.set_restitution(bodyCfg.restitution);
174                }
175
176                if (bodyCfg.friction != undefined) {
177                    body.set_friction(bodyCfg.friction);
178                }
179
180                if (bodyCfg.velocity != undefined) {
181                    body.setVelocity(bodyCfg.velocity);
182                }
183
184                numBodies++;
185
186                break;
187
188            // Update a physics body
189            case "updateBody":
190
191                var bodyId = data.bodyId;
192                var body = bodies[bodyId].body;
193
194                if (!body) {
195                    return;
196                }
197
198                var bodyCfg = data.bodyCfg;
199
200                if (bodyCfg.movable != undefined) {
201                    body.set_movable(!!bodyCfg.movable);
202                }
203
204                if (bodyCfg.pos) {
205                    body.moveTo(bodyCfg.pos);
206                }
207
208                if (bodyCfg.mass != undefined) {
209                    body.set_mass(bodyCfg.mass);
210                }
211
212                if (bodyCfg.restitution != undefined) {
213                    body.set_restitution(bodyCfg.restitution);
214                }
215
216                if (bodyCfg.friction != undefined) {
217                    body.set_friction(bodyCfg.friction);
218                }
219
220                if (bodyCfg.velocity != undefined) {
221                    body.setVelocity(bodyCfg.velocity);
222                }
223
224                break;
225
226            // Remove a physics body
227            case "removeBody":
228                var body = bodies[data.bodyId];
229                if (!body) {
230                    return;
231                }
232                bodies[data.bodyId] = null;
233                system.removeBody(body);
234                numBodies--;
235                break;
236
237            // Integrate the physics system and post back the body updates
238            case "integrate":
239
240                var output = new Float32Array(data.buffer);
241
242                var now = (new Date()).getTime();
243
244                //       if (numBodies > 0) { // Only integrate and post if there are bodies
245
246                var secs = (now - then) / 1000;
247                var item;
248                var body;
249                var spherical;
250                var state;
251                var pos;
252                var dir;
253                var ibuf = 0;
254
255                system.integrate(secs);
256
257                for (var bodyId = 0, ibody = 0; ibody < numBodies; bodyId++) {
258
259                    item = bodies[bodyId];
260
261                    if (!item) { // Deleted
262                        continue;
263                    }
264
265                    body = item.body;
266                    spherical = item.spherical;
267
268                    state = body.get_currentState();
269
270                    // Body ID
271                    output[ibuf++] = bodyId;
272
273                    // New position
274
275                    pos = state.position;
276
277                    output[ibuf++] = pos[0];
278                    output[ibuf++] = pos[1];
279                    output[ibuf++] = pos[2];
280
281                    if (spherical) {
282
283                        // No rotation necessary for spheres
284                        ibuf += 16;
285
286                    } else {
287
288                        // New rotation matrix
289
290                        dir = state.get_orientation().glmatrix;
291
292                        output[ibuf++] = dir[0];
293                        output[ibuf++] = dir[1];
294                        output[ibuf++] = dir[2];
295                        output[ibuf++] = dir[3];
296                        output[ibuf++] = dir[4];
297                        output[ibuf++] = dir[5];
298                        output[ibuf++] = dir[6];
299                        output[ibuf++] = dir[7];
300                        output[ibuf++] = dir[8];
301                        output[ibuf++] = dir[9];
302                        output[ibuf++] = dir[10];
303                        output[ibuf++] = dir[11];
304                        output[ibuf++] = dir[12];
305                        output[ibuf++] = dir[13];
306                        output[ibuf++] = dir[14];
307                        output[ibuf++] = dir[15];
308                    }
309
310                    ibody++; // Next body;
311                }
312
313                // Post the output
314
315                var response = {
316                    buffer:output.buffer,
317                    lenOutput:ibuf - 20
318                };
319
320                self.postMessage(response, [response.buffer]);
321
322                then = now;
323
324                break;
325
326            default:
327
328
329                break;
330        }
331    }, false);
332
333
Full Screen

filter.js

Source: filter.js Github

copy
1const fs = require("fs");
2let testJobs
3let filteredJobs = []
4let removedJobs = []
5
6const loadJobs = async () => {
7  await fs.readFile('./jobs/allScraped.js', async (err, data) => {
8    if (err) throw err;
9    testJobs = await JSON.parse(data)
10  });
11}
12
13
14// checkTitle = (title) => {
15//   return title.includes('Sr')
16//     || title.includes('Senior')
17//     || title.includes('senior')
18//     || title.includes('Lead')
19//     ? true : false
20// }
21
22checkBody = (body) => {
23  return body.includes('Sr')
24    || body.includes('Senior')
25    || body.includes('senior')
26    || body.includes('4+')
27    || body.includes('5+')
28    || body.includes('6+')
29    || body.includes('7+')
30    || body.includes('8+')
31    || body.includes('9+')
32    || body.includes('10+')
33    || body.includes('11+')
34    || body.includes('12+')
35    || body.includes('13+')
36    || body.includes('14+')
37    || body.includes('15+')
38    || body.includes('4 + years')
39    || body.includes('5 + years')
40    || body.includes('6 + years')
41    || body.includes('7 + years')
42    || body.includes('8 + years')
43    || body.includes('9 + years')
44    || body.includes('10 + years')
45    || body.includes('11 + years')
46    || body.includes('12 + years')
47    || body.includes('13 + years')
48    || body.includes('14 + years')
49    || body.includes('15 + years')
50    || body.includes('4 years')
51    || body.includes('5 years')
52    || body.includes('6 years')
53    || body.includes('7 years')
54    || body.includes('8 years')
55    || body.includes('9 years')
56    || body.includes('10 years')
57    || body.includes('11 years')
58    || body.includes('12 years')
59    || body.includes('13 years')
60    || body.includes('14 years')
61    || body.includes('15 years')
62    || body.includes('4 plus years')
63    || body.includes('5 plus years')
64    || body.includes('6 plus years')
65    || body.includes('7 plus years')
66    || body.includes('8 plus years')
67    || body.includes('9 plus years')
68    || body.includes('10 plus years')
69    || body.includes('11 plus years')
70    || body.includes('12 plus years')
71    || body.includes('13 plus years')
72    || body.includes('14 plus years')
73    || body.includes('15 plus years')
74    || body.includes('our client')
75    || body.includes('Our client')
76    || body.includes('Our Client')
77    || body.includes('my client')
78    || body.includes('My client')
79    || body.includes('My Client')
80    ? true : false
81}
82
83// checkCompany = (company) => {
84//   return company.includes('Jobs @')
85//     || company.includes('Andiamo')
86//     || company.includes('CyberCoders')
87//     || company.includes('Jobspring')
88//     || company.includes('ClearedJobs')
89//     ? true : false
90// }
91
92check = () => {
93  for (job of testJobs) {
94    // checkCompany(job.company) || checkTitle(job.title) || checkBody(job.body) ? removedJobs.push(job) : filteredJobs.push(job)
95    checkBody(job.body) ? removedJobs.push(job) : filteredJobs.push(job)
96  }
97  saveJobs()
98  // console.log(filteredJobs)
99}
100
101// writeJobs = () => {
102//   const parent = document.querySelector('#jobs')
103
104//   const el = document.createElement('li')
105//   filteredJobs.forEach(job => {
106//     el.innerHTML=job
107//     parent.appenChild(el)
108//   })
109// }
110
111const saveJobs = async() => {
112  await fs.writeFile(`./jobs/allFilteredJobs.js`, JSON.stringify(filteredJobs), function (err) {
113    if (err) {
114      console.log(err);
115    }
116    else {
117      console.log("Output saved to /allFilteredJobs.js.");
118    }
119  });
120}
121
122const runFilter = async () => {
123  await loadJobs()
124  setTimeout(check, 2000)
125  // console.log(testJobs)
126
127}
128
129runFilter()
130
Full Screen

racket.js

Source: racket.js Github

copy
1/**
2 * racket : Not optimally desgiend, but to assist in some gaming utility and 
3 * physics for animation.
4 * 
5 * The racket namespace currently contains the two libraries:
6 * 
7 * 1. physikz: supports cheap physics and collision detection.
8 * 2. num: a lib of utility methods to work with numbers.
9 * 
10 * dependencies: See the bower.json file for current dependency versions, and 
11 * ensure you add dependencies to your index.html file, as in:
12 * 
13 * <script src="bower_components/lodash/lodash.min.js"></script>
14 *
15 */
16(function (window) {
17    window.opspark = window.opspark || {};
18    
19    function sortNumbersAscending(a, b) { return a - b; }
20    
21    function sortNumbersDecending(a, b) { return b - a; }
22    
23    function randomIntBetween(min, max) { 
24        return Math.floor(Math.random() * (max - min + 1) + min);
25    }
26    
27    // radians = degrees * Math.PI / 180 //
28    function degreesToRadians(degrees) {
29        return degrees * Math.PI / 180;
30    }
31    
32    // degrees = radians * 180 / Math.PI //
33    function radiansToDegrees(radians) {
34        return radians * 180 / Math.PI;
35    }
36    
37    function getDistance(pointOne, pointTwo) {
38        var distanceX = pointTwo.x - pointOne.x;
39        var distanceY = pointTwo.y - pointOne.y;
40        return Math.sqrt(distanceX * distanceX + distanceY * distanceY);
41    }
42    
43    function getDistanceProperties(bodyA, bodyB) {
44        var distanceX = bodyB.x - bodyA.x;
45        var distanceY = bodyB.y - bodyA.y;
46        return {
47            bodyA: bodyA,
48            bodyB: bodyB,
49            distanceX: distanceX,
50            distanceY: distanceY,
51            distance: Math.sqrt(distanceX * distanceX + distanceY * distanceY)
52        };
53    }
54    
55    function hitTestRadial(distance, bodyA, bodyB) { 
56        var radiusCombined = bodyA.radius + bodyB.radius;
57        return {
58            bodyA: bodyA,
59            bodyB: bodyB,
60            isHit: (distance < radiusCombined),
61            radiusCombined: radiusCombined
62        };
63    }
64    
65    function getImpactProperties(bodyA, bodyB) {
66        var combinedVolatility = bodyA.volatility + bodyB.volatility;
67        var combinedDensity = bodyA.density * bodyB.density;
68        return {
69            bodyA: bodyA,
70            bodyB: bodyB,
71            combinedVolatility: combinedVolatility,
72            combinedDensity: combinedDensity,
73            impact: (combinedVolatility ? combinedVolatility * combinedDensity : combinedDensity)
74        };
75    }
76    
77    var racket = {
78        physikz: {
79            addRandomVelocity: function (body, area, multiplierX, multiplierY) {
80                if (!body.integrity) { _.extend(body, this.makeBody()); }
81                
82                multiplierX = (multiplierX) ? multiplierX : .6;
83                multiplierY = (multiplierY) ? multiplierY : .5;
84                
85                var tx = randomIntBetween(0, area.width);
86                var ty = randomIntBetween(0, area.height);
87                var dx = Math.abs(tx - body.x);
88                var dy = Math.abs(ty - body.y);
89                var radians = Math.atan2(dy, dx);
90                body.rotation = radiansToDegrees(radians);
91                
92                var rotationalDirection = (Math.round(Math.random()) === 1) ? 1 : -1;
93                body.rotationalVelocity = randomIntBetween(1, 3) * rotationalDirection;
94                var forceX = Math.cos(radians) * (Math.random() * multiplierX);
95                var forceY = Math.sin(radians) * (Math.random() * multiplierY);
96                
97                body.velocityX = (tx > body.x) ? forceX : -forceX;
98                body.velocityY = (ty > body.y) ? forceY : -forceY;
99            },
100            
101            updatePosition: function (body) {
102                body.x += body.velocityX;
103                body.y += body.velocityY;
104                body.rotation += body.rotationalVelocity;
105            },
106            
107            updateRadialPositionInArea: function (body, area) {
108                var radius = body.radius;
109                var w  = area.width + radius * 2;
110                var h = area.height + radius * 2;
111                
112                body.x = (body.x + radius + body.velocityX + w) % w - radius;
113                body.y = (body.y + radius + body.velocityY + h) % h - radius;
114                body.rotation += body.rotationalVelocity;
115            },
116            
117            updateRadialPositionAndReboundInArea: function (body, area) {
118                var radius = body.radius;
119                var top = 0;
120                var left = 0;
121                var right = area.width;
122                var bottom = area.height;
123                
124                body.x += body.velocityX;
125                body.y += body.velocityY;
126                body.rotation += body.rotationalVelocity;
127                
128                if (body.x + radius > right) {
129                    body.x = right - radius;
130                    body.velocityX *= -1;
131                    
132                } else if (body.x - radius < left) {
133                    body.x = left + radius;
134                    body.velocityX *= -1;
135                }
136                
137                if (body.y + radius > bottom) {
138                    body.y = bottom - radius;
139                    body.velocityY *= -1;
140                } else if (body.y - radius < top) {
141                    body.y = top + radius;
142                    body.velocityY *= -1;
143                }
144            },
145            
146            /*
147             * getDistance: Using the Pythagorean Theorem, returns the 
148             *      distance between two points.
149             *
150             * @return A Number representing the distance between two points.
151             */
152            getDistance: getDistance,
153            
154            /*
155             * getDistanceProperties: Using the Pythagorean Theorem, returns an 
156             *      distanceobject with properties distance, distanceX, and distanceY.
157             *
158             * @return Object  An object with properties pointOne, pointTwo, 
159             *      distance, distanceX, and distanceY.
160             */
161            getDistanceProperties: getDistanceProperties,
162            
163            /*
164             * Takes to bodies, returns an object with their combinedVolatility, 
165             *      combinedDensity, and impact.
166             */
167            getImpactProperties: getImpactProperties,
168            
169            /*
170             * hitTestRadial: Expects the distance betwo bodies with a radius property. Returns 
171             *      an object with the result of the radial hit test, with the 
172             *      property isHit being true if the distance between the x/y of 
173             *      the radial shapes is less than the sum of their two radius.
174             *
175             * @return Object
176             */
177            hitTestRadial: hitTestRadial,
178            
179            /*
180             * Takes an Array of bodies to manage as the space, a hitTest 
181             *      function to preform between each body in the space, and a 
182             *      handleCollision function designed to respond to collision. 
183             */
184            updateSpace: function (space, hitTest, handleCollision) {
185                for(var i = space.length - 1; i > 0; i--) {
186                    var bodyA = space[i];
187                    for(var j = i - 1; j > -1; j--) {
188                        var bodyB = space[j];
189                        var distanceProperties = getDistanceProperties(bodyA, bodyB);
190                        var hitResult = hitTest(distanceProperties.distance, bodyA, bodyB);
191                        if(hitResult.isHit) {
192                            handleCollision(distanceProperties, hitResult, getImpactProperties(bodyA, bodyB));
193                        }
194                    }
195                }
196            },
197            
198            makeBody: function (type, velocityX, velocityY, rotationalVelocity, integrity, density, volatility) {
199                return {
200                    type: type || 'undefined',
201                    velocityX: velocityX || 0,
202                    velocityY: velocityY || 0,
203                    rotationalVelocity: rotationalVelocity || 0,
204                    integrity: integrity || 1,
205                    density: density || 1,
206                    volatility: volatility || 0,
207                    
208                    handleCollision: function (impact, body) {
209                        // template method //
210                    }
211                };
212            },
213            
214            degreesToRadians: degreesToRadians,
215            radiansToDegrees: radiansToDegrees
216        },
217        
218        num: {
219            randomIntBetween: randomIntBetween,
220            sortNumbersAscending: sortNumbersAscending,
221            sortNumbersDecending: sortNumbersDecending,
222            degreesToRadians: degreesToRadians,
223            radiansToDegrees: radiansToDegrees
224        }
225    };
226    window.opspark.racket = racket;
227}(window));
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 Playwright Internal 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)