Advanced Playwright Actions

This section covers additional Playwright actions that are commonly used in real-world automation scenarios, such as drag-and-drop interactions, screenshots, dropdown handling, file uploads, and dialog handling.

1. Drag and Drop

Playwright provides a built-in dragTo() method to handle drag-and-drop interactions between elements.

await page.locator('#source').dragTo(page.locator('#target'));


This method automatically performs the mouse actions required to drag the source element and drop it onto the target element.

2. Taking Screenshots

Screenshots are useful for debugging failures and documenting application state.

Full Page Screenshot

await page.screenshot({ path: 'page.png', fullPage: true });

Element Screenshot

await page.locator('#logo').screenshot({ path: 'logo.png' });


Element-level screenshots capture only the visible area of the selected element.

3. Miscellaneous Page-Level Actions

These actions operate at the page level and are useful for test control and environment setup.

await page.waitForTimeout(2000); // Hard wait (use cautiously)
await page.setViewportSize({ width: 1280, height: 720 });
await page.emulateMedia({ media: 'print' });


waitForTimeout should be avoided in favor of auto-waiting and assertions whenever possible.

4. Dropdown Handling

Playwright handles native <select> dropdowns using selectOption().

await page.locator('#country').selectOption('IN');


Select by value:

await page.locator('#country').selectOption({ value: 'IN' });


Select by visible label:

await page.locator('#country').selectOption({ label: 'India' });


5. File Upload

Playwright supports multiple ways to upload files without interacting with the system file picker.

Upload a Single File

await page.locator('#file').setInputFiles('tests/data/file.txt');

 

Upload Multiple Files

await page.getByLabel('Upload files').setInputFiles([
  path.join(__dirname, 'file1.txt'),
  path.join(__dirname, 'file2.txt'),
]);


Upload File from Memory (No Physical File)

await page.getByLabel('Upload file').setInputFiles({
  name: 'file.txt',
  mimeType: 'text/plain',
  buffer: Buffer.from('this is test'),
});


File Chooser Handling

Some applications open the browser’s file chooser dialog. This can be handled using the filechooser event.

const fileChooserPromise = page.waitForEvent('filechooser');
await page.getByLabel('Upload file').click();
const fileChooser = await fileChooserPromise;
await fileChooser.setFiles(path.join(__dirname, 'myfile.pdf'));


6. Dialog Handling in Playwright JS

Playwright automatically listens for JavaScript dialogs such as:

     - alert()
     - confirm()
     - prompt()

Basic Dialog Handling

page.on('dialog', async dialog => {
  await dialog.accept();
});


Accessing Dialog Properties

page.on('dialog', async dialog => {
  console.log(dialog.type());    // alert | confirm | prompt | beforeunload
  console.log(dialog.message()); // dialog text
  await dialog.accept();
});


Handling Different Dialog Types

Alert (Only OK):

page.on('dialog', async dialog => {
  await dialog.accept();
});


Confirm Dialog

Accept (OK):

page.on('dialog', async dialog => {
  await dialog.accept();
});


Dismiss (Cancel):

page.on('dialog', async dialog => {
  await dialog.dismiss();
});


Prompt Dialog (With Input)

page.on('dialog', async dialog => {
  await dialog.accept('Hello Playwright');
});


Validating Dialog Message

page.on('dialog', async dialog => {
  expect(dialog.message()).toBe('Are you sure?');
  await dialog.accept();
});


One-Time Dialog Handling (Best Practice)

Use page.once() to avoid accidental multiple handlers.

page.once('dialog', async dialog => {
  await dialog.accept();
});


This approach is cleaner and safer when handling a single dialog.

 

Related Tutorials