After 5 hours of reading the vendor code for dusk I was able to figure it out, here is the working code that worked for me in Laravel 9 and 10.
Inside DuskTestCase.php
use Facebook\WebDriver\Chrome\ChromeDevToolsDriver;
protected function grantPermission( \Laravel\Dusk\Browser $browser )
{
try {
$driver = $browser->driver;
$devtools = new ChromeDevToolsDriver($driver);
$result = $devtools->execute('Browser.grantPermissions', [
"permissions" => ["clipboardReadWrite", "clipboardSanitizedWrite"],
]);
return $result;
} catch (\Exception) {
return null;
}
}
public function testClipboard()
{
$this->browse(function ( \Laravel\Dusk\Browser $browser) {
$this->grantPermission($browser );
// Your test flow and assertion
});
}
Then in your Tests when you open the browser closure you will make call to this via parent since it is an extension of this abstract class:
class WhateverItis extends DuskTestCase
{
/**
* A Dusk test example.
*/
protected $preserveGlobalState = FALSE;
protected $runTestInSeparateProcess = TRUE;
public function testExample(): void
{
$this->browse(function (Browser $browser) use ($reviews, $selector_map, $biz, $dusk) {
parent::testClipboard($browser);
$clipboard_data = $browser->script("return navigator.clipboard.readText();");
...rest of code