Skip to content

Commit

Permalink
Add testing for grammar wiki page
Browse files Browse the repository at this point in the history
  • Loading branch information
mtusnio committed Aug 26, 2024
1 parent e9d7aec commit e8f2219
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 10 deletions.
22 changes: 22 additions & 0 deletions browser_tests/popup.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,3 +137,25 @@ test("prints out a different HTML when hovering over 有 in an HTML-rich site wi

expect(windowHTML).toEqual("<span class=\"w-hanzi\">有</span>&nbsp;<br><span class=\"tone3 w-zhuyin\">ㄧㄡˇ</span><br><span class=\"w-def\">to have/there is/there are/to exist/to be</span><br>")
})

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()
})
19 changes: 18 additions & 1 deletion browser_tests/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 };

10 changes: 1 addition & 9 deletions browser_tests/wordlist.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit e8f2219

Please sign in to comment.