-
Notifications
You must be signed in to change notification settings - Fork 124
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Puppeteer/screenshot: Clear browser selection before taking screenshot.
- Loading branch information
1 parent
ee1d5e0
commit a7dbdaa
Showing
1 changed file
with
14 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |