2022-03-21 23:04:43 +08:00
|
|
|
const { expect } = require("@playwright/test");
|
2023-05-11 03:51:05 +08:00
|
|
|
const { exec } = require('child_process');
|
2022-03-21 23:04:43 +08:00
|
|
|
|
2021-11-26 02:23:58 +08:00
|
|
|
// Common
|
2021-12-01 22:02:26 +08:00
|
|
|
function checkElement([element, index = 0]) {
|
2022-08-20 07:57:17 +08:00
|
|
|
/* Because this function is passed to a page.evaluate, it can only
|
|
|
|
* take a single argument; that's why we pass it an array. It's so
|
|
|
|
* easy to pass it a string by mistake that we check to make sure
|
|
|
|
* the second argument is an integer and not a character from a
|
|
|
|
* destructured string.
|
|
|
|
*/
|
|
|
|
if (typeof index != "number") {
|
|
|
|
throw Error("Assert failed: index not a number");
|
|
|
|
}
|
2021-11-26 02:23:58 +08:00
|
|
|
return document.querySelectorAll(element)[index] !== undefined;
|
|
|
|
}
|
|
|
|
|
2021-12-02 12:12:14 +08:00
|
|
|
// Length
|
2021-12-01 13:36:20 +08:00
|
|
|
function checkElementLengthEqualTo([element, count]) {
|
|
|
|
return document.querySelectorAll(element).length == count;
|
|
|
|
}
|
|
|
|
|
2022-09-13 01:10:57 +08:00
|
|
|
function getElementLength(element) {
|
|
|
|
return document.querySelectorAll(element).length;
|
|
|
|
}
|
|
|
|
|
2022-03-21 23:04:43 +08:00
|
|
|
// Text
|
|
|
|
async function checkTextContent(baseContent, checkData) {
|
2022-03-29 21:53:07 +08:00
|
|
|
if (typeof checkData === 'string') checkData = new Array(checkData);
|
2022-03-21 23:04:43 +08:00
|
|
|
|
|
|
|
const check = checkData.every(word => baseContent.includes(word));
|
|
|
|
await expect(check).toBeTruthy();
|
|
|
|
}
|
|
|
|
|
2023-03-08 23:33:05 +08:00
|
|
|
function constructClipObj(wbBox) {
|
|
|
|
return {
|
2023-04-06 05:02:40 +08:00
|
|
|
x: wbBox.x,
|
|
|
|
y: wbBox.y,
|
|
|
|
width: wbBox.width,
|
|
|
|
height: wbBox.height,
|
|
|
|
};
|
2023-03-08 23:33:05 +08:00
|
|
|
}
|
|
|
|
|
2023-05-11 03:51:05 +08:00
|
|
|
async function runScript(script, { handleError, handleOutput, timeout }) {
|
|
|
|
return new Promise((res, rej) => {
|
|
|
|
return exec(script, { timeout }, (err, stdout, stderr) => {
|
|
|
|
res(handleError ? handleError(stderr) : handleOutput ? handleOutput(stdout) : null)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-12-01 13:36:20 +08:00
|
|
|
exports.checkElement = checkElement;
|
2021-12-04 01:01:36 +08:00
|
|
|
exports.checkElementLengthEqualTo = checkElementLengthEqualTo;
|
2022-09-13 01:10:57 +08:00
|
|
|
exports.getElementLength = getElementLength;
|
2022-03-21 23:04:43 +08:00
|
|
|
exports.checkTextContent = checkTextContent;
|
2023-03-08 23:33:05 +08:00
|
|
|
exports.constructClipObj = constructClipObj;
|
2023-05-11 03:51:05 +08:00
|
|
|
exports.runScript = runScript;
|