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.
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.
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.
1import Robot from '@hurdlegroup/robotjs';
2
3// some code that triggers permission dialog
4// ....
5// ....
6
7// accept permission dialog
8await wait(1000);
9Robot.keyTap('tab');
10Robot.keyTap('enter');
11await wait(1000);
12