Welcome to the SmartUI SDK sample for K6. This repository demonstrates how to integrate SmartUI visual regression testing with K6 browser automation.
smartui-k6-sample/
├── k6-smartui.js # Cloud test with single scenario
├── smartui-k6-scenarios.js # Cloud test with multiple scenarios
└── smartui-web.json # SmartUI config (create with npx smartui config:create)
- K6 installed (see K6 Installation Guide)
- LambdaTest account credentials
- Node.js installed (for SmartUI CLI, optional)
macOS:
brew install k6Linux:
sudo gpg -k
sudo gpg --no-default-keyring --keyring /usr/share/keyrings/k6-archive-keyring.gpg --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys C5AD17C747E3415A3642D57D77C6C491D6AC1D69
echo "deb [signed-by=/usr/share/keyrings/k6-archive-keyring.gpg] https://dl.k6.io/deb stable main" | sudo tee /etc/apt/sources.list.d/k6.list
sudo apt-get update
sudo apt-get install k6Windows: Download the installer from K6 Installation Guide
Verify installation:
k6 versionexport LT_USERNAME='your_username'
export LT_ACCESS_KEY='your_access_key'git clone https://github.com/LambdaTest/smartui-k6-sample
cd smartui-k6-sampleFor K6 browser automation with hooks, the SmartUI config is optional. The smartUIProjectName in capabilities is used instead.
npx smartui config:create smartui-web.jsonThe SmartUI screenshot function is already implemented in the repository using hooks-based integration.
Test File (k6-smartui.js):
import { chromium } from 'k6/experimental/browser';
await page.goto("https://www.lambdatest.com");
// Take screenshot using SmartUI hooks
await captureSmartUIScreenshot(page, "screenshot");
async function captureSmartUIScreenshot(page, screenshotName) {
await page.evaluate(_ => {}, `lambdatest_action: ${JSON.stringify({
action: "smartui.takeScreenshot",
arguments: { screenshotName: screenshotName }
})}`);
}Note:
- The code is already configured and ready to use
- You can modify the URL and screenshot name if needed
- Update
smartUIProjectNamein capabilities to match your SmartUI project name - The test uses K6 browser automation with hooks-based SmartUI integration
Execute visual regression tests on SmartUI using the following command:
K6_BROWSER_ENABLED=true k6 run k6-smartui.jsNote:
K6_BROWSER_ENABLED=trueis required to enable browser automation in K6- Navigate to the LambdaTest dashboard to view the running test
- Visit your SmartUI project to see captured screenshots
- Connects to LambdaTest Cloud using K6 browser automation
- Reads credentials from environment variables (
LT_USERNAME,LT_ACCESS_KEY) - Takes screenshot with name:
screenshot - Uses
smartui.takeScreenshotaction via hooks
- Demonstrates running tests with multiple browser scenarios
- Tests Chrome and Microsoft Edge browsers
- Uses per-VU iterations executor
The test files include capabilities configuration for LambdaTest Cloud. Update smartUIProjectName to match your SmartUI project name:
const capabilities = {
"browserName": "Chrome",
"browserVersion": "latest",
"LT:Options": {
"user": __ENV.LT_USERNAME,
"accessKey": __ENV.LT_ACCESS_KEY,
"smartUIProjectName": "K6_Test_Sample", // Update this to match your project
}
};- Use descriptive, unique names for each screenshot
- Include scenario and state information
- Avoid special characters
- Use consistent naming conventions
- After page loads
- After user interactions
- At different stages of user journeys
- After state changes
- Always set
K6_BROWSER_ENABLED=trueenvironment variable - Use
page.waitForLoadState()before screenshots - Handle errors gracefully in scenarios
- Use proper VU (Virtual User) configuration
import { chromium } from 'k6/experimental/browser';
export default async function () {
const browser = chromium.launch();
const page = browser.newPage();
await page.goto('https://www.lambdatest.com');
await page.waitForLoadState('networkidle');
await captureSmartUIScreenshot(page, "homepage");
browser.close();
}import { chromium } from 'k6/experimental/browser';
export const options = {
scenarios: {
ui: {
executor: 'shared-iterations',
options: {
browser: {
type: 'chromium',
},
},
},
},
};
export default async function () {
const browser = chromium.launch();
const page = browser.newPage();
await page.goto('https://www.lambdatest.com');
await captureSmartUIScreenshot(page, "homepage");
browser.close();
}import { check } from 'k6';
import { chromium } from 'k6/experimental/browser';
export default async function () {
const browser = chromium.launch();
const page = browser.newPage();
const response = await page.goto('https://www.lambdatest.com');
check(response, {
'status is 200': (r) => r.status() === 200,
});
await captureSmartUIScreenshot(page, "homepage-performance-test");
browser.close();
}name: K6 SmartUI Tests
on: [push, pull_request]
jobs:
visual-tests:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Install K6
run: |
sudo gpg -k
sudo gpg --no-default-keyring --keyring /usr/share/keyrings/k6-archive-keyring.gpg --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys C5AD17C747E3415A3642D57D77C6C491D6AC1D69
echo "deb [signed-by=/usr/share/keyrings/k6-archive-keyring.gpg] https://dl.k6.io/deb stable main" | sudo tee /etc/apt/sources.list.d/k6.list
sudo apt-get update
sudo apt-get install k6
- name: Run K6 with SmartUI
env:
K6_BROWSER_ENABLED: true
LT_USERNAME: ${{ secrets.LT_USERNAME }}
LT_ACCESS_KEY: ${{ secrets.LT_ACCESS_KEY }}
run: |
k6 run k6-smartui.jsSolution: Always set the environment variable:
export K6_BROWSER_ENABLED=true
k6 run k6-smartui.jsSolution:
- Verify
LT_USERNAMEandLT_ACCESS_KEYare set - Check
smartUIProjectNamein capabilities matches your project - Ensure K6 browser automation is enabled
- Wait for page to load before taking screenshots
Solution:
- Verify LambdaTest credentials are correct
- Check credentials in LambdaTest Profile Settings
- Ensure no extra spaces in environment variables
Solution:
- Verify K6 version supports browser automation (v0.43.0+)
- Ensure
K6_BROWSER_ENABLED=trueis set - Check K6 installation:
k6 version
Update capabilities in your test file:
const capabilities = {
"browserName": "Chrome",
"browserVersion": "latest",
"LT:Options": {
"user": __ENV.LT_USERNAME,
"accessKey": __ENV.LT_ACCESS_KEY,
"platform": "Windows 10",
"smartUIProjectName": "Your_Project_Name", // Update this
"smartUIBaseline": false // Set to false to update baseline
}
};Configure virtual users appropriately:
export const options = {
vus: 1, // Number of virtual users
iterations: 1, // Number of iterations
scenarios: {
ui: {
executor: 'shared-iterations',
options: {
browser: {
type: 'chromium',
},
},
},
},
};After running the tests, visit your LambdaTest dashboard to view the running test and SmartUI project to see captured screenshots.
- SmartUI K6 Onboarding Guide
- K6 Documentation
- K6 Browser Automation
- LambdaTest K6 Documentation
- SmartUI Dashboard
- LambdaTest Community
- K6 browser automation requires
K6_BROWSER_ENABLED=trueenvironment variable - The repository uses hooks-based SmartUI integration (not SDK-based)
- Screenshots are captured using
smartui.takeScreenshotaction viapage.evaluate() - K6 browser automation is available in K6 v0.43.0 and later