summaryrefslogtreecommitdiff
path: root/test/playwright
diff options
context:
space:
mode:
Diffstat (limited to 'test/playwright')
-rw-r--r--test/playwright/integration.spec.js13
-rw-r--r--test/playwright/playwright-util.js11
-rw-r--r--test/playwright/visual.spec.js12
3 files changed, 29 insertions, 7 deletions
diff --git a/test/playwright/integration.spec.js b/test/playwright/integration.spec.js
index b9a86d84..c8cf2f6d 100644
--- a/test/playwright/integration.spec.js
+++ b/test/playwright/integration.spec.js
@@ -45,7 +45,8 @@ test('search clipboard', async ({page, extensionId}) => {
test('anki add', async ({context, page, extensionId}) => {
// mock anki routes
- let resolve;
+ /** @type {?(value: unknown) => void} */
+ let resolve = null;
const addNotePromise = new Promise((res) => {
resolve = res;
});
@@ -53,7 +54,7 @@ test('anki add', async ({context, page, extensionId}) => {
mockAnkiRouteHandler(route);
const req = route.request();
if (req.url().includes('127.0.0.1:8765') && req.postDataJSON().action === 'addNote') {
- resolve(req.postDataJSON());
+ /** @type {(value: unknown) => void} */ (resolve)(req.postDataJSON());
}
});
@@ -63,7 +64,11 @@ test('anki add', async ({context, page, extensionId}) => {
// load in test dictionary
const dictionary = createDictionaryArchive(path.join(root, 'test/data/dictionaries/valid-dictionary1'), 'valid-dictionary1');
const testDictionarySource = await dictionary.generateAsync({type: 'arraybuffer'});
- await page.locator('input[id="dictionary-import-file-input"]').setInputFiles({name: 'valid-dictionary1.zip', buffer: Buffer.from(testDictionarySource)});
+ await page.locator('input[id="dictionary-import-file-input"]').setInputFiles({
+ name: 'valid-dictionary1.zip',
+ mimeType: 'application/x-zip',
+ buffer: Buffer.from(testDictionarySource)
+ });
await expect(page.locator('id=dictionaries')).toHaveText('Dictionaries (1 installed, 1 enabled)', {timeout: 5 * 60 * 1000});
// connect to anki
@@ -75,7 +80,7 @@ test('anki add', async ({context, page, extensionId}) => {
await page.locator('select.anki-card-deck').selectOption('Mock Deck');
await page.locator('select.anki-card-model').selectOption('Mock Model');
for (const modelField of mockModelFieldNames) {
- await page.locator(`[data-setting="anki.terms.fields.${modelField}"]`).fill(mockModelFieldsToAnkiValues[modelField]);
+ await page.locator(`[data-setting="anki.terms.fields.${modelField}"]`).fill(/** @type {string} */ (mockModelFieldsToAnkiValues[modelField]));
}
await page.locator('#anki-cards-modal > div > div.modal-footer > button:nth-child(2)').click();
await writeToClipboardFromPage(page, '読むの例文');
diff --git a/test/playwright/playwright-util.js b/test/playwright/playwright-util.js
index 5ceb92fd..ac68db4d 100644
--- a/test/playwright/playwright-util.js
+++ b/test/playwright/playwright-util.js
@@ -55,6 +55,7 @@ export const mockModelFieldNames = [
'Sentence'
];
+/** @type {{[key: string]: string|undefined}} */
export const mockModelFieldsToAnkiValues = {
'Word': '{expression}',
'Reading': '{furigana-plain}',
@@ -62,6 +63,10 @@ export const mockModelFieldsToAnkiValues = {
'Audio': '{audio}'
};
+/**
+ * @param {import('playwright').Route} route
+ * @returns {Promise<void>|undefined}
+ */
export const mockAnkiRouteHandler = (route) => {
const reqBody = route.request().postDataJSON();
const respBody = ankiRouteResponses[reqBody.action];
@@ -71,6 +76,11 @@ export const mockAnkiRouteHandler = (route) => {
route.fulfill(respBody);
};
+/**
+ * @param {import('playwright').Page} page
+ * @param {string} text
+ * @returns {Promise<void>}
+ */
export const writeToClipboardFromPage = async (page, text) => {
await page.evaluate(`navigator.clipboard.writeText('${text}')`);
};
@@ -100,6 +110,7 @@ const baseAnkiResp = {
contentType: 'text/json'
};
+/** @type {{[key: string]: import('core').SerializableObject}} */
const ankiRouteResponses = {
'version': Object.assign({body: JSON.stringify(6)}, baseAnkiResp),
'deckNames': Object.assign({body: JSON.stringify(['Mock Deck'])}, baseAnkiResp),
diff --git a/test/playwright/visual.spec.js b/test/playwright/visual.spec.js
index 2f46990f..8b48b7c0 100644
--- a/test/playwright/visual.spec.js
+++ b/test/playwright/visual.spec.js
@@ -48,10 +48,16 @@ test('visual', async ({page, extensionId}) => {
// take a screenshot of the settings page with jmdict loaded
await expect.soft(page).toHaveScreenshot('settings-jmdict-loaded.png', {mask: [storage_locator]});
+ /**
+ * @param {number} doc_number
+ * @param {number} test_number
+ * @param {import('@playwright/test').ElementHandle<Node>} el
+ * @param {{x: number, y: number}} offset
+ */
const screenshot = async (doc_number, test_number, el, offset) => {
const test_name = 'doc' + doc_number + '-test' + test_number;
- const box = await el.boundingBox();
+ const box = (await el.boundingBox()) || {x: 0, y: 0, width: 0, height: 0};
// find the popup frame if it exists
let popup_frame = page.frames().find((f) => f.url().includes('popup.html'));
@@ -66,7 +72,7 @@ test('visual', async ({page, extensionId}) => {
popup_frame = await frame_attached; // wait for popup to be attached
}
try {
- await (await popup_frame.frameElement()).waitForElementState('visible', {timeout: 500}); // some tests don't have a popup, so don't fail if it's not there; TODO: check if the popup is expected to be there
+ await (await /** @type {import('@playwright/test').Frame} */ (popup_frame).frameElement()).waitForElementState('visible', {timeout: 500}); // some tests don't have a popup, so don't fail if it's not there; TODO: check if the popup is expected to be there
} catch (error) {
console.log(test_name + ' has no popup');
}
@@ -75,7 +81,7 @@ test('visual', async ({page, extensionId}) => {
await expect.soft(page).toHaveScreenshot(test_name + '.png');
await page.mouse.click(0, 0); // click away so popup disappears
- await (await popup_frame.frameElement()).waitForElementState('hidden'); // wait for popup to disappear
+ await (await /** @type {import('@playwright/test').Frame} */ (popup_frame).frameElement()).waitForElementState('hidden'); // wait for popup to disappear
};
// Load test-document1.html