keepcalculator

06 Numbers and Powers

Add a GUI to Cypress End-to-End API Tests

Card No. 06·54624 · Updated · Standard formula, shown below

This guide walks you through adding a graphical user interface (GUI) to your Cypress end-to-end API tests. Instead of running tests only from the command line, you’ll build a simple web page that lets you trigger tests, view results, and see pass/fail status at a glance. This is useful for QA teams, demo environments, or anyone who wants a more visual way to run and monitor API test suites. You’ll need basic familiarity with Cypress, Node.js, and HTML/JavaScript.

What You’ll Need

  • A project with Cypress installed and at least one API test file (e.g., cypress/e2e/api.cy.js).
  • Node.js (v16 or later) and npm.
  • A code editor.
  • Basic understanding of Express.js (or similar) for the backend server.

Step-by-Step

  1. Set up a minimal Express server – Create a file called server.js in your project root. Install Express and the Cypress module runner: npm install express cypress. In server.js, set up a basic Express app that will serve your GUI and expose an API endpoint to trigger tests.
  2. Create the GUI HTML page – Make a folder called public and inside it create index.html. This page will have a button to run the tests and a div to display results. Keep it simple: a heading, a button, and a pre-formatted area for output.
  3. Add a route to run Cypress tests – In server.js, add a POST endpoint like /run-tests. This endpoint will execute the Cypress test runner programmatically using cypress.run() from the Cypress module API. Capture the output and return it as JSON.
  4. Wire the GUI button to the backend – In index.html, add a script that sends a POST request to /run-tests when the button is clicked. Display the returned JSON (test results, pass/fail counts) in the output area.
  5. Serve the GUI – In server.js, use express.static('public') to serve the HTML file. Start the server with node server.js and open http://localhost:3000 in your browser.
  6. Enhance the GUI (optional but recommended) – Add a dropdown to select specific test files, a “Run All” button, and a status indicator (e.g., green for pass, red for fail). You can also display the test duration and any error messages.

Common Mistakes

  • Not handling async properly – cypress.run() returns a Promise. Forgetting to await it or handle the promise will result in undefined responses.
  • Hardcoding test paths – Always use relative paths from the project root. If you move the server file, tests may break.
  • Ignoring Cypress exit codes – Cypress may exit with code 1 if tests fail. Your server should still return a 200 response with the result JSON, not crash.
  • Port conflicts – If port 3000 is in use, the server won’t start. Use a different port or handle the error.

Tips

  • Use cypress.run() with the config option to override baseUrl or other settings dynamically from the GUI.
  • Add a loading spinner to the button while tests are running to improve UX.
  • Store the last run results in a file or memory so you can show historical runs.
  • Consider using a framework like React or Vue for the GUI if you need more complex interactions, but plain HTML/JS is fine for a simple dashboard.

Troubleshooting

If the GUI doesn’t work as expected, try these steps in order:

  1. Check the server console – Look for errors from cypress.run(). Common issues include missing dependencies or invalid test paths.
  2. Verify the API endpoint – Use Postman or curl to POST to /run-tests directly. If that fails, the problem is in the server code, not the GUI.
  3. Check the browser console – Open DevTools in the GUI page to see if the fetch request is failing due to CORS or wrong URL.
  4. Ensure Cypress is installed locally – If you run cypress.run() from a global install, it may not find the project’s config. Use the local node_modules/.bin/cypress.
  5. Look at the Cypress output – The cypress.run() result includes a runs array with detailed per-test info. Log it to see what’s happening.

Frequently Asked Questions

Can I use this GUI with Cypress Dashboard?

Yes, but the GUI is separate. You can still record runs to the Dashboard while triggering them from your custom GUI.

How do I run only a specific test file from the GUI?

Pass the spec option to cypress.run(), e.g., cypress.run({ spec: 'cypress/e2e/api.cy.js' }). You can make this dynamic via a dropdown in the GUI.

Is it possible to see live output while tests run?

Yes, but it requires streaming. You can use Server-Sent Events (SSE) or WebSockets to push progress updates from the server to the GUI.

Can I run this on a CI server?

Yes, but the GUI is meant for manual triggering. On CI, you’d typically run Cypress directly via the CLI. The GUI can still be used for on-demand debugging.

Do I need to modify my existing Cypress tests?

No, the GUI works with any existing tests. It just invokes the Cypress runner.

How do I secure the GUI if it’s exposed publicly?

Add authentication (e.g., a simple token) to the /run-tests endpoint, and restrict access to the GUI itself. Avoid exposing it to the open internet without protection.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.

More cards in this drawer

The whole Numbers and Powers drawer →