Programming & Development / April 18, 2025

How to Test UI and API from the Command Line Like a Pro

UI testing CLI API testing CLI Cypress headless test Newman command line Cypress vs Postman automation testing terminal command line testing JavaScript end-to-end test script bash

Testing both your UI and API from the command line is a powerful way to integrate automated tests into CI/CD pipelines and boost development efficiency. This article walks through tools and scripts to perform UI testing using Cypress, and API testing using Newman, the CLI runner for Postman collections.

πŸ§ͺ UI Testing from the Command Line with Cypress

Cypress is a popular JavaScript-based end-to-end testing framework that runs in the browser. It can be run interactively or headlessly through the terminal.

βœ… Step 1: Install Cypress

Make sure you have Node.js installed. Then run:

bash

npm install cypress --save-dev

βœ… Step 2: Initialize Cypress

To create the Cypress structure and default configuration:

bash

npx cypress open

This creates a cypress/ directory with sample tests and config files.

βœ… Step 3: Write a Test

Create a file like cypress/e2e/example_spec.cy.js:

javascript

describe('My First Test', () => {
  it('Visits the Kitchen Sink', () => {
    cy.visit('https://example.cypress.io');
    cy.contains('type').click();
    cy.url().should('include', '/commands/actions');
    cy.get('.action-email')
      .type('fake@email.com')
      .should('have.value', 'fake@email.com');
  });
});

βœ… Step 4: Run Cypress Headlessly

Run tests directly in the terminal:

bash

npx cypress run

This will launch the test in headless mode using the Electron browser by default. You can also specify a browser:

bash

npx cypress run --browser chrome

πŸ”— API Testing from the Command Line with Newman

Newman is a CLI companion for Postman that lets you run Postman collections as automated tests.

βœ… Step 1: Install Newman

Install globally via npm:

bash

npm install -g newman

βœ… Step 2: Export Postman Collection

From Postman, export your collection (JSON file), and optionally, an environment file.

βœ… Step 3: Run API Tests

bash

newman run path/to/collection.json

With environment variables:

bash

newman run path/to/collection.json -e path/to/environment.json

You’ll get a complete summary with test results and response data.

πŸ”„ Combine UI and API Tests in One Script

Want to run everything in one go? Use a shell script like this:

bash

#!/bin/bash

echo "Running Cypress UI tests..."
npx cypress run
if [ $? -ne 0 ]; then
  echo "Cypress tests failed."
  exit 1
fi

echo "Running Newman API tests..."
newman run path/to/collection.json -e path/to/environment.json
if [ $? -ne 0 ]; then
  echo "Newman tests failed."
  exit 1
fi

echo "βœ… All tests passed successfully!"

Make the script executable:

bash

chmod +x run_tests.sh

Run it:

bash

./run_tests.sh

🧩 Bonus: Integrate into CI/CD

Both Cypress and Newman are CI/CD-friendly. You can plug them into GitHub Actions, GitLab CI, Jenkins, or any CI platform with minimal setup, automating your entire test suite after every push or pull request.

βœ… Conclusion

By combining Cypress and Newman, you can thoroughly test both the frontend UI and backend APIs β€” all from your terminal. Whether it’s for local dev, staging, or part of your CI/CD workflow, this command-line approach ensures your app stays rock solid.


Comments

No comments yet

Add a new Comment

NUHMAN.COM

Information Technology website for Programming & Development, Web Design & UX/UI, Startups & Innovation, Gadgets & Consumer Tech, Cloud Computing & Enterprise Tech, Cybersecurity, Artificial Intelligence (AI) & Machine Learning (ML), Gaming Technology, Mobile Development, Tech News & Trends, Open Source & Linux, Data Science & Analytics

Categories

Tags

©{" "} Nuhmans.com . All Rights Reserved. Designed by{" "} HTML Codex