Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(button): add e2e UI snapshot #34

Merged
merged 4 commits into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 88 additions & 0 deletions tests/button/xdesign.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import { expect, test } from '@playwright/test'

test.describe('button 组件xdesign规范', () => {
test('button默认--UI截图', async ({ page }) => {
page.on('pageerror', (exception) => expect(exception).toBeNull())
await page.goto('button#basic-usage')
const demo = page.locator('#basic-usage .pc-demo')
await expect(demo).toBeInViewport()
await expect(demo).toHaveScreenshot('basic-usage.png')

// hover时截图
const btns = await demo.locator('.tiny-button').all()
let i=1;
for(const btn of btns){
await btn.hover()
await page.waitForTimeout(100)
await expect(demo).toBeInViewport()
await expect(demo).toHaveScreenshot({threshold:10})
i++
}
Comment on lines +12 to +20
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Specify unique filenames for screenshots in the hover loop

Within the hover loop, screenshots are taken without specifying filenames, and the variable i is incremented but not used. This may cause screenshots to overwrite each other or not be saved properly. Consider using i to create unique filenames for each screenshot.

Apply this diff to specify unique filenames:

 let i = 1;
 for (const btn of btns) {
   await btn.hover();
   await page.waitForTimeout(100);
   await expect(demo).toBeInViewport();
-  await expect(demo).toHaveScreenshot({ threshold: 10 });
+  await expect(demo).toHaveScreenshot(`basic-usage-hover-${i}.png`, { threshold: 10 });
   i++;
 }
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const btns = await demo.locator('.tiny-button').all()
let i=1;
for(const btn of btns){
await btn.hover()
await page.waitForTimeout(100)
await expect(demo).toBeInViewport()
await expect(demo).toHaveScreenshot({threshold:10})
i++
}
const btns = await demo.locator('.tiny-button').all()
let i=1;
for(const btn of btns){
await btn.hover()
await page.waitForTimeout(100)
await expect(demo).toBeInViewport()
await expect(demo).toHaveScreenshot(`basic-usage-hover-${i}.png`, { threshold: 10 })
i++
}

})

test('幽灵--UI截图', async ({ page }) => {
page.on('pageerror', (exception) => expect(exception).toBeNull())
await page.goto('button#ghost')
const demo = page.locator('#ghost .pc-demo')
await expect(demo).toBeInViewport()
await expect(demo).toHaveScreenshot('basic-usage.png')
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct the screenshot filename

The screenshot filename 'basic-usage.png' does not match the test case '幽灵--UI截图'. It seems like a copy-paste error. Please update the filename to 'ghost.png' to correctly reflect the test case.

Apply this diff to correct the filename:

     await expect(demo).toHaveScreenshot('basic-usage.png');
+    await expect(demo).toHaveScreenshot('ghost.png');

Or replace the line:

-    await expect(demo).toHaveScreenshot('basic-usage.png');
+    await expect(demo).toHaveScreenshot('ghost.png');
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
await expect(demo).toHaveScreenshot('basic-usage.png')
await expect(demo).toHaveScreenshot('ghost.png')


// hover时截图
const btns = await demo.locator('.tiny-button').all()
let i=1;
for(const btn of btns){
await btn.hover()
await page.waitForTimeout(100)
await expect(demo).toBeInViewport()
await expect(demo).toHaveScreenshot({threshold:10})
i++
}
Comment on lines +31 to +39
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Specify unique filenames for screenshots in the hover loop

Similar to the previous test case, the screenshots in the hover loop do not have specified filenames, and the variable i is incremented but not used. Consider specifying unique filenames to prevent overwriting.

Apply this diff:

 let i = 1;
 for (const btn of btns) {
   await btn.hover();
   await page.waitForTimeout(100);
   await expect(demo).toBeInViewport();
-  await expect(demo).toHaveScreenshot({ threshold: 10 });
+  await expect(demo).toHaveScreenshot(`ghost-hover-${i}.png`, { threshold: 10 });
   i++;
 }
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const btns = await demo.locator('.tiny-button').all()
let i=1;
for(const btn of btns){
await btn.hover()
await page.waitForTimeout(100)
await expect(demo).toBeInViewport()
await expect(demo).toHaveScreenshot({threshold:10})
i++
}
const btns = await demo.locator('.tiny-button').all()
let i=1;
for(const btn of btns){
await btn.hover()
await page.waitForTimeout(100)
await expect(demo).toBeInViewport()
await expect(demo).toHaveScreenshot(`ghost-hover-${i}.png`, { threshold: 10 })
i++
}

})

test('动态禁用--UI截图', async ({ page }) => {
page.on('pageerror', (exception) => expect(exception).toBeNull())
await page.goto('button#dynamic-disabled')
const demo = page.locator('#dynamic-disabled .pc-demo')
await page.waitForTimeout(100)
await expect(demo).toBeInViewport()
await expect(demo).toHaveScreenshot('dynamic-disabled1.png')

const switchBtn = demo.locator('.tiny-switch')
await switchBtn.click()
await page.waitForTimeout(100)
await expect(demo).toBeInViewport()
await expect(demo).toHaveScreenshot('dynamic-disabled2.png')
})

test('图标--UI截图', async ({ page }) => {
page.on('pageerror', (exception) => expect(exception).toBeNull())
await page.goto('button#icon')
const demo = page.locator('#icon .pc-demo')
await expect(demo).toBeInViewport()
await expect(demo).toHaveScreenshot('icon.png')
})

test('文字--UI截图', async ({ page }) => {
page.on('pageerror', (exception) => expect(exception).toBeNull())
await page.goto('button#text')
const demo = page.locator('#text .pc-demo')
await expect(demo).toBeInViewport()
await expect(demo).toHaveScreenshot('text.png')
})

test('加载--UI截图', async ({ page }) => {
page.on('pageerror', (exception) => expect(exception).toBeNull())
await page.goto('button#loading')
const demo = page.locator('#loading .pc-demo')
await expect(demo).toBeInViewport()
await expect(demo).toHaveScreenshot('loading.png')
})

test('尺寸--UI截图', async ({ page }) => {
page.on('pageerror', (exception) => expect(exception).toBeNull())
await page.goto('button#size')
const demo = page.locator('#size .pc-demo')
await expect(demo).toBeInViewport()
await expect(demo).toHaveScreenshot('size.png')
})
Comment on lines +4 to +87
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Refactor duplicated code to improve maintainability

There is a significant amount of duplicated code across the test cases, such as setting up page error handling, navigating to pages, locating demo elements, and taking screenshots. Consider refactoring this code by creating helper functions or utilizing beforeEach hooks to reduce redundancy and enhance maintainability.

})
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.