Skip to main content

Handle Dynamic Data in Visual Tests

What is Dynamic Data?

Dynamic data refers to content on your web pages that changes between test runs, such as timestamps, user IDs, session tokens, randomly generated content, or data that updates in real-time. When conducting visual regression tests, these dynamic elements can cause false positives because the content differs between the baseline and current screenshots, even when the actual UI design remains unchanged.

SmartUI provides two powerful options to handle dynamic data:

  • Ignore DOM Elements: Exclude specific elements from visual comparison
  • Select DOM Elements: Include only specific elements in visual comparison
Related Documentation

If you're experiencing high mismatch percentages or false positives, see our Comprehensive Troubleshooting Guide for solutions. For project-level settings, check Project Settings.

When to Use Dynamic Data Handling

You should use dynamic data handling in the following scenarios:

  1. Timestamps and Dates: Pages displaying current date/time that changes on each load
  2. User-Specific Content: User IDs, usernames, or personalized content
  3. Session Tokens: Authentication tokens or session identifiers
  4. Random Content: Randomly generated IDs, UUIDs, or nonces
  5. Live Data: Stock prices, weather data, or other real-time information
  6. Advertisements: Rotating ads or promotional content
  7. Cookie Banners: Consent banners that appear differently each time
  8. Notifications: Unread message counts or notification badges

Dynamic Data Handling Methods

Ignore DOM Elements

Use ignoreDOM to exclude specific elements from visual comparison. This is useful when you want to compare the entire page but ignore certain dynamic elements.

Syntax

let options = {
ignoreDOM: {
id: ["element-id-1", "element-id-2"],
class: ["class-name-1", "class-name-2"],
cssSelector: ["selector-1", "selector-2"],
xpath: ["xpath-1", "xpath-2"]
}
}
smartuiSnapshot(driver, 'Screenshot Name', options);

Examples by Selector Type

JavaScript (Selenium)

const { Builder } = require('selenium-webdriver');
const { smartuiSnapshot } = require('@lambdatest/selenium-driver');

let driver = await new Builder().forBrowser("chrome").build();
await driver.get('https://example.com');

let options = {
ignoreDOM: {
id: ["timestamp", "user-id", "session-token"]
}
};
await smartuiSnapshot(driver, 'Home Page', options);

Java (Selenium)


HashMap<String, ArrayList<String>> ignoreDOM = new HashMap<>();
ArrayList<String> ids = new ArrayList<>();
ids.add("timestamp");
ids.add("user-id");
ids.add("session-token");
ignoreDOM.put("id", ids);

HashMap<String, Object> options = new HashMap<>();
options.put("ignoreDOM", ignoreDOM);

SmartUISnapshot.smartuiSnapshot(driver, "Home Page", options);

Python (Selenium)

from lambdatest import smartui_snapshot

options = {
"ignoreDOM": {
"id": ["timestamp", "user-id", "session-token"]
}
}
smartui_snapshot(driver, "Home Page", options)

Troubleshooting

Issue: Elements Not Being Ignored

Possible Causes:

  • Selector is incorrect or doesn't match any elements
  • Element is loaded dynamically after the snapshot
  • Selector syntax error

Solutions:

  1. Verify the selector using browser DevTools
  2. Add a wait before taking the snapshot to ensure elements are loaded
  3. Check selector syntax (CSS selectors vs XPath)
// Wait for element before snapshot
await driver.wait(until.elementLocated(By.id('timestamp')), 5000);
let options = {
ignoreDOM: {
id: ["timestamp"]
}
};
await smartuiSnapshot(driver, 'Page', options);

Additional Resources

Test across 3000+ combinations of browsers, real devices & OS.

Book Demo

Help and Support

Related Articles