clone of mouse move helper

This commit is contained in:
Kurt Hutten Irev-Dev
2024-07-23 09:07:42 +10:00
parent 397839da84
commit a977d0d386
2 changed files with 74 additions and 8 deletions

View File

@ -1,4 +1,4 @@
import { test, expect, Page } from '@playwright/test' import { test, expect, Page, TestInfo } from '@playwright/test'
import { import {
makeTemplate, makeTemplate,
getUtils, getUtils,
@ -5801,7 +5801,7 @@ test.describe('Testing segment overlays', () => {
* @param {number} options.steps - The number of steps to perform * @param {number} options.steps - The number of steps to perform
*/ */
const _clickConstrained = const _clickConstrained =
(page: Page) => (page: Page, testInfo?: TestInfo) =>
async ({ async ({
hoverPos, hoverPos,
constraintType, constraintType,
@ -5834,7 +5834,7 @@ test.describe('Testing segment overlays', () => {
x = hoverPos.x + Math.cos(ang * deg) * 32 x = hoverPos.x + Math.cos(ang * deg) * 32
y = hoverPos.y - Math.sin(ang * deg) * 32 y = hoverPos.y - Math.sin(ang * deg) * 32
await page.mouse.move(x, y) await page.mouse.move(x, y)
await wiggleMove(page, x, y, 20, 30, ang, 10, 5, locator) await wiggleMove(page, x, y, 20, 30, ang, 10, 5, locator, testInfo)
await expect(page.locator('.cm-content')).toContainText( await expect(page.locator('.cm-content')).toContainText(
expectBeforeUnconstrained expectBeforeUnconstrained
@ -5955,7 +5955,7 @@ test.describe('Testing segment overlays', () => {
test.setTimeout(120000) test.setTimeout(120000)
test('for segments [line, angledLine, lineTo, xLineTo]', async ({ test('for segments [line, angledLine, lineTo, xLineTo]', async ({
page, page,
}) => { }, testInfo) => {
await page.addInitScript(async () => { await page.addInitScript(async () => {
localStorage.setItem( localStorage.setItem(
'persistCode', 'persistCode',
@ -5999,7 +5999,7 @@ test.describe('Testing segment overlays', () => {
await expect(page.getByTestId('segment-overlay')).toHaveCount(13) await expect(page.getByTestId('segment-overlay')).toHaveCount(13)
const clickUnconstrained = _clickUnconstrained(page) const clickUnconstrained = _clickUnconstrained(page)
const clickConstrained = _clickConstrained(page) const clickConstrained = _clickConstrained(page, testInfo)
await u.openAndClearDebugPanel() await u.openAndClearDebugPanel()
await u.sendCustomCmd({ await u.sendCustomCmd({

View File

@ -1,4 +1,4 @@
import { test, expect, Page, Download } from '@playwright/test' import { expect, Page, Download, TestInfo } from '@playwright/test'
import { EngineCommand } from '../../src/lang/std/engineConnection' import { EngineCommand } from '../../src/lang/std/engineConnection'
import os from 'os' import os from 'os'
import fsp from 'fs/promises' import fsp from 'fs/promises'
@ -107,7 +107,7 @@ async function waitForCmdReceive(page: Page, commandType: string) {
} }
export const wiggleMove = async ( export const wiggleMove = async (
page: any, page: Page,
x: number, x: number,
y: number, y: number,
steps: number, steps: number,
@ -115,11 +115,13 @@ export const wiggleMove = async (
ang: number, ang: number,
amplitude: number, amplitude: number,
freq: number, freq: number,
locator?: string locator?: string,
testInfo?: TestInfo
) => { ) => {
const tau = Math.PI * 2 const tau = Math.PI * 2
const deg = tau / 360 const deg = tau / 360
const step = dist / steps const step = dist / steps
let mouseMovements: MouseMovement[] = []
for (let i = 0, j = 0; i < dist; i += step, j += 1) { for (let i = 0, j = 0; i < dist; i += step, j += 1) {
if (locator) { if (locator) {
const isElVis = await page.locator(locator).isVisible() const isElVis = await page.locator(locator).isVisible()
@ -132,7 +134,71 @@ export const wiggleMove = async (
] ]
const [xr, yr] = [x2, y2] const [xr, yr] = [x2, y2]
await page.mouse.move(x + xr, y + yr, { steps: 5 }) await page.mouse.move(x + xr, y + yr, { steps: 5 })
mouseMovements.push({ x: x + xr, y: y + yr, action: 'move' })
} }
// element was not visible, show the mouse movements in the screenshot
if (testInfo) {
testInfo?.annotations.push({
type: '⚠️',
description:
'Wiggling the mouse did not make the element visible - refer to the screenshot below to view mouse movements',
})
await showMovementsInScreenshot(page, mouseMovements, testInfo)
}
}
export type MouseMovement = {
x: number
y: number
action: 'click' | 'move'
}
const showMovementsInScreenshot = async (
page: Page,
mouseMovements: MouseMovement[],
testInfo: TestInfo
) => {
await page.evaluate((mouseMovements) => {
for (let mouseMovement of mouseMovements) {
const { x, y, action } = mouseMovement
const elementWidth = action === 'move' ? 3 : 5
let circle = document.createElement('div')
circle.id = `pw-indicator-x${x}-y${y}`
circle.style.position = 'absolute'
circle.style.width = `${elementWidth}px`
circle.style.height = `${elementWidth}px`
circle.style.pointerEvents = 'none'
if (action === 'click') {
circle.style.backgroundColor = 'green'
circle.style.transform = 'rotate(45deg)'
// in case click and move are at the same position, ensure click is shown behind
circle.style.zIndex = '999'
} else {
circle.style.backgroundColor = 'red'
circle.style.borderRadius = '50%'
circle.style.zIndex = '1000'
}
circle.style.left = `${x - elementWidth / 2}px`
circle.style.top = `${y - elementWidth / 2}px`
document.body.appendChild(circle)
}
}, mouseMovements)
const screenshot = await page.screenshot()
await testInfo.attach('screenshot', {
body: screenshot,
contentType: 'image/png',
})
await removeIndicators(page)
}
const removeIndicators = async (page: Page) => {
await page.evaluate(() => {
let indicators = document.querySelectorAll('[id*="pw-indicator-"]')
indicators.forEach((e) => e.remove())
})
} }
export const circleMove = async ( export const circleMove = async (