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

Conversation

shenjunjian
Copy link
Collaborator

@shenjunjian shenjunjian commented Sep 19, 2024

PR

PR Checklist

Please check if your PR fulfills the following requirements:

  • The commit message follows our Commit Message Guidelines
  • Tests for the changes have been added (for bug fixes / features)
  • Docs have been added / updated (for bug fixes / features)

PR Type

What kind of change does this PR introduce?

  • Bugfix
  • Feature
  • Code style update (formatting, local variables)
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • CI related changes
  • Documentation content changes
  • Other... Please describe:

What is the current behavior?

Issue Number: N/A

What is the new behavior?

Does this PR introduce a breaking change?

  • Yes
  • No

Other information

Summary by CodeRabbit

  • New Features

    • Introduced a comprehensive test suite for validating button UI components against design specifications.
    • Added automated visual validation for various button states, including default, ghost, disabled, icon, text, loading, and size.
  • Bug Fixes

    • Enhanced reliability of button component's UI by ensuring consistent visual representation across different interactions.

Copy link

coderabbitai bot commented Sep 19, 2024

Walkthrough

A new test suite has been added in tests/button/xdesign.spec.ts to validate the UI components of a button design using Playwright. The suite includes multiple test cases for different button variants, ensuring visual accuracy through screenshot comparisons and simulating user interactions.

Changes

File Change Summary
tests/button/xdesign.spec.ts Added a test suite for button UI validation with multiple test cases for different button states.

Possibly related PRs

Suggested reviewers

  • zzcr

🐇 In a world of buttons bright,
We test them day and night.
With Playwright by our side,
Visuals clear, we take great pride.
Each click and hover, we ensure,
Designs stay true, that's for sure! 🌟


Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 8ba68cc and a8fb9fb.

Files selected for processing (1)
  • tests/button/xdesign.spec.ts (1 hunks)
Files skipped from review as they are similar to previous changes (1)
  • tests/button/xdesign.spec.ts

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    -- I pushed a fix in commit <commit_id>, please review it.
    -- Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    -- @coderabbitai generate unit testing code for this file.
    -- @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    -- @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    -- @coderabbitai read src/utils.ts and generate unit testing code.
    -- @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    -- @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 5

Outside diff range and nitpick comments (3)
tests/button/xdesign.spec.ts (3)

12-20: Remove unused variable 'i' if not needed

Alternatively, if unique filenames are not required, you can remove the variable i since it is not used elsewhere.

Apply this diff to remove i:

-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++;
 }

31-39: Remove unused variable 'i' if not needed

If unique filenames are not required, you can remove the variable i.

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 });
-  i++;
 }

46-46: Evaluate the necessity of the timeout

At line 46, await page.waitForTimeout(100); introduces a delay before checking if the demo is in the viewport. Unless there is a specific reason for this delay (e.g., waiting for animations or asynchronous content), consider removing it to optimize test execution time.

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between eff32a3 and 8ba68cc.

Files ignored due to path filters (30)
  • tests/button/xdesign.spec.ts-snapshots/basic-usage-chromium-win32.png is excluded by !**/*.png
  • tests/button/xdesign.spec.ts-snapshots/dynamic-disabled1-chromium-win32.png is excluded by !**/*.png
  • tests/button/xdesign.spec.ts-snapshots/dynamic-disabled2-chromium-win32.png is excluded by !**/*.png
  • tests/button/xdesign.spec.ts-snapshots/icon-chromium-win32.png is excluded by !**/*.png
  • tests/button/xdesign.spec.ts-snapshots/input组件xdesign规范-button默认--UI截图-1-chromium-win32.png is excluded by !**/*.png
  • tests/button/xdesign.spec.ts-snapshots/input组件xdesign规范-button默认--UI截图-10-chromium-win32.png is excluded by !**/*.png
  • tests/button/xdesign.spec.ts-snapshots/input组件xdesign规范-button默认--UI截图-11-chromium-win32.png is excluded by !**/*.png
  • tests/button/xdesign.spec.ts-snapshots/input组件xdesign规范-button默认--UI截图-12-chromium-win32.png is excluded by !**/*.png
  • tests/button/xdesign.spec.ts-snapshots/input组件xdesign规范-button默认--UI截图-13-chromium-win32.png is excluded by !**/*.png
  • tests/button/xdesign.spec.ts-snapshots/input组件xdesign规范-button默认--UI截图-14-chromium-win32.png is excluded by !**/*.png
  • tests/button/xdesign.spec.ts-snapshots/input组件xdesign规范-button默认--UI截图-15-chromium-win32.png is excluded by !**/*.png
  • tests/button/xdesign.spec.ts-snapshots/input组件xdesign规范-button默认--UI截图-16-chromium-win32.png is excluded by !**/*.png
  • tests/button/xdesign.spec.ts-snapshots/input组件xdesign规范-button默认--UI截图-17-chromium-win32.png is excluded by !**/*.png
  • tests/button/xdesign.spec.ts-snapshots/input组件xdesign规范-button默认--UI截图-18-chromium-win32.png is excluded by !**/*.png
  • tests/button/xdesign.spec.ts-snapshots/input组件xdesign规范-button默认--UI截图-2-chromium-win32.png is excluded by !**/*.png
  • tests/button/xdesign.spec.ts-snapshots/input组件xdesign规范-button默认--UI截图-3-chromium-win32.png is excluded by !**/*.png
  • tests/button/xdesign.spec.ts-snapshots/input组件xdesign规范-button默认--UI截图-4-chromium-win32.png is excluded by !**/*.png
  • tests/button/xdesign.spec.ts-snapshots/input组件xdesign规范-button默认--UI截图-5-chromium-win32.png is excluded by !**/*.png
  • tests/button/xdesign.spec.ts-snapshots/input组件xdesign规范-button默认--UI截图-6-chromium-win32.png is excluded by !**/*.png
  • tests/button/xdesign.spec.ts-snapshots/input组件xdesign规范-button默认--UI截图-7-chromium-win32.png is excluded by !**/*.png
  • tests/button/xdesign.spec.ts-snapshots/input组件xdesign规范-button默认--UI截图-8-chromium-win32.png is excluded by !**/*.png
  • tests/button/xdesign.spec.ts-snapshots/input组件xdesign规范-button默认--UI截图-9-chromium-win32.png is excluded by !**/*.png
  • tests/button/xdesign.spec.ts-snapshots/input组件xdesign规范-幽灵--UI截图-1-chromium-win32.png is excluded by !**/*.png
  • tests/button/xdesign.spec.ts-snapshots/input组件xdesign规范-幽灵--UI截图-2-chromium-win32.png is excluded by !**/*.png
  • tests/button/xdesign.spec.ts-snapshots/input组件xdesign规范-幽灵--UI截图-3-chromium-win32.png is excluded by !**/*.png
  • tests/button/xdesign.spec.ts-snapshots/input组件xdesign规范-幽灵--UI截图-4-chromium-win32.png is excluded by !**/*.png
  • tests/button/xdesign.spec.ts-snapshots/input组件xdesign规范-幽灵--UI截图-5-chromium-win32.png is excluded by !**/*.png
  • tests/button/xdesign.spec.ts-snapshots/input组件xdesign规范-幽灵--UI截图-6-chromium-win32.png is excluded by !**/*.png
  • tests/button/xdesign.spec.ts-snapshots/size-chromium-win32.png is excluded by !**/*.png
  • tests/button/xdesign.spec.ts-snapshots/text-chromium-win32.png is excluded by !**/*.png
Files selected for processing (1)
  • tests/button/xdesign.spec.ts (1 hunks)

Comment on lines +4 to +87
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++
}
})

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')

// 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++
}
})

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')
})
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.

@@ -0,0 +1,88 @@
import { expect, test } from '@playwright/test'

test.describe('input组件xdesign规范', () => {
Copy link

Choose a reason for hiding this comment

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

Update test suite description to 'button' component

The test suite is currently described as 'input组件xdesign规范', but the tests are for the button component. Please update the description to accurately reflect the component being tested.

Apply this diff to correct the description:

-test.describe('input组件xdesign规范', () => {
+test.describe('button组件xdesign规范', () => {
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
test.describe('input组件xdesign规范', () => {
test.describe('button组件xdesign规范', () => {

Comment on lines +31 to +39
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++
}
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++
}

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')

Comment on lines +12 to +20
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++
}
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++
}

@zzcr zzcr added the e2e playwright label Sep 19, 2024
@@ -0,0 +1,88 @@
import { expect, test } from '@playwright/test'

test.describe('input组件xdesign规范', () => {
Copy link
Member

Choose a reason for hiding this comment

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

这里改一下,是button组件不是input组件

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
e2e playwright
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants