Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
"lint": "eslint --fix index.ts ./src/**/*.ts webpack.config.js",
"lint:check": "eslint index.ts ./src/**/*.ts webpack.config.js",
"format": "prettier --write . webpack.config.js",
"format:check": "prettier --check . webpack.config.js"
"format:check": "prettier --check . webpack.config.js",
"test": "node ./scripts/test-harness.mjs"
},
"description": "Browsertrix Behaviors",
"files": [
Expand All @@ -36,5 +37,9 @@
],
"dependencies": {
"query-selector-shadow-dom": "^1.0.1"
},
"optionalDependencies": {
"memfs": "^4.17.0",
"puppeteer": "^24.7.2"
}
}
72 changes: 72 additions & 0 deletions scripts/test-harness.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import puppeteer from "puppeteer";
import Webpack from "webpack";
import { fs } from "memfs";

import webpackConfig from "../webpack.config.js";

/**
* Validate a URL
* @param {URL} url
* @returns {boolean}
*/
const validateUrl = (url) => {
try {
return new URL(url);
} catch (_e) {
return false;
}
};

if (!process.argv[2]) {
console.error("Usage: yarn test '<url>'");
process.exit(1);
}

if (!validateUrl(process.argv[2])) {
console.error("Invalid URL (hint: include http:// or https://)");
process.exit(1);
}

const config = webpackConfig({}, { mode: "development" });

const compiler = Webpack(config);
compiler.outputFileSystem = fs;

const browser = await puppeteer.launch({ headless: false, devtools: true });
const page = await browser.newPage();

const _watching = compiler.watch({}, async (err, stats) => {
if (err) {
console.error(err);
console.error("Not opening browser");
return;
}
console.log(
stats.toString({
colors: true,
preset: "summary",
}),
);
const behaviorScript = fs.readFileSync("dist/behaviors.js", "utf8");

await page.goto(validateUrl(process.argv[2]));

await page.evaluate(
behaviorScript +
`
self.__bx_behaviors.init({
autofetch: true,
autoplay: true,
autoscroll: true,
siteSpecific: true,
});
`,
);

// call and await run on top frame and all child iframes
await Promise.allSettled(
page
.frames()
.map(async (frame) => frame.evaluate("self.__bx_behaviors.run()")),
);
});
Loading
Loading