@@ -104,52 +104,209 @@ npx smartui exec node sdkCloud.js
104104
105105## Testing with LambdaTest Hooks
106106
107- This repository also includes examples for using SmartUI with LambdaTest Hooks integration.
107+ This repository also includes examples for using SmartUI with LambdaTest Hooks integration. Hooks-based integration allows you to use SmartUI directly within your existing LambdaTest Cloud automation tests without requiring the SmartUI CLI.
108108
109- ### Hooks Integration
109+ ### SDK vs Hooks: Which Approach to Use?
110+
111+ ** SDK Approach (Recommended for Local Testing):**
112+ - ✅ Works with both local and cloud execution
113+ - ✅ Uses SmartUI CLI for configuration and execution
114+ - ✅ Supports multiple browsers and viewports via ` smartui-web.json `
115+ - ✅ Better for CI/CD integration
116+ - ✅ Requires ` PROJECT_TOKEN ` environment variable
117+
118+ ** Hooks Approach (Recommended for Cloud-Only Testing):**
119+ - ✅ Works only with LambdaTest Cloud Grid
120+ - ✅ No CLI required - direct integration with LambdaTest
121+ - ✅ Uses LambdaTest capabilities for configuration
122+ - ✅ Better for existing LambdaTest automation suites
123+ - ✅ Requires ` LT_USERNAME ` and ` LT_ACCESS_KEY ` environment variables
124+
125+ ### Hooks Integration Setup
110126
111127** Location:** See the ` hooks ` folder, where you can see all the ` examples ` scripts to setup your suite or run the demo.
112128
113- ** Purpose:** Enhance visual regression capabilities in your LambdaTest web automation tests.
129+ ** Purpose:** Enhance visual regression capabilities in your LambdaTest web automation tests running on LambdaTest Cloud Grid .
114130
115131** Documentation:** [ LambdaTest Selenium Visual Regression Documentation] ( https://www.lambdatest.com/support/docs/selenium-visual-regression/ ) .
116132
117133### Hooks Setup Steps
118134
119- 1 . Install the dependencies:
135+ #### 1. Install Dependencies
136+
137+ Navigate to the hooks directory and install dependencies:
138+
120139``` bash
121140cd hooks
122- npm i
141+ npm i selenium-webdriver
123142```
124143
125- 2 . Configure the capabilities (SmartUI Project Name and other SmartUI options) in ` examples/test.js ` :
144+ #### 2. Configure Environment Variables
145+
146+ Set your LambdaTest credentials:
147+
148+ ``` bash
149+ export LT_USERNAME=' your_username'
150+ export LT_ACCESS_KEY=' your_access_key'
151+ ```
152+
153+ #### 3. Configure Capabilities
154+
155+ In your test file (e.g., ` hooks/examples/test.js ` ), configure the capabilities with SmartUI options:
156+
126157``` javascript
158+ const USERNAME = process .env .LT_USERNAME ;
159+ const KEY = process .env .LT_ACCESS_KEY ;
160+
127161let capabilities = {
128162 platform: " Windows 10" ,
129163 browserName: " chrome" ,
130164 version: " latest" ,
131- visual: true ,
165+ visual: true , // Enable visual testing
166+ " user" : USERNAME ,
167+ " accessKey" : KEY ,
132168 name: " test session" ,
133169 build: " Automation Build" ,
134- " smartUI.project" : " <Your Project Name>" ,
135- " smartUI.build" : " <Your Build Name>" ,
136- " smartUI.baseline" : false
170+ " LT:Options" : {
171+ " smartUI.project" : " <Your Project Name>" , // Your SmartUI project name
172+ " smartUI.build" : " <Your Build Name>" , // Optional: Build name
173+ " smartUI.baseline" : false , // Set to true to update baseline
174+ " smartUI.options" : {
175+ " output" : {
176+ " errorColor" : { " red" : 200 , " green" : 0 , " blue" : 255 },
177+ " errorType" : " movement" ,
178+ " transparency" : 0.3 ,
179+ " largeImageThreshold" : 100 ,
180+ " useCrossOrigin" : false ,
181+ " outputDiff" : true
182+ },
183+ " scaleToSameSize" : true ,
184+ " ignore" : " antialiasing"
185+ }
186+ }
137187};
138188```
139189
140- 3 . Add the Screenshot hook in ` examples/test.js ` :
190+ #### 4. Connect to LambdaTest Grid
191+
192+ Create a WebDriver instance connected to LambdaTest Cloud:
193+
194+ ``` javascript
195+ const GRID_HOST = " @hub.lambdatest.com/wd/hub" ;
196+ const gridUrl = ` https://${ USERNAME } :${ KEY }${ GRID_HOST } ` ;
197+
198+ const driver = await new webdriver.Builder ()
199+ .usingServer (gridUrl)
200+ .withCapabilities (capabilities)
201+ .build ();
202+ ```
203+
204+ #### 5. Add Screenshot Hooks
205+
206+ Use ` driver.executeScript() ` to capture screenshots at any point in your test:
207+
141208``` javascript
209+ // Navigate to your page
210+ await driver .get (' https://www.lambdatest.com' );
211+
212+ // Take a screenshot with basic configuration
142213let config = {
143- screenshotName: ' <Name of your screenshot> '
214+ screenshotName: ' homepage- screenshot'
144215};
145216await driver .executeScript (" smartui.takeScreenshot" , config);
217+
218+ // Take a full-page screenshot
219+ let fullPageConfig = {
220+ screenshotName: ' homepage-full-page' ,
221+ fullPage: true
222+ };
223+ await driver .executeScript (" smartui.takeScreenshot" , fullPageConfig);
224+
225+ // Take a screenshot with custom options
226+ let customConfig = {
227+ screenshotName: ' homepage-custom' ,
228+ fullPage: true ,
229+ ignore: [" antialiasing" , " colors" ],
230+ boundingBoxes: [{ x: 100 , y: 100 , width: 200 , height: 200 }]
231+ };
232+ await driver .executeScript (" smartui.takeScreenshot" , customConfig);
146233```
147234
148- 4 . Run the script:
235+ #### 6. Run the Test
236+
237+ Execute your test script:
238+
149239``` bash
150- node examples/test.js
240+ node hooks/ examples/test.js
151241```
152242
243+ ### Advanced Hooks Examples
244+
245+ #### Multiple Screenshots in One Test
246+
247+ ``` javascript
248+ await driver .get (' https://www.lambdatest.com' );
249+
250+ // Screenshot 1: Homepage
251+ await driver .executeScript (" smartui.takeScreenshot" , {
252+ screenshotName: ' homepage'
253+ });
254+
255+ // Navigate and take another screenshot
256+ await driver .get (' https://www.lambdatest.com/pricing' );
257+ await driver .executeScript (" smartui.takeScreenshot" , {
258+ screenshotName: ' pricing-page'
259+ });
260+ ```
261+
262+ #### Screenshot with Ignore Areas
263+
264+ ``` javascript
265+ let config = {
266+ screenshotName: ' homepage-ignored' ,
267+ ignore: [" antialiasing" ],
268+ ignoredBoxes: [
269+ { x: 0 , y: 0 , width: 100 , height: 50 } // Ignore header area
270+ ]
271+ };
272+ await driver .executeScript (" smartui.takeScreenshot" , config);
273+ ```
274+
275+ #### Screenshot with Bounding Boxes (Compare Specific Area)
276+
277+ ``` javascript
278+ let config = {
279+ screenshotName: ' homepage-bounded' ,
280+ boundingBoxes: [
281+ { x: 100 , y: 200 , width: 800 , height: 400 } // Compare only this area
282+ ]
283+ };
284+ await driver .executeScript (" smartui.takeScreenshot" , config);
285+ ```
286+
287+ ### Hooks Configuration Options
288+
289+ The ` smartUI.options ` in capabilities supports various configuration options:
290+
291+ - ** errorColor** : RGB values for highlighting differences
292+ - ** errorType** : Type of error detection (` "movement" ` , ` "flat" ` , ` "flatDifferenceIntensity" ` , ` "movementDifferenceIntensity" ` , ` "diffOnly" ` )
293+ - ** transparency** : Opacity of the error overlay (0.0 to 1.0)
294+ - ** largeImageThreshold** : Threshold for large image comparison
295+ - ** useCrossOrigin** : Enable cross-origin resource sharing
296+ - ** outputDiff** : Generate difference images
297+ - ** scaleToSameSize** : Scale images to same size before comparison
298+ - ** ignore** : Ignore specific visual artifacts (` "antialiasing" ` , ` "colors" ` , ` "less" ` , ` "alpha" ` , ` "nothing" ` )
299+
300+ ### View Hooks Results
301+
302+ After running your hooks-based tests, visit the [ LambdaTest Automation Dashboard] ( https://automation.lambdatest.com/ ) to view:
303+ - Test execution status
304+ - Screenshots captured
305+ - Visual comparison results
306+ - Build and session details
307+
308+ Navigate to your SmartUI project in the [ SmartUI Dashboard] ( https://smartui.lambdatest.com/ ) to see detailed visual regression results.
309+
153310## Test Files
154311
155312### Cloud Test (` sdk/sdkCloud.js ` )
0 commit comments