Refactor the copy/paste logic in the integration tests

The integration tests are currently not consistent in how they do
copy/pasting: some tests use the `kbCopy`/`kbPaste` functions with
waiting for the event inline, some have their own helper function to
combine those actions and some even call `kbCopy`/`kbPaste` without
waiting for the event at all (which can cause intermittent failures).

This commit fixes the issues by providing a set of four helper functions
that all tests use and that abstract e.g. waiting for the event away
from the caller. This makes the invididual tests simpler and consistent,
reduces code duplication and fixes possible intermittent failures
due to not waiting for events to trigger.
This commit is contained in:
Tim van der Meij 2024-06-21 20:37:01 +02:00
parent 2fbd61944b
commit 55ba4aa66a
No known key found for this signature in database
GPG key ID: 8C3FD2925A5F2762
4 changed files with 69 additions and 85 deletions

View file

@ -16,6 +16,8 @@
import {
awaitPromise,
closePages,
copy,
copyToClipboard,
createPromise,
dragAndDropAnnotation,
firstPageOnTop,
@ -30,21 +32,19 @@ import {
kbBigMoveLeft,
kbBigMoveRight,
kbBigMoveUp,
kbCopy,
kbGoToBegin,
kbGoToEnd,
kbModifierDown,
kbModifierUp,
kbPaste,
kbRedo,
kbSelectAll,
kbUndo,
loadAndWait,
paste,
pasteFromClipboard,
scrollIntoView,
switchToEditor,
waitForAnnotationEditorLayer,
waitForEvent,
waitForSelectedEditor,
waitForSerialized,
waitForStorageEntries,
@ -53,16 +53,6 @@ import {
} from "./test_utils.mjs";
import { PNG } from "pngjs";
const copyPaste = async page => {
let promise = waitForEvent(page, "copy");
await kbCopy(page);
await promise;
promise = waitForEvent(page, "paste");
await kbPaste(page);
await promise;
};
const selectAll = async page => {
await kbSelectAll(page);
await page.waitForFunction(
@ -187,7 +177,8 @@ describe("FreeText Editor", () => {
);
await waitForSelectedEditor(page, getEditorSelector(0));
await copyPaste(page);
await copy(page);
await paste(page);
await page.waitForSelector(getEditorSelector(1), {
visible: true,
});
@ -203,7 +194,8 @@ describe("FreeText Editor", () => {
expect(pastedContent).withContext(`In ${browserName}`).toEqual(content);
await copyPaste(page);
await copy(page);
await paste(page);
await page.waitForSelector(getEditorSelector(2), {
visible: true,
});
@ -263,7 +255,8 @@ describe("FreeText Editor", () => {
);
await waitForSelectedEditor(page, getEditorSelector(3));
await copyPaste(page);
await copy(page);
await paste(page);
await page.waitForSelector(getEditorSelector(4), {
visible: true,
});
@ -276,9 +269,7 @@ describe("FreeText Editor", () => {
);
for (let i = 0; i < 2; i++) {
const promise = waitForEvent(page, "paste");
await kbPaste(page);
await promise;
await paste(page);
await page.waitForSelector(getEditorSelector(5 + i));
}
@ -597,7 +588,8 @@ describe("FreeText Editor", () => {
.withContext(`In ${browserName}`)
.toEqual([0, 1, 3]);
await copyPaste(page);
await copy(page);
await paste(page);
await page.waitForSelector(getEditorSelector(6), {
visible: true,
});
@ -1275,7 +1267,8 @@ describe("FreeText Editor", () => {
);
await waitForSelectedEditor(page, getEditorSelector(1));
await copyPaste(page);
await copy(page);
await paste(page);
await page.waitForSelector(getEditorSelector(6), {
visible: true,
});
@ -3425,14 +3418,11 @@ describe("FreeText Editor", () => {
);
await select(0);
await pasteFromClipboard(
page,
{
"text/html": "<b>Bold Foo</b>",
"text/plain": "Foo",
},
`${editorSelector} .internal`
);
await copyToClipboard(page, {
"text/html": "<b>Bold Foo</b>",
"text/plain": "Foo",
});
await pasteFromClipboard(page, `${editorSelector} .internal`);
let lastText = data;
@ -3442,14 +3432,11 @@ describe("FreeText Editor", () => {
expect(text).withContext(`In ${browserName}`).toEqual(lastText);
await select(3);
await pasteFromClipboard(
page,
{
"text/html": "<b>Bold Bar</b><br><b>Oof</b>",
"text/plain": "Bar\nOof",
},
`${editorSelector} .internal`
);
await copyToClipboard(page, {
"text/html": "<b>Bold Bar</b><br><b>Oof</b>",
"text/plain": "Bar\nOof",
});
await pasteFromClipboard(page, `${editorSelector} .internal`);
await waitForTextChange(lastText, editorSelector);
text = await getText(editorSelector);
@ -3457,13 +3444,8 @@ describe("FreeText Editor", () => {
expect(text).withContext(`In ${browserName}`).toEqual(lastText);
await select(0);
await pasteFromClipboard(
page,
{
"text/html": "<b>basic html</b>",
},
`${editorSelector} .internal`
);
await copyToClipboard(page, { "text/html": "<b>basic html</b>" });
await pasteFromClipboard(page, `${editorSelector} .internal`);
// Nothing should change, so it's hard to wait on something.
// eslint-disable-next-line no-restricted-syntax
@ -3477,15 +3459,12 @@ describe("FreeText Editor", () => {
const prevHTML = await getHTML();
// Try to paste an image.
await pasteFromClipboard(
page,
{
"image/png":
// 1x1 transparent png.
"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8BQDwAEhQGAhKmMIQAAAABJRU5ErkJggg==",
},
`${editorSelector} .internal`
);
await copyToClipboard(page, {
"image/png":
// 1x1 transparent png.
"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8BQDwAEhQGAhKmMIQAAAABJRU5ErkJggg==",
});
await pasteFromClipboard(page, `${editorSelector} .internal`);
// Nothing should change, so it's hard to wait on something.
// eslint-disable-next-line no-restricted-syntax
@ -3505,14 +3484,11 @@ describe("FreeText Editor", () => {
});
const fooBar = "Foo\nBar\nOof";
await pasteFromClipboard(
page,
{
"text/html": "<b>html</b>",
"text/plain": fooBar,
},
`${editorSelector} .internal`
);
await copyToClipboard(page, {
"text/html": "<b>html</b>",
"text/plain": fooBar,
});
await pasteFromClipboard(page, `${editorSelector} .internal`);
await waitForTextChange("", editorSelector);
text = await getText(editorSelector);