Backstop Generator
Terminology
- Profile
- An entity that contains a set of tests (Scenarios) you want to run. It also defines one or more Viewports.
- Scenario
- A representation of a page you want to visually test. Each Scenario belongs to a Profile.
- Scenario Defaults
- Common settings defined in a Profile that apply to all Scenarios unless individually overridden.
- Viewport
- Defines the window size for testing. Typically configured for mobile, tablet, and desktop breakpoints.
How to Use This Module
1. Configure the Module
- Navigate to Configuration > Development > Backstop Generator (
/admin/config/development/backstop-generator
).
- Test domain is pre-filled with the current site URL. In the Reference domain field, enter the URL of the site you want to compare against (typically the live production site).
- Scenario Defaults and Profile Parameters allow you to set default values that new Profiles will inherit. These settings apply to new Profiles only and can be adjusted later per Profile.
2. Configure Your Viewports
Click the Viewports tab. When the module is installed, Viewports are automatically generated based on your default theme’s breakpoints.
Use the Viewport Generator to generate Viewports for other enabled themes with configured breakpoints. You can also manually create a Viewport using the Add Viewport button in the upper right corner.
3. Configure Your First Profile
- Click the Profile tab. You’ll see an empty list if no Profiles exist.
- Click Add Backstop Profile in the upper right corner.
- Enter a descriptive Title and Description.
- Select the Viewports to use for this Profile.
- Customize the Profile Parameters and Scenario Defaults as needed.
- Scroll to the Scenario Generator section:
- Select which languages to test.
- Check Homepage to include your site’s homepage.
- Select menus to use for generating Scenarios. You can set Menu Depth to determine how many levels deep the generator should go.
- Under Scenarios from content types, specify how many random nodes per content type you want to test.
- In Scenarios from Nodes and Paths, select specific nodes or manually add paths (e.g., Views or search pages). Use the format:
label | path
.
- Click Save and generate scenarios to create the Scenario list.
- Select the Scenarios to include and click Save.
- The Profile is now saved and a BackstopJS JSON file is generated.
Congratulations! You've created a visual regression test using BackstopJS.
4. Edit Any of Your Scenarios
If any Scenarios fail during testing, you can edit them individually. For example, hide dynamic elements like carousels using the hideSelectors setting.
Use the text filter to search for specific Scenarios you want to modify.
5. Run the Commands
Go to the Commands tab to view the terminal commands for running BackstopJS tests:
- Run the reference command to capture screenshots from the Reference site.
- Run the test command to compare those screenshots against your local or development environment.
Overview of BackstopJS
BackstopJS is an open-source tool designed for automating visual regression testing of web applications. It captures screenshots of web pages and compares them over time to detect unintended visual changes. This is especially helpful in Drupal projects for ensuring that theme changes or code updates do not introduce UI regressions.
Key Features
- Automated Screenshot Comparisons: Captures and compares screenshots to identify visual discrepancies.
- Responsive Design Testing: Tests across various screen sizes and devices.
- Integration with CI/CD Pipelines: Facilitates continuous visual testing within development workflows.
- Detailed Reporting: Generates reports highlighting differences between reference and test images.
Explanation of Configuration Fields
- viewports: An array defining screen sizes for testing. Each object includes:
- label: Descriptive name for the viewport.
- width: Viewport width in pixels.
- height: Viewport height in pixels.
- scenarios: An array of test scenarios. Each scenario can include:
- label: Descriptive name for the scenario.
- url: The URL of the page to test.
- referenceUrl: Optional URL for the reference image, if different from url.
- readyEvent: Browser event to wait for before capturing a screenshot.
- readySelector: Waits for this selector to appear before taking a screenshot.
- delay: Wait time (in milliseconds) after readyEvent or readySelector before capturing.
- hideSelectors: Array of selectors to hide from the page before taking a screenshot.
- removeSelectors: Array of selectors to remove from the page before taking a screenshot.
- hoverSelector: Selector to hover over before capturing.
- clickSelector: Selector to click before capturing.
- postInteractionWait: Wait time (in milliseconds) after interactions before capturing.
- selectors: Array of selectors defining the areas to capture. Defaults to the entire page if empty.
- selectorExpansion: If true, captures each matched selector separately.
- expect: Defines the number of expected mismatches.
- misMatchThreshold: Acceptable percentage of pixel mismatches before considering a test failed.
- requireSameDimensions: Ensures the reference and test images have the same dimensions.
For more information, see the BackstopJS GitHub Repository.