Rayrun

How do I perform a drag and drop action between two tabs using Playwright?

Answer

Sure, you can test a drag and drop scenario that requires multiple tabs or windows with Playwright. Playwright supports multiple origins, iframes, tabs, and contexts, so you can interact with different browser windows or tabs during your tests.

Here's how you can do it:

  1. Open the initial tab or window where the drag action starts.
  2. Locate the element that needs to be dragged.
  3. Use the dragAndDrop method provided by Playwright to initiate the drag action on the element.
  4. Switch to the target tab or window where you want to drop the element.
  5. Identify the target location where you want to drop the element.
  6. Use another appropriate method provided by Playwright (e.g., mouse.click) to simulate dropping at the desired location.

Here's a TypeScript code snippet demonstrating how you can perform a drag and drop action between two tabs using Playwright:

import { chromium } from 'playwright';

async function performDragAndDrop() {
  const browser = await chromium.launch();
  const context = await browser.newContext();

  // Open first tab
  const page1 = await context.newPage();
  await page1.goto('https://ray.run');

  // Open second tab
  const page2 = await context.newPage();
  await page2.goto('https://ray.run');

  // Perform drag and drop from first tab/page to second tab/page
  const draggableElement = await page1.$('#draggable');
  
   // Get bounding box of draggable element
   const sourceBoundingBox = await draggableElement.boundingBox();

   // Drag from source position in first tab/page
   await page1.mouse.move(sourceBoundingBox.x + sourceBoundingBox.width / 2,
                          sourceBoundingBox.y + sourceBoundingBox.height / 2);
   await page1.mouse.down();

   // Switch to second tab/page
   await page2.bringToFront();

   // Get target position in second tab/page
   const targetElement = await page2.$('#droppable');
   const targetBoundingBox = await targetElement.boundingBox();

   // Drop at target position in second tab/page
   await page2.mouse.move(targetBoundingBox.x + targetBoundingBox.width / 2,
                          targetBoundingBox.y + targetBoundingBox.height / 2);
   await page2.mouse.up();

  // Close the browser
  await browser.close();
}

performDragAndDrop();

This example launches a Chromium browser using Playwright and opens two tabs with the same URL. It locates the draggable element on the first tab and initiates the drag action. Then, it switches to the second tab and identifies the drop location element. Finally, it simulates dropping at the desired location.

Remember, this is just a basic example, and you may need to adapt it based on your specific scenario. Also, Playwright supports multiple browsers such as Chrome, Firefox, Safari, etc., so you can perform similar drag and drop actions across different browsers as well.

References

Thank you!
Was this helpful?
Still have questions?

If you still have questions, please ask a question and I will try to answer it.

Related Discord Threads

Related Questions

TwitterGitHubLinkedIn
AboutQuestionsDiscord ForumBrowser ExtensionTagsQA Jobs

Rayrun is a community for QA engineers. I am constantly looking for new ways to add value to people learning Playwright and other browser automation frameworks. If you have feedback, email luc@ray.run.