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: playwright boilerplate #65

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
Open

Conversation

fmorency
Copy link
Contributor

@fmorency fmorency commented Nov 21, 2024

This PR adds PR support for e2e testing.

  • Wallet authentication using GitHub & Web3Auth
  • Landing page title and button check
  • Bank page - send 1MFX to the USER 1 account.

Create a .env.playwright and set the GITHUB_USER_EMAIL and GITHUB_USER_PASSWORD variables to the appropriate testing account. Make sure the account has MFX funds.

This gives us the basics to add more e2e tests.

@fmorency fmorency self-assigned this Nov 21, 2024
Copy link
Contributor

coderabbitai bot commented Nov 21, 2024

Walkthrough

The changes in this pull request encompass modifications to several files, primarily focusing on enhancing end-to-end testing capabilities using Playwright. Key updates include the addition of a new .gitignore entries to exclude specific directories and files, the introduction of a Playwright configuration file, and the creation of new test suites and utility functions for user authentication and feature testing. Additionally, updates to package.json reflect new scripts and dependencies to support the testing framework.

Changes

File Change Summary
.gitignore Added entries: node_modules/, test-results/, playwright-report/, playwright/.auth, blob-report/, playwright/.cache/
e2e/auth.setup.ts Added async function authenticate({ page }) for GitHub authentication setup.
e2e/bank.spec.ts Added test suite for "Bank" with a test case "send mfx" for MFX transaction automation.
e2e/landing.spec.ts Added test suite with two test cases: "has title" and "learn how it works."
e2e/utils/auth.ts Added async function signInWithGitHub({ page, github }) to facilitate GitHub sign-in.
package.json Added script test:playwright, updated test:coverage, and added dependencies: @playwright/test, @types/node, dotenv. Updated eslint.
playwright.config.ts Introduced Playwright configuration with settings for test directory, parallel execution, retries, and project setups for major browsers.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant AuthSetup
    participant GitHub
    participant BankFeature

    User->>AuthSetup: Start Authentication
    AuthSetup->>GitHub: Sign in with GitHub
    GitHub-->>AuthSetup: Authentication Response
    AuthSetup->>User: Authentication Successful
    User->>BankFeature: Initiate MFX Transaction
    BankFeature-->>User: Transaction Confirmation
Loading

🐰 "In the land of code where bunnies play,
New tests and scripts brighten the day.
With GitHub sign-ins and banks to explore,
Our repository's clean, and we're ready for more!
Hop along, dear friends, let’s test with delight,
For every new change makes our project just right!" 🐇✨


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

codecov bot commented Dec 5, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 60.37%. Comparing base (4f8b533) to head (0a3c1e5).

Additional details and impacted files
@@           Coverage Diff           @@
##             main      #65   +/-   ##
=======================================
  Coverage   60.37%   60.37%           
=======================================
  Files         148      148           
  Lines       14757    14757           
=======================================
  Hits         8909     8909           
  Misses       5848     5848           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@fmorency fmorency marked this pull request as ready for review December 5, 2024 16:11
Copy link
Contributor

@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: 9

🧹 Outside diff range and nitpick comments (11)
e2e/landing.spec.ts (2)

3-6: Consider using exact title match instead of regex

The regex pattern /Alberto - Blockchain App/ might be too permissive and could pass with unexpected titles. Consider using an exact match for more reliable testing.

-  await expect(page).toHaveTitle(/Alberto - Blockchain App/);
+  await expect(page).toHaveTitle('Alberto - Blockchain App');

1-12: Improve test organization and reusability

Consider restructuring the tests using test.describe for better organization and beforeEach for common setup.

import { test, expect } from '@playwright/test';

test.describe('Landing Page', () => {
  let page;

  test.beforeEach(async ({ page: p }) => {
    page = p;
    await page.goto('/');
  });

  test('has correct title', async () => {
    await expect(page).toHaveTitle('Alberto - Blockchain App');
  });

  test('learn how it works button', async () => {
    const button = page.getByTestId('learn-more-button');
    await expect(button).toBeVisible();
    await button.click();
    // Add assertions for expected behavior after click
  });
});
.gitignore (1)

39-39: Remove duplicate node_modules entry

The node_modules/ entry is already covered by /node_modules at the top of the file.

-node_modules/
e2e/bank.spec.ts (1)

2-2: Remove unused import

The signInWithGitHub utility is imported but never used in the test.

-import { signInWithGitHub } from '@/e2e/utils/auth';
playwright.config.ts (2)

14-33: Consider adding timeout settings and verify baseURL.

While the configuration is good, consider:

  1. Adding global timeout settings for CI environments where tests might run slower
  2. Making baseURL configurable via environment variables for different environments
  use: {
-   baseURL: 'http://127.0.0.1:3000',
+   baseURL: process.env.PLAYWRIGHT_BASE_URL || 'http://127.0.0.1:3000',
    trace: 'on-first-retry',
+   // Add timeout settings
+   navigationTimeout: 30000,
+   actionTimeout: 15000,
  },

35-78: Consider enabling Firefox for cross-browser testing.

Currently, only Chromium is enabled. While this is a good start, consider enabling Firefox as well for better cross-browser coverage. Firefox often helps catch CSS and JavaScript compatibility issues early.

  projects: [
    { name: 'setup', testMatch: /.*\.setup\.ts/ },
    {
      name: 'chromium',
      use: { ...devices['Desktop Chrome'], storageState: 'playwright/.auth/user.json' },
      dependencies: ['setup'],
    },
-   // {
-   //   name: 'firefox',
-   //   use: { ...devices['Desktop Firefox'] },
-   // },
+   {
+     name: 'firefox',
+     use: { ...devices['Desktop Firefox'], storageState: 'playwright/.auth/user.json' },
+     dependencies: ['setup'],
+   },
package.json (1)

14-15: Enhance test scripts with useful options.

The current test scripts could benefit from additional options for better usability:

-    "test:playwright": "bunx playwright test",
+    "test:playwright": "bunx playwright test",
+    "test:playwright:ui": "bunx playwright test --ui",
+    "test:playwright:debug": "bunx playwright test --debug",
+    "test:playwright:report": "bunx playwright show-report",
e2e/auth.setup.ts (3)

7-10: Validate GitHub credentials before proceeding

The GitHub email and password are retrieved from environment variables without checking if they are provided. If the environment variables are missing, the credentials default to empty strings, which could cause authentication to fail without a clear error message. Consider adding validation to ensure that the credentials are present.

You can add a validation check:

const github = {
  email: process.env.GITHUB_USER_EMAIL ?? '',
  password: process.env.GITHUB_USER_PASSWORD ?? '',
};

+ if (!github.email || !github.password) {
+   throw new Error('GitHub credentials are not provided in environment variables.');
+ }

24-30: Use resilient selectors for reauthorization handling

The selector 'button:has-text("Authorize iam-login")' is tied to specific button text, which may change if the application name changes or GitHub updates their UI. This could cause the test to fail unexpectedly.

Consider using a more robust selector:

- await page1.waitForSelector('button:has-text("Authorize iam-login")', {
+ await page1.waitForSelector('form[action^="/login/oauth/authorize"] button[type="submit"]', {
    state: 'visible',
    timeout: 10000,
});

36-41: Use resilient selectors for 2FA setup handling

Similar to the previous comment, the selector 'text=Skip for now' may fail if the button text changes. Using a more reliable selector can make the test less brittle.

Consider updating the selector:

- await page1.waitForSelector('text=Skip for now', {
+ await page1.waitForSelector('button[data-testid="2fa-skip-button"]', {
    state: 'visible',
    timeout: 10000,
});
e2e/utils/auth.ts (1)

28-30: Log errors during sign-in for better troubleshooting

When an error occurs during the sign-in process, it is silently caught, making it difficult to debug issues.

Modify the catch block to log the error:

} catch (error) {
+ console.error('Error during GitHub sign-in:', error);
  return false;
}
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 4f8b533 and 0a3c1e5.

⛔ Files ignored due to path filters (1)
  • bun.lockb is excluded by !**/bun.lockb
📒 Files selected for processing (7)
  • .gitignore (1 hunks)
  • e2e/auth.setup.ts (1 hunks)
  • e2e/bank.spec.ts (1 hunks)
  • e2e/landing.spec.ts (1 hunks)
  • e2e/utils/auth.ts (1 hunks)
  • package.json (3 hunks)
  • playwright.config.ts (1 hunks)
🔇 Additional comments (1)
playwright.config.ts (1)

1-9: LGTM! Good environment setup.

The configuration properly separates test environment variables using a dedicated .env.playwright file.

Comment on lines +10 to +11
const button = page.locator('text=Learn how it works');
await expect(button).toBeVisible();
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Use data-testid for more reliable element selection

Using text content for element selection can be fragile as it may change with UI updates or internationalization. Consider using data-testid attributes.

-  const button = page.locator('text=Learn how it works');
+  const button = page.getByTestId('learn-more-button');

Also, consider adding a test for the button's click interaction and its expected behavior.

Committable suggestion skipped: line range outside the PR's diff.

Comment on lines +13 to +14
.getByPlaceholder('Enter address')
.fill('manifest1hj5fveer5cjtn4wd6wstzugjfdxzl0xp8ws9ct');
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Move test data to a separate constants file

Hardcoded values like addresses should be moved to a separate test constants file for better maintainability.

Create a new file e2e/constants.ts:

export const TEST_CONSTANTS = {
  RECIPIENT_ADDRESS: 'manifest1hj5fveer5cjtn4wd6wstzugjfdxzl0xp8ws9ct',
  TEST_AMOUNT: '1',
  TEST_MEMO: 'E2E bank test'
};

Then update the test to use these constants.

Comment on lines +5 to +20
test('send mfx', async ({ page }) => {
await page.goto('/bank');
await expect(page.getByText('mfx', { exact: true })).toBeVisible();
await page.getByLabel('send-mfx').click();
await page.getByPlaceholder('0.00').click();
await page.getByPlaceholder('0.00').fill('1');
await page.getByPlaceholder('Enter address').click();
await page
.getByPlaceholder('Enter address')
.fill('manifest1hj5fveer5cjtn4wd6wstzugjfdxzl0xp8ws9ct');
await page.getByPlaceholder('Memo').click();
await page.getByPlaceholder('Memo').fill('E2E bank test');
await page.getByLabel('send-btn').click();
await page.getByRole('button', { name: 'Approve' }).click();
await page.waitForSelector('text=Transaction Successful');
});
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Improve test robustness and organization

The test needs several improvements:

  1. Add error handling for transaction steps
  2. Validate transaction details before approval
  3. Break down the long test into smaller, focused test cases
  4. Add proper assertions for the transaction state

Here's a suggested refactor:

import { test, expect } from '@playwright/test';
import { TEST_CONSTANTS } from './constants';

test.describe('Bank Transactions', () => {
  test.beforeEach(async ({ page }) => {
    await page.goto('/bank');
  });

  test('should display MFX balance', async ({ page }) => {
    await expect(page.getByTestId('mfx-balance')).toBeVisible();
  });

  test('should validate transaction form', async ({ page }) => {
    await page.getByLabel('send-mfx').click();
    
    // Test form validation
    await page.getByLabel('send-btn').click();
    await expect(page.getByText('Amount is required')).toBeVisible();
    
    // Fill invalid amount
    await page.getByPlaceholder('0.00').fill('-1');
    await expect(page.getByText('Amount must be positive')).toBeVisible();
  });

  test('should complete MFX transaction', async ({ page }) => {
    await page.getByLabel('send-mfx').click();
    
    // Fill transaction details
    await page.getByPlaceholder('0.00').fill(TEST_CONSTANTS.TEST_AMOUNT);
    await page.getByPlaceholder('Enter address').fill(TEST_CONSTANTS.RECIPIENT_ADDRESS);
    await page.getByPlaceholder('Memo').fill(TEST_CONSTANTS.TEST_MEMO);
    
    // Verify transaction details in confirmation dialog
    await page.getByLabel('send-btn').click();
    await expect(page.getByText(`Amount: ${TEST_CONSTANTS.TEST_AMOUNT} MFX`)).toBeVisible();
    await expect(page.getByText(`To: ${TEST_CONSTANTS.RECIPIENT_ADDRESS}`)).toBeVisible();
    
    // Complete transaction
    await page.getByRole('button', { name: 'Approve' }).click();
    
    // Verify success and balance update
    await expect(page.getByText('Transaction Successful')).toBeVisible();
    // Add assertion for updated balance
  });
});

await page.getByPlaceholder('Memo').fill('E2E bank test');
await page.getByLabel('send-btn').click();
await page.getByRole('button', { name: 'Approve' }).click();
await page.waitForSelector('text=Transaction Successful');
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Add timeout and error handling for transaction confirmation

The waitForSelector call should include a reasonable timeout and error handling for failed transactions.

-    await page.waitForSelector('text=Transaction Successful');
+    try {
+      await page.waitForSelector('text=Transaction Successful', { timeout: 30000 });
+    } catch (error) {
+      throw new Error('Transaction did not complete within 30 seconds');
+    }
📝 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 page.waitForSelector('text=Transaction Successful');
try {
await page.waitForSelector('text=Transaction Successful', { timeout: 30000 });
} catch (error) {
throw new Error('Transaction did not complete within 30 seconds');
}

Comment on lines +80 to +85
/* Run your local dev server before starting the tests */
webServer: {
command: 'bun run start',
url: 'http://127.0.0.1:3000',
reuseExistingServer: !process.env.CI,
},
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Add health check URL for reliable server startup.

The webServer configuration should include a health check URL to ensure the server is fully ready before tests begin.

  webServer: {
    command: 'bun run start',
    url: 'http://127.0.0.1:3000',
+   reuseExistingServer: !process.env.CI,
+   stdout: 'pipe',
+   stderr: 'pipe',
+   timeout: 120000,
  },

Committable suggestion skipped: line range outside the PR's diff.

@@ -84,13 +86,15 @@
"@types/bad-words": "^3.0.3",
"@types/crypto-js": "^4.2.2",
"@types/identicon.js": "^2.3.4",
"@types/node": "^22.9.1",
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue

Verify @types/node version compatibility.

The @types/node version (^22.9.1) seems unusually high and might be incorrect, as Node.js itself is currently at version 20.x. This could lead to type definition incompatibilities.

-    "@types/node": "^22.9.1",
+    "@types/node": "^20.11.0",
📝 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
"@types/node": "^22.9.1",
"@types/node": "^20.11.0",

timeout: 20000,
});

await page.context().storageState({ path: authFile });
Copy link
Contributor

Choose a reason for hiding this comment

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

💡 Codebase verification

Add playwright/.auth to .gitignore to prevent committing sensitive authentication data

Add the following line to .gitignore:

playwright/.auth

This will prevent accidentally committing authentication state files that may contain sensitive information like session tokens.

🔗 Analysis chain

Ensure authentication state file is excluded from version control

The authentication state is saved to ../playwright/.auth/user.json, which may contain sensitive information like session tokens. To prevent accidental commits of sensitive data, ensure this path is listed in .gitignore.

Run the following script to verify that playwright/.auth is included in .gitignore:

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check if 'playwright/.auth' is listed in .gitignore

if ! grep -q '^playwright/.auth' .gitignore; then
  echo "'playwright/.auth' is not in .gitignore. Please add it to prevent committing sensitive data."
else
  echo "'playwright/.auth' is correctly ignored."
fi

Length of output: 241

setup('authenticate', async ({ page }) => {
setup.setTimeout(120000);

await signInWithGitHub({ page, github });
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue

Handle sign-in failure from signInWithGitHub

The function signInWithGitHub returns a boolean indicating success or failure, but the return value is not checked. If the sign-in fails, the script will proceed, potentially causing unexpected errors later. Consider handling the result of the sign-in attempt.

Modify the code to check the return value:

- await signInWithGitHub({ page, github });
+ const signInSuccess = await signInWithGitHub({ page, github });
+ if (!signInSuccess) {
+   throw new Error('GitHub sign-in failed.');
+ }
📝 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 signInWithGitHub({ page, github });
const signInSuccess = await signInWithGitHub({ page, github });
if (!signInSuccess) {
throw new Error('GitHub sign-in failed.');
}

Comment on lines +25 to +26
await page.waitForSelector('text=Create repository');
expect(page.isVisible('text=Create repository')).toBe(true);
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Use a more reliable check for successful login

Waiting for 'text=Create repository' assumes the user's landing page contains this text, which may not always be true (e.g., for new accounts or if GitHub changes their UI). This could lead to false negatives in login verification.

Consider verifying the existence of the user's avatar or a consistent element in the navbar:

- await page.waitForSelector('text=Create repository');
- expect(page.isVisible('text=Create repository')).toBe(true);
+ await page.waitForSelector('summary[aria-label="View profile and more"]', {
+   state: 'visible',
+ });
+ expect(await page.isVisible('summary[aria-label="View profile and more"]')).toBe(true);
📝 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 page.waitForSelector('text=Create repository');
expect(page.isVisible('text=Create repository')).toBe(true);
await page.waitForSelector('summary[aria-label="View profile and more"]', {
state: 'visible',
});
expect(await page.isVisible('summary[aria-label="View profile and more"]')).toBe(true);

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

Successfully merging this pull request may close these issues.

1 participant