Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Playwright test drag [closed]

I am working on this template where I am using dragTo() function for dragging and dropping. When I run my tests on headed mode, it works fine. But when I run the tests in headless mode, it simply wont drag anything and the test will pass with the page blank. Is there any way I can slow down the dragging so that the page could identify the dragged element before jumping onto the other action?

I tried adding timeout the following way but still no luck:

await this.page.locator('text=Column').first()
  .dragTo(this.page.locator('[role="tabpanel"]')
  .first(), {force:true}), {timeout:3000};
like image 878
user9857359 Avatar asked Jul 02 '26 08:07

user9857359


1 Answers

So, the only way I could make this work was using the following code:

async dragDrop(page: Page, originSelector: string, destinationSelector: string) {
    const originElement = await page.waitForSelector(originSelector);
    const destinationElement = await page.waitForSelector(destinationSelector);

    await originElement.hover();
    await page.mouse.down();
    const box = (await destinationElement.boundingBox())!;
    await page.mouse.move(box.x + box.width / 2, box.y + box.height / 2);
    await destinationElement.hover();
    await page.mouse.up();
}
like image 187
user9857359 Avatar answered Jul 05 '26 03:07

user9857359



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!