Today's class contents
- What is TestCafe
- What programming language is needed.
- Pre-requisite of TestCafe
- Download and install node
- Download VS Code IDE
- Initiate a TestCafe project
- Write basics testcases with testcafe
- Tips
TestCafe: Automation testing framework for web application.
Free and open source
Supports windows, linux and mac OS
Easy Setup
Multiple browser supports
Runs on Node.js
Test can be created with JavaScript or TypeScript, CoffeeScript
- Test Recording.
- Parallel run on multiple browsers.
- Local and remote run.
- Automatic page load wait time.
- Page object model support.
- CI/CD integrations.
- TestCafe Studio
-
Install Node and NPM :
https://nodejs.org/en/downloadHow to check node and npm version:
node --version npm --version -
Install IDE : vscode
https://code.visualstudio.com/download
-
Install TestCafe
a) Open VS Code
b) Open your project folder
c) Open terminal and run:
npm init -y npm install testcafe --save-dev- At first, "package.json" file will be created (node project)
- package.json gets updated
- node_modules and package-lock.json are created
-
(Optional) Install HTML Reporter for TestCafe:
npm install --save-dev testcafe-reporter-html
a) Create a new folder named tests and create a file test1.js
b) Create a fixture and test section
TestCafe test files consist of fixtures and tests
A fixture is a group of tests that share the same starting URL.
If TestCafe is installed locally (as a dev dependency), use npx to run it:
npx testcafe chrome .\tests\test1.js
Or for headless mode:
npx testcafe chrome:headless .\tests\test1.js
If you want to use a reporter (like HTML):
npx testcafe chrome:headless tests/ --reporter html:tests/reports/report.html
npx testcafe chrome tests/ --reporter html:tests/reports/report.html
-
'testcafe' is not recognized as the name of a cmdlet...
- This means TestCafe is not installed globally. Use
npx testcafe ...instead of justtestcafe ...when running from the terminal. - Or install globally:
npm install -g testcafe(not recommended for CI/CD).
- This means TestCafe is not installed globally. Use
-
Reporter not found error:
- Install the reporter with:
npm install --save-dev testcafe-reporter-html
- Install the reporter with:
-
Google blocks automation:
- Example workflow file:
.github/workflows/github-actions.yml - Runs tests on push/pull request to main/master
- Installs dependencies and runs TestCafe with HTML reporter
- Uploads test reports as artifacts
- Check Node.js and npm version:
node --version npm --version - Install dependencies:
npm ci - Run all tests:
npx testcafe chrome tests/ - Run tests with HTML report:
npx testcafe chrome:headless tests/ --reporter html:tests/reports/report.html
TestCafe provides several ways to run specific tests instead of the entire test suite:
npx testcafe chrome tests/test3.jsUse the -t or --test flag followed by the test name:
npx testcafe chrome tests/test3.js -t "Contact page navigation and assertion"npx testcafe chrome tests/test3.js -t "2nd test" -t "Dropdown test"Use wildcards to match test names:
npx testcafe chrome tests/ -t "*dropdown*"npx testcafe chrome tests/test3.js -t "Contact page navigation and assertion" --reporter html:tests/reports/report.htmlBased on the tests in test3.js:
# Run only the contact page test
npx testcafe chrome tests/test3.js -t "Contact page navigation and assertion"
# Run only the dropdown test
npx testcafe chrome tests/test3.js -t "Dropdown test"
# Run only the 2nd test
npx testcafe chrome tests/test3.js -t "2nd test"Summary of what was done:
- Installed Node.js, npm, and VS Code
- Initialized a Node.js project and installed TestCafe
- Created test files and wrote basic tests
- Learned to run tests with npx
- Installed and used HTML reporter
- Handled common errors (like command not found)
- Used demo sites for testing
- Set up GitHub Actions for CI/CD
How it works:
The report is copied to public/index.html. The public folder is deployed to the gh-pages branch.
The summary includes a link to the live report: https://.github.io//
Note:
The report will be updated on every push to main or master.
The link will only work after the first successful deployment to gh-pages.
You may need to enable GitHub Pages in your repo settings (set source to gh-pages branch, / (root) folder).