GitHub Actions lets you run tasks automatically when you change your code. Integrating Cypress means you can test your website every time you update it, without doing it by hand.
GitHub Actions integration in Cypress
name: Cypress Tests
on: [push, pull_request]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Install Node.js
uses: actions/setup-node@v3
with:
node-version: '18'
- run: npm install
- run: npx cypress runThis is a basic GitHub Actions workflow file in YAML format.
It runs Cypress tests on every push or pull request automatically.
on: push
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- run: npm install
- run: npx cypress runon:
pull_request:
branches: [main]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- run: npm ci
- run: npx cypress runjobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Cache node modules
uses: actions/cache@v3
with:
path: ~/.npm
key: ${{ runner.os }}-node-${{ hashFiles('package-lock.json') }}
- run: npm ci
- run: npx cypress runThis workflow runs Cypress tests on every push or pull request. It checks out the code, sets up Node.js version 18, installs dependencies cleanly, and runs Cypress tests.
name: Cypress Tests
on: [push, pull_request]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Setup Node.js
uses: actions/setup-node@v3
with:
node-version: '18'
- run: npm ci
- run: npx cypress runMake sure your project has a valid package.json with Cypress listed as a dependency.
You can customize the Node.js version in the setup step to match your project needs.
Check GitHub Actions logs to see detailed test results and errors if tests fail.
GitHub Actions can run Cypress tests automatically on code changes.
This helps catch bugs early and saves manual testing time.
Workflows are defined in YAML files inside your project's .github/workflows folder.
Practice
Solution
Step 1: Understand GitHub Actions role
GitHub Actions automates tasks like running tests when code changes.Step 2: Connect Cypress testing purpose
Cypress tests run automatically to find bugs early without manual effort.Final Answer:
To automatically run tests on code changes and catch bugs early -> Option BQuick Check:
Automation of tests = A [OK]
- Thinking GitHub Actions only deploys code
- Believing tests run manually only
- Confusing test writing with test running
Solution
Step 1: Identify trigger key in GitHub Actions YAML
Theonkey defines events like push or pull_request that start the workflow.Step 2: Differentiate from other keys
runis used inside steps,stepslists commands,jobsgroups tasks.Final Answer:
on -> Option DQuick Check:
Trigger event key = on [OK]
- Confusing 'run' with trigger event
- Mixing 'steps' with event triggers
- Using 'jobs' as trigger key
on:
push:
branches:
- main
jobs:
cypress-run:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Run Cypress tests
run: npx cypress run
Solution
Step 1: Analyze trigger event
The workflow triggers on push to themainbranch only.Step 2: Understand job steps
It checks out code and runs Cypress tests withnpx cypress run.Final Answer:
Cypress tests run automatically on push to main branch -> Option AQuick Check:
Push to main triggers tests = C [OK]
- Assuming manual trigger needed
- Thinking tests run on all branches
- Believing workflow lacks trigger
on:
push:
branches:
- main
jobs:
test:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Run tests
run: cypress run
What is the most likely error?Solution
Step 1: Check Cypress run command syntax
The command should benpx cypress runto run Cypress installed locally.Step 2: Verify other parts
Branch name 'main' is correct, 'runs-on' is valid, and Cypress action is optional if using CLI.Final Answer:
Missing 'npx' before 'cypress run' command -> Option AQuick Check:
Run command needs 'npx' prefix = B [OK]
- Running 'cypress run' without 'npx'
- Changing branch name incorrectly
- Expecting Cypress action is mandatory
- Misconfiguring runner OS
Solution
Step 1: Identify required events and branches
Tests must run on both push and pull_request events targeting the 'develop' branch only.Step 2: Match YAML configuration
on: push: branches: - develop pull_request: branches: - develop correctly lists both events with 'develop' branch under each.Final Answer:
on: push: branches: - develop pull_request: branches: - develop -> Option CQuick Check:
Both events on 'develop' branch = D [OK]
- Mixing branch names between events
- Listing only one event
- Using wrong branch name
Ready to prove it?
Pass the test and this pattern moves unknown
