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.