Camera Service Service
wdio-camera-service is a 3rd party package, for more information please see GitHub | npm
A WebdriverIO service that enables camera feed injection for testing applications that use camera/video inputs. This service allows you to mock camera feeds with pre-recorded video files during automated testing.
Featuresโ
- ๐ฅ Inject custom video feeds into Chrome browsers during testing
- ๐ Dynamically change camera sources during test execution
- ๐ Automatic video directory management
- ๐ Automatic format conversion - Use MP4, WebM, PNG, JPG and more (requires FFmpeg)
- ๐พ Smart caching - Converted files are cached to avoid re-conversion
- ๐งช Perfect for testing camera-dependent applications like QR code scanners, video conferencing, etc.
Installationโ
npm install --save-dev wdio-camera-service
Configurationโ
Add the camera service to your WebdriverIO configuration:
// wdio.conf.ts
export const config: WebdriverIO.Config = {
// ... other config
services: [
['camera', {
defaultCameraFeed: './camera/default.mjpeg',
videoDirectory: './camera/video',
}],
],
// ... other config
};
Service Optionsโ
| Option | Type | Required | Default | Description |
|---|---|---|---|---|
defaultCameraFeed | string | โ | - | Path to the default video file |
videoDirectory | string | โ | - | Directory for session-specific video files |
ffmpegPath | string | โ | 'ffmpeg' | Custom path to FFmpeg executable |
cacheEnabled | boolean | โ | true | Enable caching of converted files |
outputFormat | 'mjpeg' | 'y4m' | โ | 'mjpeg' | Output format for converted files |
Supported Formatsโ
The service supports multiple input formats with automatic conversion:
Native Formats (No Conversion)โ
.mjpeg- Motion JPEG.y4m- YUV4MPEG2
Video Formats (Requires FFmpeg)โ
.mp4- MPEG-4 Video.webm- WebM Video.avi- AVI Video.mov- QuickTime Video.gif- GIF (animated or static)
Image Formats (Requires FFmpeg)โ
Images are converted to a single-frame MJPEG (Chrome loops it automatically):
.png- PNG Image.jpg/.jpeg- JPEG Image.bmp- Bitmap Image
FFmpeg Requirementโ
FFmpeg is only required when using non-native formats (MP4, WebM, PNG, etc.).
If you only use .mjpeg or .y4m files, FFmpeg is not needed.
Installing FFmpegโ
macOS:
brew install ffmpeg
Ubuntu/Debian:
sudo apt-get install ffmpeg
Windows:
winget install FFmpeg
# or
choco install ffmpeg
Or download from: ffmpeg.org
Browser Supportโ
- โ Chrome/Chromium/Android Chrome - Full support
- โ Firefox - Not supported
- โ Safari - Not supported
- โ Edge - Not supported (unless Chromium-based)
Note: This service only works with Chrome/Chromium browsers as it relies on Chrome-specific command line arguments for camera mocking.
Android SDK Supports (With Chrome)โ
| SDK | Version | Support? |
|---|---|---|
| 31 | 12 | โ |
| 33 | 13 | โ |
| 34 | 14 | โ |
| 35 | 15 | โ |
| 36 | 16 | โ |
Usageโ
Basic Usageโ
The service automatically injects the default camera feed when the browser starts:
describe('Camera Tests', () => {
it('should use default camera feed', async () => {
await browser.url('https://example.com/camera-app');
// Your default camera feed is now active
});
});
Using Different Formatsโ
You can use various formats as your default camera feed:
// wdio.conf.ts
export const config: WebdriverIO.Config = {
services: [
['camera', {
// Use a PNG image as the default feed (Chrome loops it automatically)
defaultCameraFeed: './camera/qr-code.png',
videoDirectory: './camera/video',
}],
],
};
Changing Camera Sourceโ
Use the changeCameraSource command to switch video feeds during test execution:
describe('Camera Tests', () => {
it('should change camera source dynamically', async () => {
await browser.url('https://example.com/camera-app');
// Start with default feed, then switch to a different video
await browser.changeCameraSource('path/to/barcode-video.mjpeg');
// Test barcode scanning functionality
await expect($('#barcode-result')).toHaveText('123456789');
// Switch to a PNG image (automatically converted to video)
await browser.changeCameraSource('path/to/qr-code.png');
// Test QR code scanning
await expect($('#qr-result')).toHaveText('QR Content');
});
});
File Structureโ
project/
โโโ camera/
โ โโโ default.mjpeg # Default camera feed (MJPEG)
โ โโโ qr-code.png # QR code image (auto-converted)
โ โโโ barcode-sample.mp4 # Barcode video (auto-converted)
โ โโโ video/ # Auto-generated session videos
โ โโโ 0-0.mjpeg # Session-specific copies
โ โโโ .cache/ # Cached converted files
โ โโโ abc123.mjpeg # Hash-based cache
โโโ test/
โ โโโ specs/
โ โโโ camera.e2e.ts # Test files
โโโ wdio.conf.ts # WebdriverIO configuration
Video File Requirementsโ
Native Format (Recommended)โ
- Format: MJPEG (Motion JPEG) or Y4M
- Extension:
.mjpegor.y4m - Location: Relative to your project root or absolute paths
Other Formatsโ
When using non-native formats, the service will automatically convert them using FFmpeg.
Converted files are cached in videoDirectory/.cache/ to avoid repeated conversions.
Creating MJPEG Files Manuallyโ
You can convert existing video files to MJPEG format using FFmpeg:
# Convert MP4 to MJPEG
ffmpeg -i input.mp4 -q:v 2 output.mjpeg
# Convert image to single-frame MJPEG
ffmpeg -i input.png -frames:v 1 -q:v 2 output.mjpeg
API Referenceโ
Browser Commandsโ
browser.changeCameraSource(videoFilePath: string)โ
Changes the active camera source to a different video file.
Parameters:
videoFilePath(string): Path to the video/image file (relative to project root). Supports all formats listed above.
Returns: Promise<void>
Example:
// Use MJPEG file (native)
await browser.changeCameraSource('camera/new-feed.mjpeg');
// Use MP4 file (auto-converted)
await browser.changeCameraSource('camera/video.mp4');
// Use PNG image (auto-converted to looping video)
await browser.changeCameraSource('camera/qr-code.png');
Error Handlingโ
The service will throw errors in the following cases:
- Missing configuration: When
defaultCameraFeedorvideoDirectoryis not specified - File not found: When the specified video file doesn't exist
- FFmpeg not found: When using non-native formats without FFmpeg installed
- Conversion failed: When FFmpeg fails to convert a file
- Unsupported format: When using an unrecognized file extension
- Unsupported browser: When used with non-Chrome browsers (logs warning instead of error)
Error Typesโ
| Error | Description |
|---|---|
FfmpegNotFoundError | FFmpeg required but not installed |
ConversionError | FFmpeg conversion failed |
UnsupportedFormatError | Unknown file extension |
Example Test Casesโ
import { browser } from '@wdio/globals';
describe('Camera Application Tests', () => {
it('should scan QR codes from image', async () => {
await browser.url('https://qr-scanner-app.com');
// Inject QR code image (auto-converted to video)
await browser.changeCameraSource('camera/qr-code.png');
await $('#start-camera').click();
await expect($('#qr-result')).toHaveText('Expected QR Content');
});
it('should detect faces from video', async () => {
await browser.url('https://face-detection-app.com');
// Inject face video (MP4 auto-converted)
await browser.changeCameraSource('camera/face.mp4');
await $('#start-detection').click();
await expect($('#face-count')).toHaveText('1 face detected');
});
});
Troubleshootingโ
Common Issuesโ
-
"FFmpeg is required but not found"
- Install FFmpeg using your package manager
- Or provide a custom path via
ffmpegPathoption
-
"Default camera feed does not exist"
- Ensure the
defaultCameraFeedpath is correct - Verify the file exists and has proper permissions
- Ensure the
-
"New source camera feed does not exist"
- Check the path passed to
changeCameraSource() - Verify the file exists
- Check the path passed to
-
"Unsupported format"
- Check the file extension is in the supported list
- Ensure FFmpeg is installed for non-native formats
-
Camera not working in browser
- Verify you're using Chrome/Chromium
- Check browser console for permission errors
- Ensure the test site allows camera access
Debug Tipsโ
- Set
logLevel: 'debug'in your WebdriverIO config to see detailed logs - Check the
videoDirectory/.cache/for converted files - Verify MJPEG files play correctly in media players
- Run
ffmpeg -versionto check FFmpeg installation
Contributingโ
- Fork the repository
- Create a feature branch
- Add tests for new functionality
- Submit a pull request
Licenseโ
MIT Licenseโsee LICENSE file for details