QA AND TEST AUTOMATION · PART 5 · UI WEB AUTOMATION · TECHNIQUE · 61 OF 112

Uploads, Downloads, iFrames and Tabs in UI Tests

The awkward parts of UI automation, run for real: a new tab from ShopRung's "SQL console" footer link, then on a small demo page a file upload with setInputFiles, a CSV download saved and read back, and a payment form inside an iframe with frameLocator. Plus Selenium equivalents (sendKeys on file inputs, window handles, switchTo().frame), dialogs, and shadow DOM.

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  16. 16
  17. 17
  18. 18
  19. 19
  20. 20
  21. 21
  22. 22
  23. 23
  24. 24
  25. 25
  26. 26
  27. 27
  28. 28
  29. 29
  30. 30
  31. 31
  32. 32
  33. 33
  34. 34
  35. 35
  36. 36
  37. 37
  38. 38
  39. 39
  40. 40
  41. 41
  42. 42
  43. 43
  44. 44
  45. 45
  46. 46
  47. 47
  48. 48
  49. 49
  50. 50
  51. 51
  52. 52
  53. 53
  54. 54
  55. 55
  56. 56
  57. 57
  58. 58
  59. 59
  60. 60
  61. 61
  62. 67
  63. 68
  64. 69
  65. 70
  66. 71
  67. 72
  68. 73
  69. 74
  70. 75
  71. 76
  72. 77
  73. 78
  74. 79
  75. 80
  76. 81
  77. 82
  78. 83
  79. 84
  80. 85
  81. 86
  82. 87
  83. 88
  84. 89
  85. 90
  86. 91
  87. 92
  88. 93
  89. 94
  90. 95
  91. 96
  92. 97
  93. 98
  94. 99
  95. 100
  96. 101
  97. 102
  98. 103
  99. 104
  100. 105
  101. 106
  102. 107
  103. 108
  104. 109
  105. 110
  106. 111
  107. 112
1 / 12

Press play to hear the narration, or step through at your own pace.

Key takeaways

  • Wait for page or download with the click
  • setInputFiles
  • check file content
  • frameLocator
  • dialogs
  • shadow DOM

Notes

New tabs

ShopRung's footer link, SQL console, opens in a new tab. In Playwright, a new tab is a new page in the same context. Start waiting for the context's page event and click the link together, using promise all, so the event is not missed. Then wait for the new page to load. Our run caught it: the URL ending in slash sql, the title ShopRung SQL console, and two pages in the context. Work with both pages as normal objects.

Catch the new page
const [sql] = await Promise.all([
  context.waitForEvent("page"),
  page.getByRole("link",
    { name: "SQL console" })
    .click(),
]);
await sql.waitForLoadState();

real: url .../sql, title
  "ShopRung SQL console",
  2 pages in the context

Tabs in Selenium

Selenium works with one window at a time, identified by handles. Save the main handle, click the link, and wait until there are two windows. Loop through the handles and switch to the one that is not the main window. Test it, close it, and switch back to the main handle. Forgetting to switch back is a classic cause of no such element errors on the next step.

Selenium windows
String main = d.getWindowHandle();
d.findElement(By.linkText(
  "SQL console")).click();
wait.until(numberOfWindowsToBe(2));
for (String h : d.getWindowHandles())
  if (!h.equals(main))
    d.switchTo().window(h);
// ... test the new tab
d.close();
d.switchTo().window(main);

Uploads

Never try to drive the operating system's file picker. Set files directly on the file input. Playwright's set input files takes a path, several paths, or an in memory file with a name, type and contents, and an empty list clears it. On our demo page, the label receipt took receipt dot txt, and the page showed uploaded receipt dot txt, thirteen bytes. In Selenium, send the absolute file path as keys to the file input.

File upload
await page.getByLabel("Receipt")
  .setInputFiles("receipt.txt");
await expect(page.locator("#nm"))
  .toHaveText("Uploaded
    receipt.txt (13 bytes)");

also: setInputFiles([a, b]),
  { name, mimeType, buffer },
  setInputFiles([]) to clear
Selenium: input.sendKeys(
  absolutePath)

Downloads

For downloads, start waiting for the download event, click export orders, then await the download. Save it to a known path and check it: our run got the file name orders dot csv, and its content was a header row and one order of forty two ninety seven. Checking the content, not just that a file appeared, is what makes it a real test. Selenium needs a browser download directory set in its options, then you wait for the file to appear.

File download
const dlP = page.waitForEvent(
  "download");
await page.getByRole("link",
  { name: "Export orders" })
  .click();
const dl = await dlP;
await dl.saveAs("out/orders.csv");

real: suggestedFilename
  "orders.csv", content
  "id,total\n1,42.97"

iframes

An iframe is a separate document inside the page, common for payment forms, maps, videos and chat widgets. Locators on the main page cannot see inside it. Use a frame locator for the iframe, found here by its title, payment, then locate inside it as usual: fill the card name, click pay, and expect paid to be visible. Our run passed. Locating the card field from the main page finds nothing.

Payment form in an iframe
const pay = page.frameLocator(
  'iframe[title="Payment"]');
await pay.getByLabel("Card name")
  .fill("Alice Shah");
await pay.getByRole("button",
  { name: "Pay" }).click();
await expect(pay.getByText(
  "Paid")).toBeVisible();

page.getByLabel("Card name")
  → not found (other document)

Frames in Selenium

In Selenium, switch into the frame, found by an element locator, by index or by name. Inside it, find elements normally. Afterwards, switch to default content to return to the main page. Nested frames need one switch per level, and the expected condition frame to be available and switch to it waits for slow loading frames. Again, forgetting to switch back is the usual mistake.

Selenium frames
d.switchTo().frame(
  d.findElement(By.cssSelector(
    "iframe[title=Payment]")));
d.findElement(By.id("n"))
  .sendKeys("Alice Shah");
...
d.switchTo().defaultContent();

nested frames: switch one
  level at a time
wait: frameToBeAvailable...

Dialogs

Browser dialogs, alert, confirm and prompt, block the page. Playwright dismisses them automatically unless you handle them. To test one, register a dialog handler before the click, check its message, then accept or dismiss it. In Selenium, wait for the alert to be present, switch to it, read its text and accept or dismiss. Custom in page dialogs are just normal elements, often with the dialog role.

Browser dialogs
// Playwright: auto-dismissed
//   unless you handle them
page.once("dialog", d => {
  expect(d.message()).toContain(
    "Delete");
  d.accept();
});
await deleteButton.click();

// Selenium
d.switchTo().alert().accept();

Shadow DOM

Web components keep their internal elements in a shadow root, separate from the main DOM. Playwright locators look inside open shadow roots automatically, so you rarely notice. Selenium four gets the shadow root from the host element, then finds elements inside it with CSS. Closed shadow roots cannot be reached from outside, so test those components through what users and their public API can see.

Shadow roots
  • web components keep their DOM in a shadow root
  • Playwright: locators pierce OPEN shadow roots automatically
  • Selenium 4: element .getShadowRoot() .findElement(By.css...)
  • closed shadow roots: test via the component's public API

Common mistakes

Mistakes. Starting to wait for a tab or download after the click, so the event is missed. Trying to drive the operating system's file picker. Checking only that a download exists, not what is in it. Forgetting to switch back to the main window or frame in Selenium. Locating iframe content from the main page. And sleeping while files save, instead of waiting for the download event.

Avoid these
  • starting to wait AFTER the click (missed tab/download)
  • driving the OS file picker
  • only checking a download exists, not its content
  • forgetting switchTo back
  • locating iframe content from the main page
  • sleeping for files to save

Say it like this

In the interview: for tabs, I wait for the context's page event together with the click in Playwright, or use window handles and switch to in Selenium. For iframes, a frame locator, or switch to frame and back to default content. Uploads use set input files or sending the path to the file input. Downloads: wait for the download event, save the file and check its content.

In the interview
  • "Tabs: wait for the context's
  • page event together with the click; in Selenium, window
  • handles and switchTo. iframes: frameLocator, or switchTo
  • .frame and defaultContent.
  • Uploads: setInputFiles or sendKeys a path. Downloads: wait for the event, save it
  • and check the content."

Interview questions

Q1How do you handle a new tab in Playwright?

Use Promise.all with context.waitForEvent("page") and the click that opens it; the new tab is a Page object in the same context.

Q2How do you switch windows in Selenium?

Store the current handle, wait for the new window, iterate getWindowHandles(), driver.switchTo().window(handle), and switch back when done.

Q3How do you upload a file in UI automation?

Set it on the file input: setInputFiles(path) in Playwright or sendKeys(absolutePath) on the input in Selenium; never drive the OS dialog.

Q4How do you test a file download?

Wait for the download event (Playwright) or configure a download directory (Selenium), save or locate the file, and assert its name and content.

Q5How do you work with iframes in Playwright?

page.frameLocator(selector) then normal locators inside it; main-page locators do not see iframe content.

Q6How do you work with iframes in Selenium?

driver.switchTo().frame(element, index or name), interact, then driver.switchTo().defaultContent(); nested frames need a switch per level.

Q7How do you handle JavaScript alerts?

Playwright: register page.on("dialog") or once before the action and accept/dismiss; Selenium: switchTo().alert() then accept(), dismiss() or getText().

Q8How do you locate elements inside shadow DOM?

Playwright pierces open shadow roots automatically; Selenium 4 uses element.getShadowRoot() and finds elements within it.