Skip to content

Commit 554c83b

Browse files
Osong-MichaelOsong Agberndifor
authored andcommitted
Fix flakiness for webkit
- Added code to ensure DOM is ready in webkit
1 parent 5e26a31 commit 554c83b

File tree

2 files changed

+42
-12
lines changed

2 files changed

+42
-12
lines changed

tests/e2e/blocks/secondary-navigation.spec.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,11 @@ test('Test Secondary Navigation block', async ({page, admin, editor}) => {
4141
await expect(page.locator('.EmptyMessage')).toBeVisible();
4242

4343
// Add content (headings and paragraphs).
44-
for (let index = 0; index < HEADINGS.length; index++) {
45-
await addHeadingOrParagraph({page}, 'Heading', 'h2', index, HEADINGS[index]);
46-
// For the paragraphs, we need to use index + 1 because there is a paragraph in the Page Header.
47-
await addHeadingOrParagraph({page}, 'Paragraph', 'p', index + 1, PARAGRAPH_CONTENT);
48-
};
44+
for (const heading of HEADINGS) {
45+
await addHeadingOrParagraph({page}, 'Heading', 'h2', heading);
46+
await page.waitForTimeout(20);
47+
await addHeadingOrParagraph({page}, 'Paragraph', 'p', PARAGRAPH_CONTENT);
48+
}
4949

5050
// Publish page.
5151
await publishPostAndVisit({page, editor});

tests/e2e/tools/lib/editor.js

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,19 @@ async function openComponentPanel({page, editor}, panelTitle) {
2727
* @param {{Page}} page
2828
*/
2929
const closeBlockInserter = async ({page}) => {
30-
const inserter = page.locator('.editor-inserter-sidebar');
30+
const getCloseButton = () => page.getByRole('button', {name: 'Close Block Inserter'});
3131

32-
if (await inserter.isVisible()) {
33-
await page.keyboard.press('Escape');
32+
try {
33+
await expect(getCloseButton()).toBeVisible({timeout: 1000});
34+
await getCloseButton().click();
35+
} catch (error) {
36+
if (process.env.CI) {
37+
// eslint-disable-next-line no-console
38+
console.warn(
39+
'[closeBlockInserter] skipped:',
40+
error?.message?.split('\n')[0]
41+
);
42+
}
3443
}
3544
};
3645

@@ -86,14 +95,35 @@ const searchAndInsertPattern = async ({page}, id) => {
8695
* @param {{Page}} page
8796
* @param {string} blockName
8897
* @param {string} blockTag
89-
* @param {number} number
9098
* @param {string} text
9199
*/
92-
const addHeadingOrParagraph = async ({page}, blockName, blockTag, number, text) => {
100+
const addHeadingOrParagraph = async ({page}, blockName, blockTag, text) => {
93101
await searchAndInsertBlock({page}, blockName, blockName.toLowerCase());
94-
const newBlock = page.getByRole('region', {name: 'Editor content'}).locator(blockTag).nth(number);
95-
await expect(newBlock).toBeVisible();
102+
103+
const getNewBlock = () => page.getByRole('region', {name: 'Editor content'}).locator(`${blockTag}[contenteditable="true"]`).last();
104+
105+
// Wait for Gutenberg to finish inserting and the block to become editable
106+
await page.waitForFunction(
107+
newBlockTag => {
108+
const region = document.querySelector('[role="region"][aria-label="Editor content"]');
109+
if (!region) {return false;}
110+
const blocks = Array.from(region.querySelectorAll(newBlockTag));
111+
return blocks.some(b => b.isContentEditable && b.offsetParent !== null);
112+
},
113+
blockTag,
114+
{timeout: 5000}
115+
);
116+
96117
await closeBlockInserter({page});
118+
119+
// Webkit hack to allow re-render
120+
if (page.context().browser()?.browserType().name() === 'webkit') {
121+
if (!page.isClosed()) {
122+
await page.evaluate(() => new Promise(requestAnimationFrame));
123+
}
124+
}
125+
126+
const newBlock = getNewBlock();
97127
await newBlock.fill(text);
98128
};
99129

0 commit comments

Comments
 (0)