feat: WebContents.focusedFrame (#45667)

feat: webContents.focusedFrame
This commit is contained in:
Sam Maddock 2025-03-04 11:38:58 -05:00 committed by GitHub
parent 54136042c6
commit 8c11764800
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 44 additions and 1 deletions

View file

@ -1825,6 +1825,38 @@ describe('webContents module', () => {
});
});
describe('focusedFrame api', () => {
const focusFrame = (frame: Electron.WebFrameMain) => {
// There has to be a better way to do this...
return frame.executeJavaScript(`(${() => {
const input = document.createElement('input');
document.body.appendChild(input);
input.onfocus = () => input.remove();
input.focus();
}})()`, true);
};
it('is null before a url is committed', () => {
const w = new BrowserWindow({ show: false });
expect(w.webContents.focusedFrame).to.be.null();
});
it('is set when main frame is focused', async () => {
const w = new BrowserWindow({ show: true });
await w.loadURL('about:blank');
w.webContents.focus();
await waitUntil(() => w.webContents.focusedFrame === w.webContents.mainFrame);
});
it('is set to child frame when focused', async () => {
const w = new BrowserWindow({ show: true });
await w.loadFile(path.join(fixturesPath, 'sub-frames', 'frame-with-frame-container.html'));
const childFrame = w.webContents.mainFrame.frames[0];
await focusFrame(childFrame);
await waitUntil(() => w.webContents.focusedFrame === childFrame);
});
});
describe('render view deleted events', () => {
let server: http.Server;
let serverUrl: string;