From 2a9a6855f432f7aa22f95f9fdf41d6e69aab9329 Mon Sep 17 00:00:00 2001 From: Peng Xiao Date: Wed, 13 Dec 2023 07:51:59 +0000 Subject: [PATCH] test(core): rewrite some flaky assertions (#5287) --- tests/affine-desktop/e2e/basic.spec.ts | 8 +++---- tests/affine-local/e2e/drag-page.spec.ts | 2 +- tests/affine-local/e2e/duplicate-page.spec.ts | 7 +++--- tests/affine-local/e2e/image-preview.spec.ts | 10 ++++---- .../e2e/local-first-collections-items.spec.ts | 4 ++-- tests/affine-local/e2e/shortcuts.spec.ts | 2 +- tests/kit/utils/cloud.ts | 23 ++----------------- 7 files changed, 19 insertions(+), 37 deletions(-) diff --git a/tests/affine-desktop/e2e/basic.spec.ts b/tests/affine-desktop/e2e/basic.spec.ts index 1089934aca..c9808d9dfc 100644 --- a/tests/affine-desktop/e2e/basic.spec.ts +++ b/tests/affine-desktop/e2e/basic.spec.ts @@ -142,19 +142,19 @@ test('affine onboarding button', async ({ page }) => { await page.getByTestId('help-island').click(); await page.getByTestId('easy-guide').click(); const onboardingModal = page.locator('[data-testid=onboarding-modal]'); - expect(await onboardingModal.isVisible()).toEqual(true); + await expect(onboardingModal).toBeVisible(); const switchVideo = page.locator( '[data-testid=onboarding-modal-switch-video]' ); - expect(await switchVideo.isVisible()).toEqual(true); + await expect(switchVideo).toBeVisible(); await page.getByTestId('onboarding-modal-next-button').click(); const editingVideo = page.locator( '[data-testid=onboarding-modal-editing-video]' ); - expect(await editingVideo.isVisible()).toEqual(true); + await expect(editingVideo).toBeVisible(); await page.getByTestId('onboarding-modal-close-button').click(); - expect(await onboardingModal.isVisible()).toEqual(false); + await expect(onboardingModal).toBeHidden(); }); test('windows only check', async ({ page }) => { diff --git a/tests/affine-local/e2e/drag-page.spec.ts b/tests/affine-local/e2e/drag-page.spec.ts index c0ac7e4869..da3376de06 100644 --- a/tests/affine-local/e2e/drag-page.spec.ts +++ b/tests/affine-local/e2e/drag-page.spec.ts @@ -24,7 +24,7 @@ const dragToFavourites = async ( const dragToCollection = async (page: Page, dragItem: Locator) => { await page.getByTestId('slider-bar-add-collection-button').click(); const input = page.getByTestId('input-collection-title'); - await input.isVisible(); + await expect(input).toBeVisible(); await input.fill('test collection'); await page.getByTestId('save-collection').click(); const collection = page.getByTestId('collection-item'); diff --git a/tests/affine-local/e2e/duplicate-page.spec.ts b/tests/affine-local/e2e/duplicate-page.spec.ts index 71e6b70890..0f48ee9d01 100644 --- a/tests/affine-local/e2e/duplicate-page.spec.ts +++ b/tests/affine-local/e2e/duplicate-page.spec.ts @@ -13,10 +13,11 @@ test('Duplicate page should work', async ({ page }) => { await waitForEditorLoad(page); await clickNewPageButton(page); const title = getBlockSuiteEditorTitle(page); - await title.pressSequentially('test'); + await expect(title).toBeVisible(); + await title.fill('test'); await clickPageMoreActions(page); const duplicateButton = page.getByTestId('editor-option-menu-duplicate'); - await duplicateButton.click({ delay: 100 }); + await duplicateButton.click(); const title2 = getBlockSuiteEditorTitle(page); - await expect(title2).toHaveText('test(1)', { timeout: 1000 }); + await expect(title2).toHaveText('test(1)'); }); diff --git a/tests/affine-local/e2e/image-preview.spec.ts b/tests/affine-local/e2e/image-preview.spec.ts index 118952323a..6afd7f007b 100644 --- a/tests/affine-local/e2e/image-preview.spec.ts +++ b/tests/affine-local/e2e/image-preview.spec.ts @@ -82,7 +82,7 @@ test('image go left and right', async ({ page }) => { await importImage(page, 'http://localhost:8081/affine-preview.png'); } const locator = page.getByTestId('image-preview-modal'); - expect(locator.isVisible()).toBeTruthy(); + await expect(locator).toBeHidden(); await page.locator('img').first().dblclick(); await page.waitForTimeout(1000); { @@ -125,7 +125,7 @@ test('image able to zoom in and out with mouse scroll', async ({ page }) => { const naturalWidth = await locator.evaluate( (img: HTMLImageElement) => img.naturalWidth ); - expect(locator.isVisible()).toBeTruthy(); + await expect(locator).toBeVisible(); const { width, height } = await page.evaluate(() => ({ width: window.innerWidth, height: window.innerHeight, @@ -660,10 +660,10 @@ test('keypress esc should close the modal', async ({ page }) => { await importImage(page, 'http://localhost:8081/large-image.png'); await page.locator('img').first().dblclick(); const locator = page.getByTestId('image-preview-modal'); - expect(locator.isVisible()).toBeTruthy(); + await expect(locator).toBeVisible(); await page.keyboard.press('Escape'); await page.waitForTimeout(1000); - expect(await locator.isVisible()).toBeFalsy(); + await expect(locator).toBeHidden(); }); test('when mouse moves outside, the modal should be closed', async ({ @@ -706,7 +706,7 @@ test('caption should be visible and different styles were applied if image zoome await page.getByPlaceholder('Write a caption').fill(sampleCaption); await page.locator('img').first().dblclick(); const locator = page.getByTestId('image-preview-modal'); - expect(await locator.isVisible()).toBeTruthy(); + await expect(locator).toBeVisible(); await page.waitForTimeout(1000); let captionLocator = locator.getByTestId('image-caption-zoomedout'); await expect(captionLocator).toBeVisible(); diff --git a/tests/affine-local/e2e/local-first-collections-items.spec.ts b/tests/affine-local/e2e/local-first-collections-items.spec.ts index ada071d86a..3bb07e0fe7 100644 --- a/tests/affine-local/e2e/local-first-collections-items.spec.ts +++ b/tests/affine-local/e2e/local-first-collections-items.spec.ts @@ -43,7 +43,7 @@ const createAndPinCollection = async ( delay: 200, }); const title = page.getByTestId('input-collection-title'); - await title.isVisible(); + await expect(title).toBeVisible(); await title.fill(options?.collectionName ?? 'test collection'); await page.getByTestId('save-collection').click(); await page.waitForTimeout(100); @@ -162,7 +162,7 @@ test('add collection from sidebar', async ({ page }) => { await expect(nullCollection).toBeVisible(); await page.getByTestId('slider-bar-add-collection-button').click(); const title = page.getByTestId('input-collection-title'); - await title.isVisible(); + await expect(title).toBeVisible(); await title.fill('test collection'); await page.getByTestId('save-collection').click(); await page.waitForTimeout(100); diff --git a/tests/affine-local/e2e/shortcuts.spec.ts b/tests/affine-local/e2e/shortcuts.spec.ts index 808cf7cac9..384d8d13db 100644 --- a/tests/affine-local/e2e/shortcuts.spec.ts +++ b/tests/affine-local/e2e/shortcuts.spec.ts @@ -10,7 +10,7 @@ test('Open shortcuts modal', async ({ page }) => { const shortcutsIcon = page.locator('[data-testid=shortcuts-icon]'); await page.waitForTimeout(1000); - expect(await shortcutsIcon.isVisible()).toEqual(true); + await expect(shortcutsIcon).toBeVisible(); await shortcutsIcon.click(); await page.waitForTimeout(1000); diff --git a/tests/kit/utils/cloud.ts b/tests/kit/utils/cloud.ts index 714757ccd7..3059a2712a 100644 --- a/tests/kit/utils/cloud.ts +++ b/tests/kit/utils/cloud.ts @@ -185,27 +185,8 @@ export async function enableCloudWorkspaceFromShareButton(page: Page) { const shareMenuButton = page.getByTestId('local-share-menu-button'); await expect(shareMenuButton).toBeVisible(); - // FIXME: this is a workaround for the flaky test - // For unknown reasons, - // the online ci test on GitHub is unable to detect the local-share-menu, - // although it works fine in local testing. - // To ensure the tests pass consistently, I’ve made the following temporary adjustments. - // { - const maxAttempts = 5; - let attempt = 0; - let menuVisible = false; - - while (!menuVisible && attempt < maxAttempts) { - try { - await shareMenuButton.click(); - menuVisible = await page.getByTestId('local-share-menu').isVisible(); - } catch (e) { - console.error(`Attempt ${attempt + 1} failed: ${e}`); - attempt += 1; - } - } - expect(menuVisible).toBeTruthy(); - // } + await shareMenuButton.click(); + await expect(page.getByTestId('local-share-menu')).toBeVisible(); await page.getByTestId('share-menu-enable-affine-cloud-button').click(); await page.getByTestId('confirm-enable-affine-cloud-button').click();