76 lines
2.5 KiB
TypeScript
76 lines
2.5 KiB
TypeScript
import type { Page } from "playwright";
|
|
|
|
async function toggleOnOff({page}:{page:Page}, label: string, on: boolean) : Promise<void>{
|
|
|
|
const row = page.locator("tr").filter({
|
|
has: page.locator("td", { hasText: label }),
|
|
});
|
|
const hideBtn = row.locator(`[title="Désactivé"]`);
|
|
const showBtn = row.locator(`[title="Actif"]`);
|
|
if (!await hideBtn.isVisible() && !await showBtn.isVisible()) {
|
|
throw new Error("Show/Hide button not found");
|
|
}
|
|
if (on && await hideBtn.isVisible()) await hideBtn.click();
|
|
if (!on && await showBtn.isVisible()) await showBtn.click();
|
|
}
|
|
|
|
// Fonction générique `fillForm` pour remplir des formulaires
|
|
async function fillForm<T>(
|
|
{ page, imgFolderPath }: { page: Page; imgFolderPath: string },
|
|
data: T,
|
|
inputNames: Map<keyof T, string>,
|
|
submit: number = 0,
|
|
) {
|
|
for (const [attr, input] of inputNames) {
|
|
const attrValue = data[attr];
|
|
if (!attrValue) continue;
|
|
|
|
const [inputName, inputType] = input.split(":");
|
|
console.log(`${inputName} => '${attrValue}'`);
|
|
switch (inputType) {
|
|
case "select":
|
|
await page.selectOption(`select[name="${inputName}"]`, {
|
|
label: attrValue as string,
|
|
});
|
|
break;
|
|
case "file": {
|
|
const [fileChooser] = await Promise.all([
|
|
page.waitForEvent("filechooser"),
|
|
page.click(`input[name="${inputName}"]`),
|
|
]);
|
|
await fileChooser.setFiles(
|
|
(attrValue as string).replace("$IMG", imgFolderPath),
|
|
);
|
|
break;
|
|
}
|
|
case "cke": { // CKEditor https://ckeditor.com/wysiwyg-editor-open-source/
|
|
const col = page.locator("td", {has: page.locator(`textarea[name="${inputName}"]`)});
|
|
const htmlEditor = col.frameLocator('iframe').locator('body');
|
|
const rawHTMLEditor = col.locator('textarea.cke_editable');
|
|
|
|
const htmlToggleBtn = col.getByRole("button").filter({hasText: /Source/});
|
|
await htmlToggleBtn.click();
|
|
await rawHTMLEditor.fill(attrValue as string);
|
|
await htmlToggleBtn.click();
|
|
await htmlEditor.screenshot({path: 'save.png'});
|
|
break;
|
|
}
|
|
case "toggle":
|
|
await toggleOnOff({page}, inputName, Boolean(JSON.parse(attrValue as string)))
|
|
break;
|
|
default:
|
|
await page.fill(
|
|
`input[name="${inputName}"],textarea[name="${inputName}"]`,
|
|
attrValue as string,
|
|
);
|
|
}
|
|
}
|
|
if (submit > 0) {
|
|
await page.click(`:nth-match(input[type="submit"], ${submit})`);
|
|
}
|
|
}
|
|
|
|
export default {
|
|
fillForm,
|
|
};
|