diff --git a/core/src/components/refresher/test/test.utils.ts b/core/src/components/refresher/test/test.utils.ts index 40910ce864..706e135a9d 100644 --- a/core/src/components/refresher/test/test.utils.ts +++ b/core/src/components/refresher/test/test.utils.ts @@ -1,4 +1,5 @@ import type { E2EPage } from '@utils/test/playwright'; +import { dragElementByYAxis } from '@utils/test/playwright'; /** * Emulates a pull-to-refresh drag gesture (pulls down and releases). @@ -16,23 +17,8 @@ const pullToRefresh = async (page: E2EPage, selector = 'body') => { await page.waitForSelector('ion-refresher.hydrated', { state: 'attached' }); const ev = await page.spyOnEvent('ionRefreshComplete'); - const boundingBox = await target.boundingBox(); - if (!boundingBox) { - return; - } - - const startX = boundingBox.x + boundingBox.width / 2; - const startY = boundingBox.y + boundingBox.height / 2; - - await page.mouse.move(startX, startY); - await page.mouse.down(); - - for (let i = 0; i < 400; i += 20) { - await page.mouse.move(startX, startY + i); - } - - await page.mouse.up(); + await dragElementByYAxis(target, page, 400); await ev.next(); }; diff --git a/core/src/utils/test/playwright/drag-element.ts b/core/src/utils/test/playwright/drag-element.ts index b053cc17d8..cf8e52e298 100644 --- a/core/src/utils/test/playwright/drag-element.ts +++ b/core/src/utils/test/playwright/drag-element.ts @@ -29,7 +29,42 @@ export const dragElementBy = async ( await page.mouse.move(startX, startY); await page.mouse.down(); + await page.mouse.move(midX, midY); await page.mouse.move(endX, endY); await page.mouse.up(); }; + +/** + * Drags an element by the given amount of pixels on the Y axis. + * @param el The element to drag. + * @param page The E2E Page object. + * @param dragByY The amount of pixels to drag the element by. + * @param startYCoord The Y coordinate to start the drag gesture at. Defaults to the center of the element. + */ +export const dragElementByYAxis = async ( + el: Locator | ElementHandle, + page: E2EPage, + dragByY: number, + startYCoord?: number +) => { + const boundingBox = await el.boundingBox(); + + if (!boundingBox) { + throw new Error( + 'Cannot get a bounding box for an element that is not visible. See https://playwright.dev/docs/api/class-locator#locator-bounding-box for more information' + ); + } + + const startX = boundingBox.x + boundingBox.width / 2; + const startY = startYCoord === undefined ? boundingBox.y + boundingBox.height / 2 : startYCoord; + + await page.mouse.move(startX, startY); + await page.mouse.down(); + + for (let i = 0; i < dragByY; i += 20) { + await page.mouse.move(startX, startY + i); + } + + await page.mouse.up(); +};