@@ -35,7 +35,9 @@ top, letting you see exactly what was tested and how.
35
35
- [ Embedding the Agent Script] ( #embedding-the-agent-script )
36
36
- [ Usage] ( #usage )
37
37
- [ Playwright] ( #playwright )
38
+ - [ Puppeteer] ( #puppeteer )
38
39
- [ Selenium] ( #selenium )
40
+ - [ Advanced Example] ( #advanced-example )
39
41
- [ Coverage Report Generation] ( #coverage-report-generation )
40
42
- [ Configuration] ( #configuration )
41
43
- [ .env] ( #configuration-via-env )
@@ -297,6 +299,105 @@ const tracker = new UICoverageTracker({ app: 'my-ui-app' });
297
299
298
300
```
299
301
302
+ ### Advanced Example
303
+
304
+ This example demonstrates how to use ` ui-coverage-scenario-tool-js ` with execution context to automatically track UI
305
+ coverage scenarios in Playwright — without manually passing the tracker through every layer of your app.
306
+
307
+ #### Step 1: Initialize the tracker
308
+
309
+ Create a single instance of ` UICoverageTracker ` that will be shared across all tests.
310
+
311
+ ` ./coverage.ts `
312
+
313
+ ``` typescript
314
+ import { UICoverageTracker } from ' ui-coverage-scenario-tool-js' ;
315
+
316
+ export const tracker = new UICoverageTracker ({ app: ' ui-course' });
317
+ ```
318
+
319
+ #### Step 2: Extend Playwright’s test with scenario setup and teardown
320
+
321
+ Create a custom test wrapper where:
322
+
323
+ - the scenario is started before each test (with a name and a link to an external TMS),
324
+ - and ended after the test is complete.
325
+
326
+ ` ./tests/base.ts `
327
+
328
+ ``` typescript
329
+ import { test as base } from ' @playwright/test' ;
330
+ import { coverageContext } from ' ui-coverage-scenario-tool-js' ;
331
+ import { tracker } from ' ../coverage' ;
332
+
333
+ export const test = base .extend <{}>({
334
+ async page({ page }, use , testInfo ) {
335
+ coverageContext .run (tracker , () => {
336
+ tracker .startScenario ({
337
+ url: ` https://company.tms.com/testcases/${testInfo .testId } ` , // link to external test case
338
+ name: testInfo .title
339
+ });
340
+ });
341
+
342
+ await use (page );
343
+
344
+ await coverageContext .get ()?.endScenario ();
345
+ }
346
+ });
347
+ ```
348
+
349
+ _ This uses ` AsyncLocalStorage ` internally to propagate the tracker across async calls — you don’t need to pass it
350
+ manually._
351
+
352
+ #### Step 3: Track interactions inside PageObjects
353
+
354
+ Inside your PageObject or UI component, simply call ` coverageContext.get() ` to record interactions:
355
+
356
+ ` ./pages/login-page.ts `
357
+
358
+ ``` typescript
359
+ import { Page } from ' @playwright/test' ;
360
+ import { ActionType , coverageContext , SelectorType } from ' ui-coverage-scenario-tool-js' ;
361
+
362
+ export class LoginPage {
363
+ constructor (private page : Page ) {
364
+ }
365
+
366
+ async clickLoginButton() {
367
+ await this .page .click (' #login' );
368
+
369
+ await coverageContext .get ()?.trackCoverage ({
370
+ selector: ' #login' ,
371
+ actionType: ActionType .Click ,
372
+ selectorType: SelectorType .CSS
373
+ });
374
+ }
375
+ }
376
+ ```
377
+
378
+ #### Step 4: Writing tests
379
+
380
+ Now your test stays clean and focused — and coverage tracking happens behind the scenes.
381
+
382
+ ` ./tests/important-feature.spec.ts `
383
+
384
+ ``` typescript
385
+ import { test } from ' ./base' ;
386
+ import { LoginPage } from ' ../pages/login-page' ;
387
+
388
+ test (' Should login via the login button' , async ({ page }) => {
389
+ const loginPage = new LoginPage (page );
390
+ await loginPage .clickLoginButton ();
391
+ });
392
+ ```
393
+
394
+ #### Why this matters
395
+
396
+ - Context-aware tracking — no need to manually pass the tracker around.
397
+ - Scenario binding — each interaction is tied to the test that triggered it.
398
+ - TMS integration — easily associate scenarios with external test cases or tickets.
399
+ - Parallel-safe — works reliably even with concurrent test execution.
400
+
300
401
### Coverage Report Generation
301
402
302
403
After every call to ` await tracker.trackCoverage(...) ` , the tool automatically stores coverage data in
0 commit comments