From e8f22190f14cb2eef325a8f535bb0d8bb2320e09 Mon Sep 17 00:00:00 2001 From: Michal Tusnio Date: Mon, 26 Aug 2024 14:52:30 +0100 Subject: [PATCH] Add testing for grammar wiki page --- browser_tests/popup.test.js | 22 ++++++++++++++++++++++ browser_tests/utils.js | 19 ++++++++++++++++++- browser_tests/wordlist.test.js | 10 +--------- 3 files changed, 41 insertions(+), 10 deletions(-) diff --git a/browser_tests/popup.test.js b/browser_tests/popup.test.js index 1e3ca1c..50fe890 100644 --- a/browser_tests/popup.test.js +++ b/browser_tests/popup.test.js @@ -137,3 +137,25 @@ test("prints out a different HTML when hovering over 有 in an HTML-rich site wi expect(windowHTML).toEqual(" 
ㄧㄡˇ
to have/there is/there are/to exist/to be
") }) + +test("pressing the grammar shortcut loads up grammar wiki", async () => { + const page = await browser.newPage(); + await page.goto(`file://${path.resolve()}/browser_tests/testdata/wiki-you.html`, { waitUntil: ['domcontentloaded', "networkidle2"] }); + await page.bringToFront(); + + await utils.toggleExtension(worker) + await utils.wait(500) + + await page.setViewport({ width: 1280, height: 720 }); + await utils.wait(500) + + const targetSelector = 'li.spaced ::-p-text(今天) em' + await page.waitForSelector(targetSelector, { timeout: 6000 }) + await page.locator(targetSelector).hover(); + await page.waitForSelector(utils.ZHONGWEN_WINDOW_SELECTOR, { timeout: 6000 }); + + await page.keyboard.press("g") + + const grammarPage = await utils.findOpenedPage(browser, `https://resources.allsetlearning.com/chinese/grammar/%E6%9C%89`) + expect(grammarPage).not.toBeNull() +}) diff --git a/browser_tests/utils.js b/browser_tests/utils.js index 1ca2356..cdf32d5 100644 --- a/browser_tests/utils.js +++ b/browser_tests/utils.js @@ -64,5 +64,22 @@ async function wait(miliseconds) { await new Promise((r) => setTimeout(r, miliseconds)); } -export { EXTENSION_ID, EXTENSION_PATH, ZHONGWEN_WINDOW_SELECTOR, getExtensionStatus, setupBrowser, toggleExtension, wait }; +async function findOpenedPage(browser, url) { + const page = await (await browser.pages()).findLast(async (page) => { + const pageURL = await page.url() + + if (pageURL == url) { + return true + } + return false + }) + + if (page == undefined) { + return null + } + + return page +} + +export { EXTENSION_ID, EXTENSION_PATH, ZHONGWEN_WINDOW_SELECTOR, findOpenedPage, getExtensionStatus, setupBrowser, toggleExtension, wait }; diff --git a/browser_tests/wordlist.test.js b/browser_tests/wordlist.test.js index f7e8870..2b17ae5 100644 --- a/browser_tests/wordlist.test.js +++ b/browser_tests/wordlist.test.js @@ -48,15 +48,7 @@ test("pressing wordlist button shows popup and adds the word to the wordlist", a await utils.wait(1000) - - page = await (await browser.pages()).findLast(async (page) => { - const url = await page.url() - - if (url == `chrome-extension://${utils.EXTENSION_ID}/options.html`) { - return true - } - return false - }) + page = await utils.findOpenedPage(browser, `chrome-extension://${utils.EXTENSION_ID}/options.html`) await page.bringToFront() expect(await page).not.toEqual(undefined)