Welcome to the SmartUI SDK sample for Cypress. This repository demonstrates how to integrate SmartUI visual regression testing with Cypress.
smartui-cypress-sdk-sample/
├── cypress/
│ └── e2e/
│ └── smartuiSDKLocal.cy.js # Test file
├── cypress.config.js # Cypress configuration
├── package.json # Dependencies
└── smartui-web.json # SmartUI config (create with npx smartui config:create)
- Node.js installed
- Cypress >= 10.0.0 (SmartUI SDK only supports Cypress versions >= 10.0.0)
- Chrome browser (for Local tests)
For Local:
export PROJECT_TOKEN='your_project_token'git clone https://github.com/LambdaTest/smartui-cypress-sdk-sample
cd smartui-cypress-sdk-sampleInstall required NPM modules for LambdaTest Smart UI Cypress SDK:
npm i @lambdatest/smartui-cli @lambdatest/cypress-driver cypress@^13Dependencies included:
@lambdatest/smartui-cli- SmartUI CLI@lambdatest/cypress-driver- SmartUI Cypress drivercypress@^13- Cypress framework
Add the following import to your cypress/support/e2e.js file:
import '@lambdatest/cypress-driver'npx smartui config:create smartui-web.jsonThe SmartUI screenshot function is already implemented in the repository.
Test File (cypress/e2e/smartuiSDKLocal.cy.js):
describe('Test Case name', () => {
beforeEach(() => {
cy.visit('Required URL')
})
it('SmartUI Snapshot', () => {
cy.smartuiSnapshot('Screenshot Name');
})
})Note: The code is already configured and ready to use. You can modify the URL and screenshot name if needed.
npx smartui exec -- npx cypress run --spec cypress/e2e/smartuiSDKLocal.cy.js --browser chrome --headedNote: The --config flag is optional if your config file is named smartui-web.json and located in the current directory. If you need to specify a different config file, use: npx smartui --config <path> exec -- npx cypress run --spec cypress/e2e/smartuiSDKLocal.cy.js --browser chrome --headed
- Runs Cypress locally using Chrome
- Requires Chrome browser installed
- Takes screenshot with name:
Screenshot Name
The Cypress configuration file is pre-configured for SmartUI integration.
Create the SmartUI configuration file using:
npx smartui config:create smartui-web.jsonThe default configuration includes:
- Browsers: chrome, firefox, safari, edge
- Viewports: 1920, 1366, 360 (full page screenshots by default)
- Optional:
waitForPageRenderandwaitForTimeoutfor slow-loading pages
- Use descriptive, unique names for each screenshot
- Include test context (e.g.,
login-form-filled,checkout-step-2) - Avoid special characters
- Use consistent naming conventions
- After critical user interactions
- Before and after form submissions
- At different viewport sizes
- After page state changes
- Use
cy.wait()before screenshots for dynamic content - Take screenshots after
cy.visit()completes - Use
cy.viewport()to test responsive designs - Combine with Cypress commands for better test flow
describe('Homepage Tests', () => {
beforeEach(() => {
cy.visit('https://www.lambdatest.com')
})
it('Homepage Visual Test', () => {
cy.smartuiSnapshot('homepage-initial')
// Interact with page
cy.get('#search').type('Cypress')
cy.wait(1000) // Wait for results
cy.smartuiSnapshot('homepage-after-search')
})
})describe('Responsive Tests', () => {
it('Desktop View', () => {
cy.viewport(1920, 1080)
cy.visit('https://www.lambdatest.com')
cy.smartuiSnapshot('homepage-desktop')
})
it('Tablet View', () => {
cy.viewport(768, 1024)
cy.visit('https://www.lambdatest.com')
cy.smartuiSnapshot('homepage-tablet')
})
it('Mobile View', () => {
cy.viewport(375, 667)
cy.visit('https://www.lambdatest.com')
cy.smartuiSnapshot('homepage-mobile')
})
})describe('Checkout Flow', () => {
it('Complete Checkout Visual Test', () => {
cy.visit('https://example.com/checkout')
cy.smartuiSnapshot('checkout-step-1')
cy.get('#next-step').click()
cy.wait(500)
cy.smartuiSnapshot('checkout-step-2')
cy.get('#complete').click()
cy.wait(1000)
cy.smartuiSnapshot('checkout-complete')
})
})name: Cypress SmartUI Tests
on: [push, pull_request]
jobs:
visual-tests:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Setup Node.js
uses: actions/setup-node@v3
with:
node-version: '18'
- name: Install dependencies
run: npm ci
- name: Run Cypress with SmartUI
env:
PROJECT_TOKEN: ${{ secrets.SMARTUI_PROJECT_TOKEN }}
run: |
npx smartui exec -- npx cypress run --spec cypress/e2e/smartuiSDKLocal.cy.js --browser chrome --headedSolution: Ensure the driver is imported in cypress/support/e2e.js:
import '@lambdatest/cypress-driver'Solution:
- Verify
PROJECT_TOKENis set - Check Cypress version (requires >= 10.0.0)
- Ensure test completes successfully
- Wait a few moments for processing
Solution: Install compatible Cypress version:
npm install cypress@^13Solution: Add waits before screenshots:
cy.visit('https://example.com')
cy.wait(2000) // Wait for page load
cy.smartuiSnapshot('screenshot'){
"web": {
"browsers": ["chrome", "firefox", "edge"],
"viewports": [
[1920, 1080],
[1366, 768],
[375, 667]
],
"waitForPageRender": 30000,
"waitForTimeout": 2000
}
}After running the tests, visit your SmartUI project dashboard to view the captured screenshots and compare them with baseline builds.