Skip to content

Commit

Permalink
Puppeteer/screenshot: Clear browser selection before taking screenshot.
Browse files Browse the repository at this point in the history
  • Loading branch information
raineorshine committed Apr 30, 2024
1 parent ee1d5e0 commit a7dbdaa
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions src/e2e/puppeteer/helpers/screenshot.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
import { Page } from 'puppeteer'

/** Refreshes the page. */
const screenshot = (page: Page) => page.screenshot()
/** Takes a screenshot. Note: Clears the browser selection first, as the timing of the blinking caret differs between runs. */
const screenshot = async (page: Page) => {
await page.evaluate(() => {
// For some reason, in headless mode, removeAllRanges is not enough to remove the caret. It will show up in the screenshot at the beginning of the focusNode.
// Blurring the active element works as expected (parallels the implementation of selection.clear).
window.getSelection()?.removeAllRanges()

if (document.activeElement instanceof HTMLElement) {
document.activeElement.blur()
}
})

return page.screenshot()
}

export default screenshot

0 comments on commit a7dbdaa

Please sign in to comment.