Visual Validation is a powerful tool designed to compare two images and detect any differences between them. It is particularly useful in scenarios such as UI regression testing, where you want to ensure that visual changes between versions of a product or webpage haven't occurred unintentionally. The tool compares the provided image to a reference image (an earlier version) to confirm if there are any discrepancies.
- Image Comparison: Compares two images to detect visual differences.
- Regressions Detection: Ideal for detecting UI regressions where layout or visual changes might affect the user interface.
- Easy Integration: Simple to integrate into your test automation framework.
- Single Function Call: Take screenshots and compare them in one operation.
- W3C Mode Optimized: Optimized for modern W3C WebDriver implementations with direct Promise-based screenshot methods.
- Test Isolation: Automatic error isolation between test runs to prevent cross-test contamination.
- Clean Reporting: Professional Cucumber report integration with focused error messages.
To add the Visual Validation tool to your project, you can use pnpm:
- Open your terminal and navigate to your project directory.
- Run the following command:
pnpm add klassijs-visual-validation
Here's a guide on how to use Visual Validation in your project:
Import the visual-validation module into your code:
const { takeImage, compareImage, ImageAssertion, clearErrors } = require('klassijs-visual-validation');Use the takeImage method to take a screenshot and automatically compare it with the baseline:
await takeImage('screenshot.png');This single call will:
- Take a screenshot of the entire page
- Compare it with the baseline image
- Generate diff images if differences are found
- Log the comparison results
// Take screenshot of a specific element and compare
await takeImage('button.png', '.submit-button');
// Hide elements during screenshot and compare
await takeImage('clean-page.png', null, '.header, .footer');
// Take screenshot without comparison
await takeImage('screenshot.png', null, '', false);
// Use custom tolerance for comparison
await takeImage('screenshot.png', null, '', true, 0.1);
// Add wait time before capture
await takeImage('screenshot.png', null, '', true, 0.2, 500);If you need to perform comparison separately:
await takeImage(fileName, elementSnapshot, elementsToHide);
await compareImage('path/to/actual-image.png');Here's a complete example that demonstrates how to use the Visual Validation tool:
const { takeImage } = require('klassijs-visual-validation');
async function validateVisualChanges() {
// Take screenshot and compare with baseline in one call
await takeImage('homepage.png');
// Take screenshot of specific element and compare
await takeImage('login-form.png', '.login-container');
// Hide dynamic content and compare
await takeImage('clean-dashboard.png', null, '.user-info, .timestamp');
// Multiple images in sequence (all errors will be collected)
await takeImage('mango_1-0.png');
await takeImage('mango_2-0.png');
await takeImage('mango_3-0.png');
await takeImage('mango_4-0.png');
}
validateVisualChanges();takeImage(fileName, elementSnapshot, elementsToHide, shouldCompare, expectedTolerance, waitBeforeCapture)fileName(string, required): Name of the screenshot fileelementSnapshot(string, optional): CSS selector for specific element to screenshotelementsToHide(string, optional): CSS selectors of elements to hide during screenshotshouldCompare(boolean, optional): Whether to perform comparison (default: true)expectedTolerance(number, optional): Tolerance for comparison (default: 0.2)waitBeforeCapture(number, optional): Wait time in milliseconds before taking screenshot (default: 100)
// Hide dynamic elements like timestamps, user info, etc.
await takeImage('dashboard.png', null, '.timestamp, .user-info, .notification');// Use different tolerance for different types of content
await takeImage('text-content.png', null, '', true, 0.1); // Strict tolerance
await takeImage('image-content.png', null, '', true, 0.5); // Relaxed tolerance// Screenshot specific elements for focused testing
await takeImage('header.png', '.site-header');
await takeImage('footer.png', '.site-footer');
await takeImage('navigation.png', '.main-nav');The tool is optimized for W3C mode WebDriver implementations, which is the modern standard for browser automation.
W3C Mode Features:
- Uses direct Promise-based screenshot methods for optimal performance
- Compatible with modern WebDriver implementations
- Optimized for WebdriverIO v9+, Selenium 4+, Appium, BrowserStack, Sauce Labs, and other modern services
- No callback complexity or mode detection overhead
Note: This tool is designed for W3C mode WebDriver implementations. If you're using legacy Classic mode WebDriver, you may need to upgrade to a W3C-compatible version.
- All image comparison failures are automatically collected
- Errors are isolated between test runs
- Comprehensive error reporting at the end of each test
- Clean, professional error messages in Cucumber reports
- No debug console clutter in reports
- Focused error information for stakeholders
- Each test run starts with a clean errors array
- No cross-test error contamination
- Professional test framework behavior
We welcome contributions! If you encounter any bugs or have suggestions for improvements, please open an issue or submit a pull request.
Licenced under MIT License © 2016 Larry Goddard