Skip to content

Commit

Permalink
Issue #131 - adjust tests to MV3 and SW
Browse files Browse the repository at this point in the history
  • Loading branch information
Manvel committed Jan 1, 2024
1 parent ffd4297 commit 6677010
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 7 deletions.
4 changes: 3 additions & 1 deletion src/js/background/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@
/** @global */
globalThis.browser = require("webextension-polyfill");

require("../analytics");
if (!process.env.MV3) {
require("../analytics");
}
const {CBA} = require("./CBA");
const {playProject} = require("./actions");
const projectsDb = require("../db/projects");
Expand Down
62 changes: 56 additions & 6 deletions tests/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@ const puppeteer = require("puppeteer");
const extensionPath = "dist";
const {tests, server, closeBrowser} = require("./config");

/** @type {import("puppeteer").Browser} */
let browser;
/** @type {import("puppeteer").Page} */
let page;
/** @type {import("puppeteer").Page | import("puppeteer").WebWorker}*/
let backgroundPage;

function run()
Expand All @@ -41,12 +43,7 @@ function run()
"--no-sandbox"
]});
page = await browser.newPage();
const targets = await browser.targets();
const backgroundPageTarget = targets.find((target) =>
target.url().startsWith("chrome-extension://") && target.type() === "background_page"
);

backgroundPage = await backgroundPageTarget.page();
backgroundPage = await waitForBackgroundPage();
const [,, extensionID] = backgroundPage.url().split('/');

await page.setUserAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3419.0 Safari/537.36");
Expand All @@ -66,6 +63,59 @@ function run()
}
}

/**
* Retries function until it returns truthy value or timeout is reached.
* @template T
* @param {function(any): T} fn - Function to execute.
* @param {number} timeout - Timeout in milliseconds.
* @param {number} interval - Interval in milliseconds.
* @returns {Promise<T>}
*/
function retryUntilTruthy(fn, timeout = 5000, interval = 100)
{
return new Promise((resolve, reject) => {
const startTime = Date.now();
const intervalId = setInterval(async () => {
let res;
try {
res = await fn();
}
catch (e) {
res = false;
}
if (res)
{
clearInterval(intervalId);
resolve(res);
}
else if (Date.now() - startTime > timeout)
{
clearInterval(intervalId);
reject(Error("Timeout"));
}
}, interval);
});
}

/**
* Waits for background page to load.
*/
async function waitForBackgroundPage()
{
return retryUntilTruthy(async() => {
const targets = await browser.targets();
const backgroundPageTarget = targets.find((target) => {
return target.url().startsWith("chrome-extension://") && target.type() === "background_page" || target.type() === "service_worker"
}
);
const bgPage = await backgroundPageTarget.page() || await backgroundPageTarget.worker();
if (!bgPage) {
throw new Error("Background page not found");
}
return bgPage;
})
}

async function navigateTo(path)
{
return page.goto(path);
Expand Down

0 comments on commit 6677010

Please sign in to comment.