From b5f8ed15eb6ba4cf3282521880675af674b7d6f9 Mon Sep 17 00:00:00 2001 From: deepak1556 Date: Wed, 14 Jul 2021 01:47:10 -0700 Subject: [PATCH] chore: improve found-in-page spec --- spec-main/webview-spec.ts | 50 +++++++++++++++++++++++---------------- 1 file changed, 30 insertions(+), 20 deletions(-) diff --git a/spec-main/webview-spec.ts b/spec-main/webview-spec.ts index f9715340092b6..f6a696ea7fca5 100644 --- a/spec-main/webview-spec.ts +++ b/spec-main/webview-spec.ts @@ -748,37 +748,47 @@ describe(' tag', function () { describe('found-in-page event', () => { let w: BrowserWindow; beforeEach(async () => { - w = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true, webviewTag: true, contextIsolation: false } }); + w = new BrowserWindow({ show: false, webPreferences: { webviewTag: true, contextIsolation: false } }); await w.loadURL('about:blank'); }); afterEach(closeAllWindows); it('emits when a request is made', async () => { - loadWebView(w.webContents, { + await loadWebView(w.webContents, { src: `file://${fixtures}/pages/content.html` }); - const [, webViewContents] = await emittedOnce(app, 'web-contents-created'); - const activeMatchOrdinal = []; - let isFirstRequest = true; + const result = await w.webContents.executeJavaScript(`new Promise((resolve, reject) => { + const webview = document.querySelector('webview') - for (;;) { - const foundInPage = emittedOnce(webViewContents, 'found-in-page'); - const requestId = webViewContents.findInPage('virtual', { findNext: isFirstRequest }); - const [, result] = await foundInPage; - isFirstRequest = false; - - expect(result.requestId).to.equal(requestId); - expect(result.matches).to.equal(3); - - activeMatchOrdinal.push(result.activeMatchOrdinal); + const waitForEvent = (target, eventName) => { + return new Promise(resolve => { + target.addEventListener(eventName, resolve, { once: true }) + }) + } - if (result.activeMatchOrdinal === result.matches) { - break; + async function startFind() { + const activeMatchOrdinal = [] + const foundInPage = waitForEvent(webview, 'found-in-page') + webview.findInPage('virtual', { findNext: true }) + const event = await foundInPage + if (event.result.matches) { + activeMatchOrdinal.push(event.result.activeMatchOrdinal) + + for (let i=1; i < event.result.matches; i++) { + const foundInPage = waitForEvent(webview, 'found-in-page') + webview.findInPage('virtual', { findNext: false }) + const event = await foundInPage + activeMatchOrdinal.push(event.result.activeMatchOrdinal) + } + } + webview.stopFindInPage('clearSelection') + resolve(activeMatchOrdinal) } - } - expect(activeMatchOrdinal).to.deep.equal([1, 2, 3]); - webViewContents.stopFindInPage('clearSelection'); + webview.focus() + startFind() + })`); + expect(result).to.deep.equal([1, 2, 3]); }); }); });