Welcome to the SmartUI SDK sample for WebdriverIO. This repository demonstrates how to integrate SmartUI visual regression testing with WebdriverIO.
smartui-sdk-wdio-sample/
├── test/
│ └── specs/
│ ├── cloud.e2e.js # Cloud test file
│ └── local.e2e.js # Local test file
├── wdio.conf.js # WebdriverIO configuration
├── 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-sdk-wdio-sample
cd smartui-sdk-wdio-sampleThe repository already includes the required dependencies in package.json. Install them:
npm installDependencies included:
@lambdatest/smartui-cli- SmartUI CLI@lambdatest/wdio-driver- SmartUI WebdriverIO driver@wdio/cli- WebdriverIO CLIwebdriverio- WebdriverIO frameworkwdio-lambdatest-service- LambdaTest service for WebdriverIO
npx smartui config:create smartui-web.jsonThe SmartUI screenshot function is already implemented in the repository.
Cloud Test (test/specs/cloud.e2e.js):
const { smartuiSnapshot } = require('@lambdatest/wdio-driver');
await browser.url(`https://webdriver.io`)
await smartuiSnapshot(browser, "screenshot");Local Test (test/specs/local.e2e.js):
const { smartuiSnapshot } = require('@lambdatest/wdio-driver');
await browser.url('https://webdriver.io');
await smartuiSnapshot(browser, "screenshot");Note: The code is already configured and ready to use. You can modify the URL and screenshot name if needed.
npx smartui exec -- node test/specs/local.e2e.jsnpx smartui exec -- wdio run ./wdio.conf.js- Connects to LambdaTest Cloud using WebdriverIO
- Reads credentials from environment variables (
LT_USERNAME,LT_ACCESS_KEY) - Uses Mocha framework
- Takes screenshot with name:
screenshot
- Runs WebdriverIO locally using Chrome
- Requires Chrome browser installed
- Takes screenshot with name:
screenshot
The configuration file is pre-configured for LambdaTest Cloud:
- Hostname:
hub.lambdatest.com - Reads credentials from environment variables
- Excludes
local.e2e.jsfrom cloud runs - Uses Mocha framework
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
browser.waitUntil()before screenshots - Take screenshots after page loads completely
- Use
browser.setWindowSize()for responsive testing - Combine with WebdriverIO assertions
const { smartuiSnapshot } = require('@lambdatest/wdio-driver');
describe('Homepage Tests', () => {
it('Should take screenshot after search', async () => {
await browser.url('https://www.lambdatest.com');
await browser.waitUntil(async () => {
return (await browser.$('#search')).isDisplayed();
});
await browser.$('#search').setValue('WebdriverIO');
await browser.$('#search-button').click();
await browser.waitUntil(async () => {
return (await browser.$('.results')).isDisplayed();
});
await smartuiSnapshot(browser, "search-results");
});
});describe('Responsive Tests', () => {
it('Desktop View', async () => {
await browser.setWindowSize(1920, 1080);
await browser.url('https://www.lambdatest.com');
await smartuiSnapshot(browser, 'homepage-desktop');
});
it('Tablet View', async () => {
await browser.setWindowSize(768, 1024);
await browser.url('https://www.lambdatest.com');
await smartuiSnapshot(browser, 'homepage-tablet');
});
it('Mobile View', async () => {
await browser.setWindowSize(375, 667);
await browser.url('https://www.lambdatest.com');
await smartuiSnapshot(browser, 'homepage-mobile');
});
});describe('Checkout Flow', () => {
it('Complete Checkout Visual Test', async () => {
await browser.url('https://example.com/checkout');
await smartuiSnapshot(browser, 'checkout-step-1');
await browser.$('#next-step').click();
await browser.waitUntil(async () => {
return (await browser.$('#step-2')).isDisplayed();
});
await smartuiSnapshot(browser, 'checkout-step-2');
});
});name: WebdriverIO 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 WebdriverIO with SmartUI (Local)
env:
PROJECT_TOKEN: ${{ secrets.SMARTUI_PROJECT_TOKEN }}
run: |
npx smartui exec -- node test/specs/local.e2e.js
- name: Run WebdriverIO 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 -- wdio run ./wdio.conf.jsSolution: Ensure the driver is imported:
const { smartuiSnapshot } = require('@lambdatest/wdio-driver');Solution:
- Verify
PROJECT_TOKENis set - Add waits before screenshots
- Ensure test completes successfully
- Check WebdriverIO configuration
Solution: Set the environment variable:
export PROJECT_TOKEN='your_project_token'Solution:
- Verify
wdio.conf.jsis configured correctly - Check
LT_USERNAMEandLT_ACCESS_KEYare set - Verify LambdaTest service is configured in
wdio.conf.js
Ensure LambdaTest service is configured:
services: [
['lambdatest', {
tunnel: false
}]
],
capabilities: [{
browserName: 'chrome',
'LT:Options': {
username: process.env.LT_USERNAME,
accessKey: process.env.LT_ACCESS_KEY,
platformName: 'Windows 10',
build: 'WebdriverIO SmartUI Build'
}
}]{
"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.