Welcome to the SmartUI SDK sample for TestCafe. This repository demonstrates how to integrate SmartUI visual regression testing with TestCafe.
smartui-testcafe-sample/
├── testcafeSDKLocal.js # Test file (works for both Local and Cloud)
├── package.json # Dependencies
└── smartui-web.json # SmartUI config (create with npx smartui config:create)
- Node.js installed
- LambdaTest account credentials (for Cloud tests)
- Chrome browser (for Local tests)
For Cloud:
export LT_USERNAME='your_username'
export LT_ACCESS_KEY='your_access_key'
export PROJECT_TOKEN='your_project_token'For Local:
export PROJECT_TOKEN='your_project_token'git clone https://github.com/LambdaTest/smartui-testcafe-sample
cd smartui-testcafe-sampleThe repository already includes the required dependencies in package.json. Install them:
npm installDependencies included:
@lambdatest/smartui-cli- SmartUI CLI@lambdatest/testcafe-driver- SmartUI TestCafe drivertestcafe- TestCafe framework
For Cloud execution, also install:
npm install testcafe-browser-provider-lambdatestnpx smartui config:create smartui-web.jsonThe SmartUI screenshot function is already implemented in the repository.
Test File (testcafeSDKLocal.js):
import { smartuiSnapshot } from '@lambdatest/testcafe-driver';
fixture('LambdaTest Test')
.page('https://www.lambdatest.com');
test('Take Homepage Screenshot', async (t) => {
await smartuiSnapshot(t, 'screenshot');
});Note: The code is already configured and ready to use. You can modify the URL and screenshot name if needed. The smartuiSnapshot function takes the test controller t as the first parameter and the screenshot name as the second parameter.
npx smartui exec -- npx testcafe chrome testcafeSDKLocal.jsnpx smartui exec -- npx testcafe "lambdatest:Chrome@latest:Windows 10" testcafeSDKLocal.jsNote: Replace "lambdatest:Chrome@latest:Windows 10" with your desired browser and platform.
The test file (testcafeSDKLocal.js) works for both local and cloud execution.
Create the SmartUI configuration file using:
npx smartui config:create smartui-web.json- Use descriptive, unique names for each screenshot
- Include test context and state
- 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
await t.wait()before screenshots for dynamic content - Take screenshots after page loads completely
- Use
t.resizeWindow()for responsive testing - Combine with TestCafe assertions for better test flow
import { smartuiSnapshot } from '@lambdatest/testcafe-driver';
fixture('LambdaTest Test')
.page('https://www.lambdatest.com');
test('Take Screenshot After Search', async (t) => {
await t.typeText('#search', 'TestCafe');
await t.wait(1000);
await smartuiSnapshot(t, 'search-results');
});fixture('Responsive Tests')
.page('https://www.lambdatest.com');
test('Desktop View', async (t) => {
await t.resizeWindow(1920, 1080);
await smartuiSnapshot(t, 'homepage-desktop');
});
test('Tablet View', async (t) => {
await t.resizeWindow(768, 1024);
await smartuiSnapshot(t, 'homepage-tablet');
});
test('Mobile View', async (t) => {
await t.resizeWindow(375, 667);
await smartuiSnapshot(t, 'homepage-mobile');
});test('Checkout Flow', async (t) => {
await t.navigateTo('https://example.com/checkout');
await smartuiSnapshot(t, 'checkout-step-1');
await t.click('#next-step');
await t.wait(500);
await smartuiSnapshot(t, 'checkout-step-2');
});name: TestCafe 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 TestCafe with SmartUI (Local)
env:
PROJECT_TOKEN: ${{ secrets.SMARTUI_PROJECT_TOKEN }}
run: |
npx smartui exec -- npx testcafe chrome testcafeSDKLocal.js
- name: Run TestCafe with SmartUI (Cloud)
env:
PROJECT_TOKEN: ${{ secrets.SMARTUI_PROJECT_TOKEN }}
LT_USERNAME: ${{ secrets.LT_USERNAME }}
LT_ACCESS_KEY: ${{ secrets.LT_ACCESS_KEY }}
run: |
npx smartui exec -- npx testcafe "lambdatest:Chrome@latest:Windows 10" testcafeSDKLocal.jsSolution: Ensure the driver is imported:
import { smartuiSnapshot } from '@lambdatest/testcafe-driver';Solution:
- Verify
PROJECT_TOKENis set - Add waits before screenshots
- Ensure test completes successfully
- Check TestCafe version compatibility
Solution: Set the environment variable:
export PROJECT_TOKEN='your_project_token'Solution:
- Install browser provider:
npm install testcafe-browser-provider-lambdatest - Verify
LT_USERNAMEandLT_ACCESS_KEYare set - Check browser/platform format:
"lambdatest:Chrome@latest:Windows 10"
{
"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.