npm init -y
npm install cypress --save-dev
npx cypress open
npx cypress open
describe('My First Test', () => {
it('Visits the Cypress example page', () => {
cy.visit('https://example.cypress.io');
cy.contains('type').click();
cy.url().should('include', '/commands/actions');
});
});
describe('Login Form', () => {
it('should display an error message for invalid credentials', () => {
cy.visit('https://example.com/login');
cy.get('input[name="username"]').type('invalid_username');
cy.get('input[name="password"]').type('invalid_password');
cy.get('button[type="submit"]').click();
cy.contains('Invalid username or password').should('be.visible');
});
});
npx cypress open
describe('Login Page', () => {
describe('Successful Login', () => {
// Test cases for successful login
});
describe('Failed Login', () => {
// Test cases for failed login
});
});
describe('Login Page', () => {
it('should display an error message for invalid credentials', () => {
// Test case code
});
});
describe('Login Page', () => {
beforeEach(() => {
// Common setup steps before each test case
});
it('should display an error message for invalid credentials', () => {
// Test case code
});
afterEach(() => {
// Common teardown steps after each test case
});
});
describe('Login Page', () => {
it('@smoke should display login form', () => {
// Test case code
});
it('@regression should display an error message for invalid credentials', () => {
// Test case code
});
});
cypress/
├── integration/
│ ├── login/
│ │ ├── login.spec.js
│ │ └── forgot-password.spec.js
│ ├── checkout/
│ │ ├── cart.spec.js
│ │ └── payment.spec.js
│ └── ...
cy.get('.login-button').should('be.visible');
cy.get('.error-message').should('contain', 'Invalid credentials');
cy.get('.login-button')
.should('be.visible')
.and('have.text', 'Log In')
.and('have.class', 'btn-primary');
cy.get('.success-message').should('not.exist');
cy.get('.error-message').should('not.contain', 'Error');
Cypress.config('defaultCommandTimeout', 5000); // Set timeout to 5 seconds
expect(user).to.have.property('name').that.is.a('string');
cy.intercept('POST', '/login').as('loginRequest');
cy.get('.login-button').click();
cy.wait('@loginRequest').its('response.statusCode').should('eq', 200);
cy.wrap(() => {
throw new Error('Test error');
}).should('throw', 'Test error');
// Using CSS selector
cy.get('.login-button').click();
// Using attribute selector
cy.get('[data-testid="username-input"]').type('username');
// Using DOM properties
cy.get('input[name="password"]').type('password');
cy.get('.login-button').click();
cy.get('input[name="username"]').type('username');
cy.get('input[name="password"]').type('password');
cy.get('input[name="username"]').clear().type('new-username');
cy.get('select[name="country"]').select('USA');
cy.get('input[type="checkbox"]').check();
cy.get('input[type="radio"]').first().check();
cy.get('.dropdown-menu').trigger('mouseover');
cy.contains('Sign Up').click();
cy.get('button[type="submit"]').click();
cy.get('#footer').scrollIntoView();
cy.visit('https://example.com');
cy.contains('Home').click();
cy.get('.success-message').should('not.exist');
cy.get('.error-message').should('not.contain', 'Error');
cy.get('form').submit();
cy.go('back');
cy.go('forward');
cy.reload();
cy.url().should('include', '/login');
cy.on('window:confirm', () => true); // Accepts a confirmation pop-up
cy.intercept('GET', '/api/user').as('getUser');
cy.visit('/');
cy.wait('@getUser');
cy.iframe('.my-iframe').find('input').type('Text inside iframe');
cy.frameLoaded('.my-iframe').then(cy.iframe).find('input').type('Text inside iframe');
cy.iframe('.my-iframe').find('input').type('Text inside iframe');
cy.iframe('.my-iframe').find('h1').should('have.text', 'Welcome to the iframe');
cy.frameLoaded('.outer-iframe').then(() => {
cy.iframe('.outer-iframe').frameLoaded('.inner-iframe').then(cy.iframe).find('input').type('Text inside inner iframe');
});
cy.visit('https://parent-frame-url.com');
cy.iframe('.first-iframe').find('input').type('Text inside first iframe');
cy.iframe('.second-iframe').find('input').type('Text inside second iframe');
cy.intercept('GET', '/api/users').as('getUsers');
cy.wait('@getUsers');
cy.intercept('POST', '/api/login', { statusCode: 200, body: { token: 'mockToken' } }).as('login');
cy.intercept('POST', '/api/login', (req) => {
req.body.username = 'testUser';
}).as('login');
cy.intercept('POST', '/api/login', (req) => {
expect(req.method).to.equal('POST');
expect(req.body.username).to.equal('testUser');
}).as('login');
cy.intercept('POST', '/api/login').as('login');
cy.wait('@login');
cy.intercept('GET', '/api/users', { statusCode: 500, body: 'Server Error', passthrough: true });
cy.get('.login-button').as('loginButton');
cy.get('@loginButton').click();
cy.intercept('POST', '/api/login').as('loginRequest');
cy.wait('@loginRequest').its('response.statusCode').should('eq', 200);
Cypress.Commands.add('login', (username, password) => {
cy.visit('/login');
cy.get('input[name="username"]').type(username);
cy.get('input[name="password"]').type(password);
cy.get('.login-button').click();
});
cy.login('testUser', 'password123');
cy.intercept('POST', '/api/login').as('loginRequest');
cy.get('.login-button').click().wait('@loginRequest').its('response.statusCode').should('eq', 200);
Cypress.Commands.add('login', (username, password) => {
cy.visit('/login');
cy.get('input[name="username"]').type(username);
cy.get('input[name="password"]').type(password);
cy.get('.login-button').click();
});
Cypress.Commands.add('login', (username, password) => {
cy.visit('/login');
cy.get('input[name="username"]').type(username);
cy.get('input[name="password"]').type(password);
cy.get('.login-button').click();
cy.url().should('include', '/dashboard');
});
{
"users": [
{ "username": "user1", "password": "password1" },
{ "username": "user2", "password": "password2" }
]
}
cy.fixture('users').then((users) => {
cy.get('input[name="username"]').type(users[0].username);
cy.get('input[name="password"]').type(users[0].password);
});
cy.fixture('users').then((users) => {
cy.get('input[name="username"]').type(users[0].username);
cy.get('input[name="password"]').type(users[0].password);
});
cy.fixture('users').each((user) => {
it(`Logs in as ${user.username}`, () => {
cy.visit('/login');
cy.get('input[name="username"]').type(user.username);
cy.get('input[name="password"]').type(user.password);
cy.get('.login-button').click();
// Add assertions or other actions here
});
});
const environment = Cypress.env('environment');
cy.fixture(`config/${environment}`).then((config) => {
// Use config data in tests
});
cypress/
├── integration/
│ ├── authentication/
│ │ ├── login.spec.js
│ │ └── signup.spec.js
│ ├── checkout/
│ │ ├── cart.spec.js
│ │ └── payment.spec.js
│ └── ...
authentication/
├── login.spec.js
├── signup.spec.js
describe('Login Page', () => {
it('should display the login form', () => {
// Test case code
});
it('should display an error message for invalid credentials', () => {
// Test case code
});
});
CYPRESS_BASE_URL=http://localhost:3000
CYPRESS_API_URL=http://localhost:3000/api
{
"baseUrl": "http://localhost:3000",
"env": {
"API_URL": "http://localhost:3000/api"
}
}
npx cypress run --config baseUrl=http://staging.example.com
module.exports = (on, config) => {
const envConfig = require(`./config.${config.env.NODE_ENV}.json`);
return { ...config, ...envConfig };
};
const baseUrl = Cypress.env('baseUrl');
const apiUrl = Cypress.env('API_URL');
npm install --save-dev mocha
Cypress.on('test:after:run', (test, runnable) => {
if (test.state === 'failed') {
const screenshotFileName = `${runnable.parent.title} -- ${test.title} (failed).png`;
cy.screenshot(screenshotFileName);
}
});
npx cypress run --spec "cypress/integration/**/*.*" --reporter mocha-multi-reporters
npm install --save-dev jest
module.exports = {
testMatch: ['**/*.spec.js'],
};
npx cypress run --spec "cypress/integration/**/*.*" --env testFiles="**/*.spec.js" --reporter cypress-jest-reporter
npm install --save-dev cypress-plugin-something
const somethingPlugin = require('cypress-plugin-something');
module.exports = (on, config) => {
somethingPlugin(on, config);
// Register other plugins here
};
Cypress.Commands.add('customCommand', (arg1, arg2) => {
// Custom command logic here
});
module.exports = (on, config) => {
on('before:browser:launch', (browser, launchOptions) => {
// Modify launchOptions here
});
};
module.exports = (on, config) => {
on('task', {
interceptNetworkRequest(url) {
// Intercept network request logic here
return null; // Return null to allow the request to continue as usual
}
});
};
const customReporter = require('custom-reporter');
module.exports = (on, config) => {
on('after:run', (results) => {
customReporter.generateReport(results);
});
};
name: CI
on:
push:
branches:
- main
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout Repository
uses: actions/checkout@v2
- name: Set up Node.js
uses: actions/setup-node@v2
with:
node-version: '14'
- name: Install Dependencies
run: npm install
- name: Run Cypress Tests
run: npm run cy:run -- --headless
- name: Archive Artifacts
uses: actions/upload-artifact@v2
with:
name: cypress-results
path: cypress/screenshots
path: cypress/videos
Cypress.Commands.add('login', (username, password) => {
cy.visit('/login');
cy.get('input[name="username"]').type(username);
cy.get('input[name="password"]').type(password);
cy.get('.login-button').click();
});
cy.login('testuser', 'password123');
// utils.js
export const generateRandomUsername = () => {
// Generate a random username
};
// test.spec.js
import { generateRandomUsername } from './utils';
describe('User Registration', () => {
const username = generateRandomUsername();
// Test code using the generated username
});
Cypress.Commands.add('runBrowserStackTest', () => {
return cy.task('runBrowserStackTest', {
browser: 'chrome',
url: 'https://example.com',
});
});