Repository: electron/electron
Pull request: test: deflake clipboard read/write specs

#### Description of Change

Deflake the following failure:

<img width="953" height="291" alt="Screenshot 2025-07-15 at 11 31 57 AM" src="https://github.com/user-attachments/assets/f0f12839-0fda-4409-92d3-ff23635f4b1a" />

https://github.com/electron/electron/actions/runs/16273281729/job/45949205666?pr=47744

#### Checklist
<!-- Remove items that do not apply. For completed items, change [ ] to [x]. -->

- [x] PR description included and stakeholders cc'd
- [x] `npm test` passes
- [x] tests are [changed or added](https://github.com/electron/electron/blob/main/docs/development/testing.md)
- [x] [PR release notes](https://github.com/electron/clerk/blob/main/README.md) describe the change in a way relevant to app developers, and are [capitalized, punctuated, and past tense](https://github.com/electron/clerk/blob/main/README.md#examples).

#### Release Notes

Notes: none


Changed files:
filename: spec/chromium-spec.ts
patch: @@ -3288,7 +3288,12 @@ describe('navigator.clipboard.read', () => {
     await w.loadFile(path.join(fixturesPath, 'pages', 'blank.html'));
   });
 
-  const readClipboard: any = () => {
+  const readClipboard = async () => {
+    if (!w.webContents.isFocused()) {
+      const focus = once(w.webContents, 'focus');
+      w.webContents.focus();
+      await focus;
+    }
     return w.webContents.executeJavaScript(`
       navigator.clipboard.read().then(clipboard => clipboard.toString()).catch(err => err.message);
     `, true);
@@ -3306,23 +3311,15 @@ describe('navigator.clipboard.read', () => {
 
   it('returns an error when permission denied', async () => {
     session.defaultSession.setPermissionRequestHandler((wc, permission, callback) => {
-      if (permission === 'clipboard-read') {
-        callback(false);
-      } else {
-        callback(true);
-      }
+      callback(permission !== 'clipboard-read');
     });
     const clipboard = await readClipboard();
     expect(clipboard).to.contain('Read permission denied.');
   });
 
   it('returns clipboard contents when permission is granted', async () => {
     session.defaultSession.setPermissionRequestHandler((wc, permission, callback) => {
-      if (permission === 'clipboard-read') {
-        callback(true);
-      } else {
-        callback(false);
-      }
+      callback(permission === 'clipboard-read');
     });
     const clipboard = await readClipboard();
     expect(clipboard).to.not.contain('Read permission denied.');
@@ -3336,7 +3333,12 @@ describe('navigator.clipboard.write', () => {
     await w.loadFile(path.join(fixturesPath, 'pages', 'blank.html'));
   });
 
-  const writeClipboard: any = () => {
+  const writeClipboard = async () => {
+    if (!w.webContents.isFocused()) {
+      const focus = once(w.webContents, 'focus');
+      w.webContents.focus();
+      await focus;
+    }
     return w.webContents.executeJavaScript(`
       navigator.clipboard.writeText('Hello World!').catch(err => err.message);
     `, true);
@@ -3378,7 +3380,13 @@ describe('navigator.clipboard.write', () => {
 });
 
 describe('paste execCommand', () => {
-  const readClipboard: any = (w: BrowserWindow) => {
+  const readClipboard = async (w: BrowserWindow) => {
+    if (!w.webContents.isFocused()) {
+      const focus = once(w.webContents, 'focus');
+      w.webContents.focus();
+      await focus;
+    }
+
     return w.webContents.executeJavaScript(`
       new Promise((resolve) => {
         const timeout = setTimeout(() => {