-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplaywright-corner-tests.js
More file actions
465 lines (400 loc) · 13.7 KB
/
Copy pathplaywright-corner-tests.js
File metadata and controls
465 lines (400 loc) · 13.7 KB
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
const { chromium } = require("playwright");
(async () => {
// Launch browser and open page
const browser = await chromium.launch({ headless: false });
const page = await browser.newPage();
await page.goto("http://localhost:4200");
// Wait for the draggable element to be visible
await page.waitForSelector('[data-drag="true"]');
// Wait for Angular to be stable
await page.waitForTimeout(1000);
// Helper function to get the position from transform style
const getCurrentPosition = async () => {
return page.evaluate(() => {
const dragElement = document.querySelector('[data-drag="true"]');
if (!dragElement) return { x: null, y: null };
const style = window.getComputedStyle(dragElement);
const transform = style.transform || style.webkitTransform;
if (transform === "none") return { x: 0, y: 0 };
try {
const matrix = new DOMMatrixReadOnly(transform);
return { x: matrix.m41, y: matrix.m42 };
} catch (e) {
console.error("Error parsing transform:", e);
return { x: null, y: null };
}
});
};
// Helper to determine which corner based on position
const determineCorner = (
position,
boundaryWidth,
boundaryHeight,
elementWidth,
elementHeight
) => {
if (!position || position.x === null) return "UNKNOWN";
const x = position.x;
const y = position.y;
const tolerance = 5; // Small tolerance for floating point comparisons
// Check if position matches any of the four corners
if (Math.abs(x) <= tolerance && Math.abs(y) <= tolerance) {
return "TOP_LEFT";
}
if (
Math.abs(x - (boundaryWidth - elementWidth)) <= tolerance &&
Math.abs(y) <= tolerance
) {
return "TOP_RIGHT";
}
if (
Math.abs(x) <= tolerance &&
Math.abs(y - (boundaryHeight - elementHeight)) <= tolerance
) {
return "BOTTOM_LEFT";
}
if (
Math.abs(x - (boundaryWidth - elementWidth)) <= tolerance &&
Math.abs(y - (boundaryHeight - elementHeight)) <= tolerance
) {
return "BOTTOM_RIGHT";
}
return "IN_BETWEEN";
};
// Get boundary dimensions
const boundaryBox = await page.locator(".example-boundary").boundingBox();
const boundaryWidth = boundaryBox.width;
const boundaryHeight = boundaryBox.height;
// Get element dimensions
const elementBox = await page.locator('[data-drag="true"]').boundingBox();
const elementWidth = elementBox.width;
const elementHeight = elementBox.height;
// Corners and their positions
const corners = {
TOP_LEFT: { x: 0, y: 0 },
TOP_RIGHT: { x: boundaryWidth - elementWidth, y: 0 },
BOTTOM_LEFT: { x: 0, y: boundaryHeight - elementHeight },
BOTTOM_RIGHT: {
x: boundaryWidth - elementWidth,
y: boundaryHeight - elementHeight,
},
};
// Directions to test from each corner
const directions = [
{ name: "RIGHT", vector: { x: 100, y: 0 } },
{ name: "DOWN", vector: { x: 0, y: 100 } },
{ name: "LEFT", vector: { x: -100, y: 0 } },
{ name: "UP", vector: { x: 0, y: -100 } },
{ name: "DOWN_RIGHT", vector: { x: 100, y: 100 } },
{ name: "DOWN_LEFT", vector: { x: -100, y: 100 } },
{ name: "UP_RIGHT", vector: { x: 100, y: -100 } },
{ name: "UP_LEFT", vector: { x: -100, y: -100 } },
];
// Expected results for each starting corner and drag direction
// Based on the component's quadrant and angle-based logic
const expectedResults = {
TOP_LEFT: {
RIGHT: "TOP_RIGHT",
DOWN: "BOTTOM_LEFT",
LEFT: "TOP_LEFT",
UP: "TOP_LEFT",
DOWN_RIGHT: "BOTTOM_RIGHT",
DOWN_LEFT: "BOTTOM_LEFT",
UP_RIGHT: "TOP_RIGHT",
UP_LEFT: "TOP_LEFT",
},
TOP_RIGHT: {
RIGHT: "TOP_RIGHT",
DOWN: "BOTTOM_RIGHT",
LEFT: "TOP_LEFT",
UP: "TOP_RIGHT",
DOWN_RIGHT: "BOTTOM_RIGHT",
DOWN_LEFT: "BOTTOM_LEFT",
UP_RIGHT: "TOP_RIGHT",
UP_LEFT: "TOP_LEFT",
},
BOTTOM_LEFT: {
RIGHT: "BOTTOM_RIGHT",
DOWN: "BOTTOM_LEFT",
LEFT: "TOP_LEFT",
UP: "TOP_LEFT",
DOWN_RIGHT: "BOTTOM_RIGHT",
DOWN_LEFT: "BOTTOM_LEFT",
UP_RIGHT: "TOP_RIGHT",
UP_LEFT: "TOP_LEFT",
},
BOTTOM_RIGHT: {
RIGHT: "BOTTOM_RIGHT",
DOWN: "BOTTOM_RIGHT",
LEFT: "BOTTOM_LEFT",
UP: "TOP_RIGHT",
DOWN_RIGHT: "BOTTOM_RIGHT",
DOWN_LEFT: "BOTTOM_LEFT",
UP_RIGHT: "TOP_RIGHT",
UP_LEFT: "TOP_LEFT",
},
};
// Function to directly set corner using the component method
const directlySetCorner = async (cornerName) => {
console.log(`Setting corner directly to ${cornerName}`);
return await page.evaluate((corner) => {
try {
// Access the component using alternate methods
// Method 1: Through Angular's debugging info
const appRef = window.ng?.getComponent(
document.querySelector("app-drag-snap")
);
if (appRef && typeof appRef.setCorner === "function") {
appRef.setCorner(corner);
return true;
}
// Method 2: Direct component access (Angular v17+)
const component = document.querySelector("app-drag-snap");
if (
component &&
component.__ngContext__ &&
typeof component.setCorner === "function"
) {
component.setCorner(corner);
return true;
}
// Fallback: Try accessing through ngContext
const context = component?.__ngContext__;
if (context && Array.isArray(context)) {
const dragSnapComponent = context.find(
(c) => c?.constructor?.name === "DragSnapComponent"
);
if (
dragSnapComponent &&
typeof dragSnapComponent.setCorner === "function"
) {
dragSnapComponent.setCorner(corner);
return true;
}
}
console.error("Could not find component using any method");
return false;
} catch (e) {
console.error("Failed to set corner:", e);
return false;
}
}, cornerName);
};
// Function to verify if element is at the expected corner
const verifyCorner = async (expectedCornerName) => {
const position = await getCurrentPosition();
const currentCorner = determineCorner(
position,
boundaryWidth,
boundaryHeight,
elementWidth,
elementHeight
);
return currentCorner === expectedCornerName;
};
// Manual drag to a corner
const manualDragToCorner = async (cornerName) => {
console.log(`Manually dragging to ${cornerName}`);
const corner = corners[cornerName];
// Get current element position
const box = await page.locator('[data-drag="true"]').boundingBox();
const centerX = box.x + box.width / 2;
const centerY = box.y + box.height / 2;
// Calculate position to center of the element
const targetCenterX = corner.x + elementWidth / 2;
const targetCenterY = corner.y + elementHeight / 2;
// Use more forceful approach with multiple clicks and slower movement
// First click to ensure focus
await page.mouse.click(centerX, centerY);
await page.waitForTimeout(100);
// Now do the drag with slower, more deliberate steps
await page.mouse.move(centerX, centerY);
await page.mouse.down();
// Move in smaller steps for more reliable dragging
const steps = 20;
const xStep = (targetCenterX - centerX) / steps;
const yStep = (targetCenterY - centerY) / steps;
for (let i = 1; i <= steps; i++) {
await page.mouse.move(centerX + xStep * i, centerY + yStep * i, {
steps: 1,
});
await page.waitForTimeout(20); // Small delay between steps
}
await page.mouse.up();
await page.waitForTimeout(500); // Wait for any animations
// Verify position
const position = await getCurrentPosition();
const resultCorner = determineCorner(
position,
boundaryWidth,
boundaryHeight,
elementWidth,
elementHeight
);
return resultCorner === cornerName;
};
// Function to set position using the buttons
const setPositionUsingButton = async (cornerName) => {
console.log(`Setting position using ${cornerName} button`);
// Find the button by text
const buttonText = {
TOP_LEFT: "Top Left",
TOP_RIGHT: "Top Right",
BOTTOM_LEFT: "Bottom Left",
BOTTOM_RIGHT: "Bottom Right",
}[cornerName];
if (!buttonText) {
console.error(`Unknown corner name: ${cornerName}`);
return false;
}
try {
// Click the button
await page.locator(`button:text("${buttonText}")`).click();
await page.waitForTimeout(500);
// Verify we're at the intended corner
return await verifyCorner(cornerName);
} catch (e) {
console.error(`Failed to click button for ${cornerName}:`, e);
return false;
}
};
// Function to reset position to a specific corner with retries
const resetToCorner = async (cornerName, maxRetries = 3) => {
for (let attempt = 1; attempt <= maxRetries; attempt++) {
console.log(
`Attempt ${attempt}/${maxRetries} to set corner to ${cornerName}`
);
// First try using the buttons (most reliable)
const buttonSuccess = await setPositionUsingButton(cornerName);
if (buttonSuccess) {
console.log(`✅ Successfully positioned at ${cornerName} using button`);
return true;
}
// Try direct method next
const directSuccess = await directlySetCorner(cornerName);
await page.waitForTimeout(300);
// Verify if we're at the correct corner
if (await verifyCorner(cornerName)) {
console.log(
`✅ Successfully positioned at ${cornerName} using direct method`
);
return true;
}
// If previous methods failed and this is the last attempt, try manual positioning
if (attempt === maxRetries - 1) {
console.log(`Trying manual drag as a last resort...`);
const manualSuccess = await manualDragToCorner(cornerName);
if (manualSuccess) {
console.log(
`✅ Successfully positioned at ${cornerName} using manual drag`
);
return true;
}
}
if (attempt === maxRetries) {
console.warn(
`⚠️ Failed to position at ${cornerName} after ${maxRetries} attempts`
);
return false;
}
await page.waitForTimeout(300);
}
return false;
};
// Test suite to test all directions from each corner
const testAllCorners = async () => {
console.log("\n=== COMPREHENSIVE CORNER TESTING ===");
console.log(`Boundary dimensions: ${boundaryWidth}x${boundaryHeight}`);
console.log(`Element dimensions: ${elementWidth}x${elementHeight}`);
let totalTests = 0;
let passedTests = 0;
let skippedTests = 0;
// Test each corner
for (const startCorner of Object.keys(corners)) {
console.log(`\n=== Testing from ${startCorner} ===`);
// Test each direction from this corner
for (const direction of directions) {
totalTests++;
// First, ensure we're at the starting corner for each test
const isReset = await resetToCorner(startCorner);
if (!isReset) {
console.error(
`⚠️ Skipping test: Couldn't position at ${startCorner} for testing`
);
skippedTests++;
continue;
}
console.log(`\nTest: Drag from ${startCorner} to ${direction.name}`);
// Get element's current position
const box = await page.locator('[data-drag="true"]').boundingBox();
const centerX = box.x + box.width / 2;
const centerY = box.y + box.height / 2;
// Perform the drag in the specified direction
await page.mouse.move(centerX, centerY);
await page.mouse.down();
await page.mouse.move(
centerX + direction.vector.x,
centerY + direction.vector.y,
{ steps: 10 }
);
await page.mouse.up();
// Wait for snap animation
await page.waitForTimeout(1000);
// Check result
const newPosition = await getCurrentPosition();
const resultCorner = determineCorner(
newPosition,
boundaryWidth,
boundaryHeight,
elementWidth,
elementHeight
);
const expectedCorner = expectedResults[startCorner][direction.name];
const testPassed = resultCorner === expectedCorner;
if (testPassed) {
passedTests++;
console.log(
`✅ PASS: Drag from ${startCorner} ${direction.name} → ${resultCorner}`
);
} else {
console.log(
`❌ FAIL: Drag from ${startCorner} ${direction.name} → ${resultCorner} (Expected: ${expectedCorner})`
);
}
}
}
// Print results summary
console.log("\n=== TEST RESULTS SUMMARY ===");
console.log(
`Passed: ${passedTests}/${totalTests} (${Math.round(
(passedTests / totalTests) * 100
)}%)`
);
if (skippedTests > 0) {
console.log(`Skipped: ${skippedTests}/${totalTests}`);
}
return {
passed: passedTests === totalTests - skippedTests,
passedCount: passedTests,
totalCount: totalTests,
skippedCount: skippedTests,
};
};
// Run all tests
const results = await testAllCorners();
console.log("\nAll tests passed:", results.passed ? "✅ YES" : "❌ NO");
if (results.skippedCount > 0) {
console.log(
`\nNote: ${results.skippedCount} tests were skipped due to positioning issues.`
);
console.log(
"You may need to fix the positioning logic in the component first."
);
}
// Wait before closing browser
await page.waitForTimeout(3000);
await browser.close();
})().catch((e) => {
console.error("Test error:", e);
process.exit(1);
});