Running Microsoft Edge on a machine with policies might get you some headache so here are 2 keys in registry you might need to tweak.
Both of the keys I am about to talk can be found in registry on path
[HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\Edge]
(just for the record my machine is latest Windows 11 and latest Microsoft Edge, the keys can move in the future)
The error from Playwright
Error: browserType.launch: Target page, context or browser has been closed
Browser logs:
<launching> C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe --disable-field-trial-config .....
<launched> pid=22664
[pid=22664][err]
[pid=22664][err] DevTools remote debugging is disallowed by the system admin.
[pid=22664][err] [22664:36100:0522/154153.935:ERROR:chrome\browser\ui\startup\startup_browser_creator.cc:1138] Headless mode is disallowed by the system admin.
Call log:
- <launching> C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe --disable-field-trial-config ....
- <launched> pid=22664
- [pid=22664][err]
- [pid=22664][err] DevTools remote debugging is disallowed by the system admin.
- [pid=22664][err] [22664:36100:0522/154153.935:ERROR:chrome\browser\ui\startup\startup_browser_creator.cc:1138] Headless mode is disallowed by the system admin.
Looking at this you can see two separate problems :
Headless mode is disallowed by the system admin.
DevTools remote debugging is disallowed by the system admin.
HeadlessModeEnabled needs to be set to 1
First of all, in order to start Microsoft Edge in headless mode you need to have HeadlessModeEnabled set to 1. This will let Playwright open the browser without UI.
More on this key here https://learn.microsoft.com/en-us/deployedge/microsoft-edge-browser-policies/headlessmodeenabled.
RemoteDebuggingAllowed needs to be set to 1
Secondly to enable any tool to talk to Microsoft Edge via this https://learn.microsoft.com/en-us/microsoft-edge/devtools-protocol-chromium/ we also need this flag to be set to one.
Since you might not need headless mode and just run headed tests you can only set the second flag and you should be ready to go.
More on this key here https://learn.microsoft.com/en-us/deployedge/microsoft-edge-browser-policies/remotedebuggingallowed.
.reg file anyone?
In case you need to quickly import these both props to register just save this content as reg file and execute it as admin.
Windows Registry Editor Version 5.00
[HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\Edge]
"HeadlessModeEnabled"=dword:00000001
"RemoteDebuggingAllowed"=dword:00000001
Hope this helps.