Skip to content

Add a Playwright e2e test #460

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

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

Conversation

msabramo
Copy link
Contributor

@msabramo msabramo commented May 28, 2025

Write a Playwright test that tests the app end-to-end (e2e).

Motivation and Context

Ensure things don't get broken

How Has This Been Tested?

abramowi at Marcs-MacBook-Pro-3 in ~/Code/OpenSource/inspector (playwright-test●)
$ npm run test:e2e

> @modelcontextprotocol/[email protected] test:e2e
> npm run test:e2e --workspace=client


> @modelcontextprotocol/[email protected] test:e2e
> playwright test e2e


Running 8 tests using 5 workers
  8 passed (7.9s)

To open last HTML report run:

  npx playwright show-report

Screenshot 2025-06-17 at 9 16 36 PM

Breaking Changes

No

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation update

Checklist

  • I have read the MCP Documentation
  • My code follows the repository's style guidelines
  • New and existing tests pass locally
  • I have added appropriate error handling
  • I have added or updated documentation as needed

Additional context

@msabramo
Copy link
Contributor Author

Related PR (which has perhaps fallen into some disrepair): #354

@olaservo
Copy link
Member

Thanks for setting this up. My draft PR I'd opened for feedback has been getting pretty moldy. A couple questions about this e2e setup:

  • Are you thinking these should just be run locally during development? Originally I assumed that we should have these run as part of CI so we're not relying on that. I think it would also be OK to work that in as separate PRs.
  • I'm making a similar assumption with adding Playwright configs - my original PR had some settings for browser, timeouts, etc. which we can add as we need them.
  • How do you think we should approach adding tests for different transports? In my original tests I was duplicating a lot of code.

Thanks again for working on this, it should help bridge some gaps with testing basic stuff efficiently.

Copy link
Contributor

@cliffhall cliffhall left a comment

Choose a reason for hiding this comment

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

The test ran successfully, but only after

  1. I downloaded the browsers (additional step required beyond just npm install)
  2. Launching the inspector (you can't just run test:e2e, you have to run start then test:e2e)

Just thinking about how to handle this in CI. We'd need to use caching to install playwright and browsers only if they aren't cached. And start the server, then wait for it to be up before running the tests.

name: Playwright Tests

on:
  push:
    branches: [ main ] 
  pull_request:
    branches: [ main ]

jobs:
  test:
    timeout-minutes: 5
    runs-on: ubuntu-latest 

    steps:
      - uses: actions/checkout@v4

      - uses: actions/setup-node@v4
        with:
          node-version: 18 

      # Cache Playwright browsers
      - name: Cache Playwright browsers
        id: cache-playwright
        uses: actions/cache@v4 
        with:
          path: ~/.cache/ms-playwright # The default Playwright cache path
          key: ${{ runner.os }}-playwright-${{ hashFiles('package-lock.json') }} # Cache key based on OS and package-lock.json
          restore-keys: |
            ${{ runner.os }}-playwright-

      - name: Install dependencies
        run: npm ci 

      - name: Install Playwright and browsers unless cached
        run: npx playwright install --with-deps
        if: steps.cache-playwright.outputs.cache-hit != 'true' 

      - name: Start Server
        run: npm run start 

      - name: Wait for server to start
        run: npx wait-on http://localhost:6274

      - name: Run Playwright tests
        run: npm run test:e2e 

@msabramo
Copy link
Contributor Author

msabramo commented Jun 4, 2025

@olaservo: I had been thinking that this would mainly be run in CI but I like to do things in baby steps so that I don't get overwhelmed and so reviewing is easier. My plan was to do the CI in a separate PR.

Though now I'm wondering if we should just go for it since @cliffhall points out that it's not trivial to set up the tests locally and a lot of people will simply not bother, so maybe it's not of much use if it's not also run in CI.

I probably should look at your PR and see what good stuff I can borrow re: config, timeouts, etc.

@cliffhall cliffhall added the waiting on submitter Waiting for the submitter to provide more info label Jun 4, 2025
@msabramo
Copy link
Contributor Author

msabramo commented Jun 5, 2025

Added .github/workflows/e2e_tests.yml (from @cliffhall) in 8a8212c. I guess a maintainer needs to approve the workflow to run so we can see how it works?

@cliffhall
Copy link
Contributor

cliffhall commented Jun 5, 2025

Added .github/workflows/e2e_tests.yml (from @cliffhall) in 8a8212c. I guess a maintainer needs to approve the workflow to run so we can see how it works?

@msabramo I tried but it failed on format check. Need to run prettier-fix to pass CI.

@msabramo
Copy link
Contributor Author

msabramo commented Jun 7, 2025

@msabramo I tried but it failed on format check. Need to run prettier-fix to pass CI.

Done in df4c372.

@msabramo
Copy link
Contributor Author

msabramo commented Jun 7, 2025

I added support for https://github.com/daun/playwright-report-summary in cdb9180.

As a result, at https://github.com/modelcontextprotocol/inspector/actions/runs/15509171521?pr=460 you can see a nice Playwright test report:

Screenshot 2025-06-07 at 8 29 44 AM

@msabramo
Copy link
Contributor Author

msabramo commented Jun 7, 2025

@olaservo @cliffhall

@cliffhall
Copy link
Contributor

cliffhall commented Jun 17, 2025

@msabramo Failures when I run the tests locally with /usr/local/bin/npm run test:e2e. What am I missing?

Screenshot 2025-06-17 at 3 20 15 PM

@cliffhall
Copy link
Contributor

@msabramo Also, I noticed that running these tests created a lot of files locally in the project. Can these not be created in the system tmp folder?

I realize the CLI tests do such a thing as well, and I'd like to make that go away too, but that's for another PR.

@msabramo
Copy link
Contributor Author

@msabramo Failures when I run the tests locally with /usr/local/bin/npm run test:e2e. What am I missing?

Well, it could probably be a few things, but one possibility is that perhaps the dev server wasn't running.

Regardless of whether that's the problem you're hitting, it seems like an easy problem to hit. I discovered that Playwright can start the server for us with a webServer config in playwright.config.ts which I added in 8b1faf1. And then I removed a similar thing in the e2e_tests.yml GH Actions workflow, because Playwright should take care of it inside and outside of CI, which seems better.

Give it another try with the new commits and see if it works any better.

If it still doesn't work, you might try opening a browser to http://localhost:6274 while the Playwright tests are running and opening a JavaScript console and looking for errors. I actually ran into another problem that confused me for a while where the server was starting but the web page was blank and it was because of a SyntaxError in the JS Console, which was resolved by running npm install to get some new package(s) that are necessary.

@msabramo
Copy link
Contributor Author

msabramo commented Jun 18, 2025

@msabramo Also, I noticed that running these tests created a lot of files locally in the project. Can these not be created in the system tmp folder?

I realize the CLI tests do such a thing as well, and I'd like to make that go away too, but that's for another PR.

You mean these:

$ git status
On branch playwright-test
Your branch is up to date with 'msabramo/playwright-test'.

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        client/playwright-report/
        client/results.json
        client/test-results/

The first two are reports that are explicitly specified in playwright.config.ts, so they seem potentially useful and I'm not sure what client/test-results is for, but probably something Playwright uses, so I'd be nervous about moving them.
How about if I add them to .gitignore? Does that help with your concern?

0b31ce7 - .gitignore: Add playwright artifacts

b783b50 - Make "npm run clean" remove Playwright files

Copy link
Contributor

@cliffhall cliffhall left a comment

Choose a reason for hiding this comment

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

@msabramo Failures when I run the tests locally with /usr/local/bin/npm run test:e2e. What am I missing?

Well, it could probably be a few things, but one possibility is that perhaps the dev server wasn't running.

Regardless of whether that's the problem you're hitting, it seems like an easy problem to hit. I discovered that Playwright can start the server for us with a webServer config in playwright.config.ts which I added in 8b1faf1. And then I removed a similar thing in the e2e_tests.yml GH Actions workflow, because Playwright should take care of it inside and outside of CI, which seems better.

Thanks so much for getting it to auto start the server. There were no instructions to start the server, and for a local test run, it shouldn't really have to be a requirement anyhow.

@msabramo Also, I noticed that running these tests created a lot of files locally in the project. Can these not be created in the system tmp folder?
I realize the CLI tests do such a thing as well, and I'd like to make that go away too, but that's for another PR.

You mean these:

$ git status
On branch playwright-test
Your branch is up to date with 'msabramo/playwright-test'.

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        client/playwright-report/
        client/results.json
        client/test-results/

The first two are reports that are explicitly specified in playwright.config.ts, so they seem potentially useful and I'm not sure what client/test-results is for, but probably something Playwright uses, so I'd be nervous about moving them. How about if I add them to .gitignore? Does that help with your concern?

Please forgive the bluntness, but in an already crowded project, these folders and files are just litter to me. IMO, adding them to .gitignore and the clean script are not an acceptable solution. The former means they remain in my project, visual clutter that I do not need to see. The latter means I need to run another command, which also removes all the node modules and does another npm install, time I do not need to waste.

When running tests, I only care about whether they failed or passed, period. The following is all I need to see:

Screenshot 2025-06-18 at 3 36 28 PM

If you could simply make the test:e2e script delete these files after the run, it would be perfect.

@msabramo
Copy link
Contributor Author

@cliffhall: Thanks for the detailed comments! I think it should be better now. With the latest commits, I don't even generate playwright-report or results.json if we're not running in CI (those are needed for the GitHub Action workflow) and test-results is moved to client/e2e/test-results and it gets removed by the test:e2e script at the end if we're not running in CI.

So I think things should be clean but let me know if I missed anything.

@msabramo msabramo requested a review from cliffhall June 19, 2025 14:29
Copy link
Contributor

@cliffhall cliffhall left a comment

Choose a reason for hiding this comment

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

Thanks @msabramo for cleaning up the unnecessary remnants. One more new side effect just showed up (not your fault in the least). Working fix suggested below.

Since merging the latest changes from `main` that put auto-open back in when running the start or dev scripts, the `test:e2e` run results in the Inspector being opened in the browser. This change suppresses that (tested locally). Thanks, @cliffhall!

Co-authored-by: Cliff Hall <[email protected]>
cliffhall
cliffhall previously approved these changes Jun 19, 2025
Copy link
Contributor

@cliffhall cliffhall left a comment

Choose a reason for hiding this comment

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

LGTM! 👍

@cliffhall cliffhall dismissed their stale review June 19, 2025 19:34

Need to discuss the github action

Copy link
Contributor

@cliffhall cliffhall left a comment

Choose a reason for hiding this comment

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

Let's streamline this action to just run the tests and make a comment on the PR with the results.

client/playwright-report/
client/test-results/
client/results.json
retention-days: 30
Copy link
Contributor

Choose a reason for hiding this comment

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

If this is done on every push to main, we don't need to retain these files for 30 days.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Reduced from 30 to 2 in eb89e01.

Comment on lines +60 to +73
- name: Publish Playwright Test Summary
uses: daun/playwright-report-summary@v3
if: always()
with:
report-file: client/results.json
comment-title: "🎭 Playwright E2E Test Results"
job-summary: true
icon-style: "emojis"
custom-info: |
**Test Environment:** Ubuntu Latest, Node.js 18
**Browsers:** Chromium, Firefox

📊 [View Detailed HTML Report](https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}) (download artifacts)
test-command: "npm run test:e2e"
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't know what the intention of all this is, it seems like its running the tests twice. We've already run it above in Run Playwright tests and here we're running it to generate the report again...?

I think what we really want is the simple report with comment setup. There's no need to upload the reports and save/publish them. Just the results in a PR comment will do.

image

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't know what the intention of all this is, it seems like its running the tests twice. We've already run it above in Run Playwright tests and here we're running it to generate the report again...?

I could be wrong, but my assumption is that it's not running the tests again. I think maybe:

test-command: "npm run test:e2e"

might be creating that impression? And it might be misleading because the docs for this action say:

    # Command used to run tests. If provided, a command to re-run failed or
    # flaky tests will be printed for each section.
    # Default: ''
    test-command: 'npm run test --'

so it sounds like it be more informative and not actually a way to run the tests, as it kind of seems it could be?
But maybe this needs a comment to clarify, since that is confusing?

Copy link
Contributor Author

@msabramo msabramo Jun 20, 2025

Choose a reason for hiding this comment

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

I think what we really want is the simple report with comment setup. There's no need to upload the reports and save/publish them. Just the results in a PR comment will do.

image

So I think this is set up to do that but it's failing to post the comment because of permissions problems:

Screenshot 2025-06-20 at 8 30 57 AM

Commenting test report on PR
  Creating new comment
  Error creating comment: Resource not accessible by integration
  Submitting PR review comment instead...
  Error creating PR review: Resource not accessible by integration
Warning: Unable to comment on your PR — 
this can happen for PR's originating from a fork without write permissions. 
You can copy the test results directly into a comment using the markdown summary below:

Maybe you can have a look and see if you are able to grant permissions so that this can work and you can see if you like the way it does comments?

@olaservo olaservo mentioned this pull request Jun 20, 2025
9 tasks
Copy link
Contributor

@cliffhall cliffhall left a comment

Choose a reason for hiding this comment

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

Try the below suggestion.

@msabramo
Copy link
Contributor Author

Still got the following after applying d5b4a6e:

Screenshot 2025-06-20 at 11 31 30 AM

@msabramo
Copy link
Contributor Author

Screenshot 2025-06-20 at 11 36 18 AM

@msabramo
Copy link
Contributor Author

msabramo commented Jun 20, 2025

Here's the markdown summary from the GH Actions log, manually pasted here so we can see how it looks:


🎭 Playwright E2E Test Results

12 passed

Details

12 tests across 1 suite
22.3 seconds
d5b4a6e
ℹ️ Test Environment: Ubuntu Latest, Node.js 18
Browsers: Chromium, Firefox

📊 View Detailed HTML Report (download artifacts)


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.

3 participants