Accepting Chrome Extension Permission Dialog with Puppeteer

Posted on Mon Apr 08 2024

Puppeteer is a popular tool for chromium automation, and it is often used for testing web applications. It can also be used for testing Chrome extensions. Chrome extension has a mechanism to request optional permission from users. Most often, it is host permission to access some website. We will see how to do this with puppeteer.

Problem

Puppeteer can only control the browser and page. Permission dialog is a native API, that is currently not accessible programmatically. So we can't use puppeteer itself to accept such dialog. There is a relevant issue on Github about this.

Solution

To accept this dialog, we need to send keyboard events directly to the operating system. More specifically, we need to press Tab and Enter. There are many libraries for this, Github issue talk about using node-key-sender. But this library requires full Java setup, it is just a wrapper around a jar file. Not good enough.

After some searching, I found robotjs suitable for this. But that library is not well maintained. But luckily, there is a fork of this library. Below is the sample code I used for accepting permission dialog.

import Robot from '@hurdlegroup/robotjs';

// some code that triggers permission dialog
// ....
// ....

// accept permission dialog
await wait(1000);
Robot.keyTap('tab');
Robot.keyTap('enter');
await wait(1000);