Skip to content

Commit e0df8a2

Browse files
authored
Merge pull request #2213 from RushilK7/stage
Stage
2 parents 2a0fa56 + b3eefa4 commit e0df8a2

File tree

1 file changed

+160
-80
lines changed

1 file changed

+160
-80
lines changed

docs/smartui-custom-css.md

Lines changed: 160 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
11
---
22
id: smartui-custom-css
3-
title: SmartUI customCSS
4-
sidebar_label: customCSS
5-
description: Use customCSS to inject test-only CSS via SmartUI CLI using a file path or embedded string in smartui-web.json.
6-
slug: smartui-custom-css/
3+
title: Custom CSS Injection in SmartUI
4+
sidebar_label: Custom CSS
5+
description: Learn how to use SmartUI's customCSS feature to inject test-only CSS styles during snapshots without modifying your application code
76
keywords:
8-
- smartui
97
- custom css
8+
- visual regression testing
9+
- css injection
10+
- smartui cli
1011
- visual testing
11-
- cli
12-
- configuration
13-
- percy css
12+
- test-only css
13+
- css configuration
14+
- visual stability
15+
url: https://www.lambdatest.com/support/docs/smartui-custom-css/
16+
site_name: LambdaTest
17+
slug: smartui-custom-css/
1418
---
1519

1620
<script type="application/ld+json"
@@ -37,94 +41,147 @@ keywords:
3741
}}
3842
></script>
3943

40-
## SmartUI customCSS
44+
import Tabs from '@theme/Tabs';
45+
import TabItem from '@theme/TabItem';
46+
47+
---
48+
49+
Custom CSS injection is a specialized feature in SmartUI that allows you to apply test-only styles during snapshot capture without modifying your application code. This feature enables you to stabilize visual tests by normalizing dynamic content, enforcing consistent styling across environments, and masking sensitive information—all while keeping your visual testing logic centralized and maintainable.
4150

42-
Use customCSS to apply test‑only styles at snapshot time without changing your app. It supports two input styles:
51+
### Why Custom CSS Matters
4352

44-
- File path (recommended for maintainability)
45-
- Embedded CSS string (quick and portable)
53+
1. **Stabilize Visual Diffs**: Hide or normalize volatile UI elements (ads, rotating banners, dynamic counters, time-based content) to reduce false positives in your visual regression tests.
54+
55+
2. **Environment Parity**: Enforce consistent fonts, themes, and spacing across different operating systems, browsers, and devices for predictable rendering and accurate comparisons.
56+
57+
3. **Non-Intrusive Testing**: Avoid modifying application code or injecting styles directly in test scripts. Keep all visual adjustments in a centralized configuration, making it easier to maintain and review.
58+
59+
4. **Enhanced Test Reliability**: Reduce test flakiness by masking dynamic elements that change between test runs, ensuring your visual tests focus on meaningful UI changes rather than transient content.
4660

4761
## Prerequisites
48-
- Node.js v20.3+ recommended
49-
- SmartUI CLI v4.1.40+ (exec and capture supported)
50-
- PROJECT_TOKEN
62+
63+
Before using the Custom CSS feature, ensure you meet the following requirements:
64+
65+
- Node.js v20.3+ (recommended)
66+
- SmartUI CLI v4.1.40+ (supports both `exec` and `capture` commands)
67+
- Valid PROJECT_TOKEN configured in your environment
5168

5269
---
5370

54-
## Why customCSS Matters
71+
# Custom CSS Configuration in SmartUI
5572

56-
1. **Stabilize Visual Diffs**: Hide or normalize volatile UI (ads, rotating banners, counters) to reduce false positives.
57-
2. **Environment Parity**: Enforce consistent fonts, themes, and spacing across different OS/browsers for predictable rendering.
58-
3. **Non‑Intrusive**: Avoid modifying application code or injecting styles in tests—keep visual adjustments in one place.
73+
SmartUI supports two methods for providing custom CSS: file path (recommended for maintainability) and embedded string (quick and portable). Choose the method that best fits your workflow.
5974

60-
---
75+
## Method 1: File Path (Recommended)
6176

62-
## 1) Configuration: Two Ways to Provide customCSS
77+
The file path method is recommended for larger stylesheets and team collaboration. Create a separate CSS file in your project and reference it in your SmartUI configuration.
6378

64-
A) File path method (recommended)
65-
- Put your CSS rules into a file (e.g., `code.css`) at the project root (or any path you prefer).
66-
- Reference that path in your SmartUI config.
79+
**Steps:**
6780

68-
```json
81+
1. Create a CSS file (e.g., `visual-test-styles.css` or `code.css`) in your project directory.
82+
83+
2. Add your CSS rules to the file:
84+
85+
```css title="visual-test-styles.css"
86+
/* General samples: pick what suits your use case */
87+
88+
/* 1) Normalize fonts for consistent rendering */
89+
body { font-family: system-ui, -apple-system, Segoe UI, Roboto, sans-serif; }
90+
91+
/* 2) Hide flaky, time-based banners or rotating promos */
92+
.promo-banner, [data-testid="rotating-banner"] { display: none !important; }
93+
94+
/* 3) Freeze dynamic badges/counters that change every run */
95+
[data-badge], .cart-count { visibility: hidden; }
96+
```
97+
98+
3. Reference the file path in your SmartUI configuration:
99+
100+
```json title="smartui-web.json"
69101
{
70-
"web": { "browsers": ["chrome"], "viewports": [[1440, 900]] },
102+
"web": {
103+
"browsers": ["chrome"],
104+
"viewports": [[1440, 900]]
105+
},
71106
"enableJavaScript": true,
72-
"customCSS": "./code.css"
107+
"customCSS": "./visual-test-styles.css"
73108
}
74109
```
75110

76-
B) Embedded string method (single‑line)
77-
- Provide the CSS directly as a JSON string. Use a single line (avoids path misdetection) and escape quotes if needed.
111+
## Method 2: Embedded String
112+
113+
The embedded string method is useful for quick edits and single-use CSS rules. Provide the CSS directly as a JSON string value.
78114

79-
```json
115+
**Important Notes:**
116+
117+
- Keep CSS single-line to avoid JSON parsing issues
118+
- Escape quotes properly (`'` or `\"`)
119+
- Use `\n` for newlines if needed (though single-line is preferred)
120+
121+
```json title="smartui-web.json"
80122
{
81-
"web": { "browsers": ["chrome"], "viewports": [[1440, 900]] },
123+
"web": {
124+
"browsers": ["chrome"],
125+
"viewports": [[1440, 900]]
126+
},
82127
"enableJavaScript": true,
83-
"customCSS": "body{font-family:'Inter',sans-serif;} .banner,.ad{display:none!important;}"
128+
"customCSS": "body{font-family:'Inter',sans-serif!important;} .banner,.ad{display:none!important;}"
84129
}
85130
```
86131

87-
Notes
88-
- Place customCSS only at the top level of your config (not inside web).
89-
- For multi‑line CSS in JSON, either:
90-
- keep it compact in one line, or
91-
- use the file‑path method to avoid escaping/formatting issues.
92-
- Paths in customCSS are relative to your project root.
132+
## Configuration Guidelines
93133

94-
---
134+
- **Placement**: The `customCSS` property must be placed at the top level of your configuration file (not inside the `web` object). Placing it inside `web` will result in a "must NOT have additional properties" error.
95135

96-
## 2) Running with SmartUI (production)
136+
- **Path Resolution**: File paths are relative to your project root directory. Ensure the CSS file exists at the specified path. Files outside the project directory may not be accessible.
97137

98-
Use either exec (wrap your test runner) or capture (static URLs). Ensure CLI is 4.1.40+.
138+
- **CSS Specificity**: Your custom CSS will be injected at snapshot time. Use `!important` declarations if you need to override existing styles. If CSS is overridden by inline styles, increase selector specificity (e.g., `.target-class``#specific-id .target-class`).
99139

100-
Exec example (generic):
101-
```bash
102-
export PROJECT_TOKEN='YOUR_PROJECT_TOKEN'
103-
npx smartui --config smartui-web.json exec -- <your-test-command>
104-
```
140+
- **Multi-line CSS**: For multi-line CSS, prefer the file path method to avoid JSON escaping complexity. If using embedded strings, keep rules compact and single-line.
105141

106-
Capture example (static URLs):
107-
```bash
108-
export PROJECT_TOKEN='YOUR_PROJECT_TOKEN'
109-
npx smartui capture --config smartui-web.json
110-
```
142+
- **Method Selection**: Choose the file path method for larger stylesheets and team collaboration. Use embedded strings only for quick, single-use CSS rules.
143+
144+
- **CSS Organization**: Keep your CSS snapshot-specific—target only elements that need stabilization or normalization. Avoid broad selectors that might affect unintended elements.
145+
146+
- **JSON Escaping**: When using embedded strings, escape quotes properly:
147+
- Use single quotes within CSS strings: `"body{font-family:'Inter',sans-serif;}"`
148+
- Or escape double quotes: `"body{font-family:\"Inter\",sans-serif;}"`
111149

112-
What you’ll see
113-
- CLI validates and injects your CSS at snapshot time
114-
- A CSS Injection Report in logs with the number of rules applied and any selectors with no matches
150+
- **Selector Verification**: If the CSS Injection Report shows "no elements found" or "invalid selector" errors:
151+
- Verify the element exists at snapshot time (use browser dev tools)
152+
- Check selector specificity—it may need to be more specific or less specific
153+
- Consider timing issues—ensure the element is rendered before snapshot capture
154+
- Check the CSS Injection Report in logs for detailed feedback
155+
156+
- **CLI Version**: Ensure SmartUI CLI v4.1.40+ is installed. You can verify this by running `npx smartui --version`. Older versions may not support the `customCSS` feature.
157+
158+
- **Waiting for UI Readiness**: If you need to wait for UI elements to be ready before snapshots, add these options to your configuration:
159+
```json
160+
{
161+
"waitForTimeout": 2000,
162+
"waitForPageRender": 5000,
163+
"customCSS": "./visual-test-styles.css"
164+
}
165+
```
115166

116167
---
117168

169+
118170
## Known Limitations
119171

120-
- Can be overridden by higher‑specificity inline styles; increase selector specificity if needed.
172+
The Custom CSS feature has the following limitations:
173+
174+
- **Specificity Constraints**: Custom CSS can be overridden by higher-specificity inline styles. Increase your selector specificity or use `!important` declarations if needed.
175+
176+
- **Snapshot-Only Application**: CSS is only injected during snapshot capture and does not affect your application's runtime behavior.
177+
178+
- **File Path Resolution**: Ensure CSS file paths are correctly specified relative to your project root. Files outside the project directory may not be accessible.
121179

122180
---
123181

124-
import Tabs from '@theme/Tabs';
125-
import TabItem from '@theme/TabItem';
182+
## Use Cases for Custom CSS
126183

127-
## 3) Sample Use Cases
184+
The custom CSS feature is particularly valuable in the following scenarios:
128185

129186
<Tabs>
130187
<TabItem value="stabilize" label="Stabilize Dynamic UI" default>
@@ -206,37 +263,60 @@ header, footer, nav { background: rgba(0,0,0,.45) !important; box-shadow: none !
206263

207264
---
208265

209-
## 4) Tips and Troubleshooting
266+
## Minimal Configuration Templates
210267

211-
- Prefer file path for larger stylesheets and team reviews.
212-
- For embedded strings, keep CSS single‑line or very carefully escaped (quotes, no raw newlines).
213-
- If a selector shows “no elements found” in the report, verify it exists at snapshot time or loosen/tighten the selector.
214-
- Keep CSS snapshot‑specific: target only what you need for stable and meaningful diffs.
215-
- If you need to wait for UI readiness, use SmartUI config waits:
216-
- `"waitForTimeout": 2000`
217-
- `"waitForPageRender": 5000`
268+
### File Path Template
218269

219-
---
220-
221-
## 5) Minimal templates
222-
223-
File path template:
224-
```json
270+
```json title="smartui-web.json"
225271
{
226-
"web": { "browsers": ["chrome"], "viewports": [[1440, 900]] },
227-
"enableJavaScript": true,
228-
"customCSS": "./path/to/visual.css"
272+
"web": {
273+
"browsers": ["chrome"],
274+
"viewports": [[1440, 900]]
275+
},
276+
"customCSS": "./path/to/visual-test-styles.css"
229277
}
230278
```
231279

232-
Embedded string template:
233-
```json
280+
### Embedded String Template
281+
282+
```json title="smartui-web.json"
234283
{
235-
"web": { "browsers": ["chrome"], "viewports": [[1440, 900]] },
236-
"enableJavaScript": true,
237-
"customCSS": "/* your CSS (single-line) */ body{font-family:'Inter',sans-serif;} .ad{display:none!important;}"
284+
"web": {
285+
"browsers": ["chrome"],
286+
"viewports": [[1440, 900]]
287+
},
288+
"customCSS": "body{font-family:'Inter',sans-serif!important;} .ad,.banner{display:none!important;}"
238289
}
239290
```
240291

292+
---
293+
294+
## Additional Resources
295+
296+
- [SmartUI CLI Documentation](/support/docs/smartui-cli/)
297+
- [SmartUI Configuration Options](/support/docs/smartui-cli/)
298+
- [Visual Regression Testing Guide](/support/docs/smart-visual-testing/)
299+
- [Layout Comparison Documentation](/support/docs/smartui-layout-comparison/)
300+
301+
---
302+
303+
<nav aria-label="breadcrumbs">
304+
<ul className="breadcrumbs">
305+
<li className="breadcrumbs__item">
306+
<a className="breadcrumbs__link" target="_self" href="https://www.lambdatest.com">
307+
Home
308+
</a>
309+
</li>
310+
<li className="breadcrumbs__item">
311+
<a className="breadcrumbs__link" target="_self" href="https://www.lambdatest.com/support/docs/">
312+
Support
313+
</a>
314+
</li>
315+
<li className="breadcrumbs__item breadcrumbs__item--active">
316+
<span className="breadcrumbs__link">Custom CSS</span>
317+
</li>
318+
</ul>
319+
</nav>
320+
241321

242322

0 commit comments

Comments
 (0)