Skip to content

Commit 42a975b

Browse files
committed
fix(Playwright): Handle selector registration in worker processes
- Wrap __value and __disabled selector registration in try-catch - Handle 'already registered' errors gracefully - Add page/context validation before storage cleanup - Skip storage cleanup if no active page/context exists These changes prevent errors when running tests in worker processes where Playwright's selector registration state is shared globally across workers.
1 parent dd909f1 commit 42a975b

File tree

1 file changed

+41
-4
lines changed

1 file changed

+41
-4
lines changed

lib/helper/Playwright.js

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -506,11 +506,30 @@ class Playwright extends Helper {
506506

507507
// register an internal selector engine for reading value property of elements in a selector
508508
try {
509-
if (!defaultSelectorEnginesInitialized) {
509+
// Always wrap in try-catch since selectors might be registered globally across workers
510+
try {
510511
await playwright.selectors.register('__value', createValueEngine)
511-
await playwright.selectors.register('__disabled', createDisabledEngine)
512-
if (process.env.testIdAttribute) await playwright.selectors.setTestIdAttribute(process.env.testIdAttribute)
513512
defaultSelectorEnginesInitialized = true
513+
} catch (e) {
514+
if (!e.message.includes('already registered')) {
515+
throw e
516+
}
517+
// Selector already registered globally, mark as initialized
518+
defaultSelectorEnginesInitialized = true
519+
}
520+
try {
521+
await playwright.selectors.register('__disabled', createDisabledEngine)
522+
} catch (e) {
523+
if (!e.message.includes('already registered')) {
524+
throw e
525+
}
526+
}
527+
if (process.env.testIdAttribute) {
528+
try {
529+
await playwright.selectors.setTestIdAttribute(process.env.testIdAttribute)
530+
} catch (e) {
531+
// Ignore if already set
532+
}
514533
}
515534

516535
// Register all custom locator strategies from the global registry
@@ -651,7 +670,20 @@ class Playwright extends Helper {
651670
}
652671
}
653672
this.debugSection('New Session', JSON.stringify(this.contextOptions))
654-
this.browserContext = await this.browser.newContext(this.contextOptions) // Adding the HTTPSError ignore in the context so that we can ignore those errors
673+
try {
674+
this.browserContext = await this.browser.newContext(this.contextOptions) // Adding the HTTPSError ignore in the context so that we can ignore those errors
675+
} catch (e) {
676+
// In worker processes, selectors might already be registered globally
677+
// causing newContext to fail. Try again or use the error
678+
if (e.message && e.message.includes('already registered')) {
679+
// Selectors already registered, try creating context anyway
680+
// This happens in worker processes where Playwright state is shared
681+
this.debugSection('Session', 'Selectors already registered, retrying context creation')
682+
this.browserContext = await this.browser.newContext(this.contextOptions)
683+
} else {
684+
throw e
685+
}
686+
}
655687
}
656688
}
657689

@@ -4775,6 +4807,11 @@ async function refreshContextSession() {
47754807
}
47764808

47774809
try {
4810+
if (!this.page || !this.browserContext) {
4811+
this.debugSection('Session', 'Skipping storage cleanup - no active page/context')
4812+
return
4813+
}
4814+
47784815
const currentUrl = await this.grabCurrentUrl()
47794816

47804817
if (currentUrl.startsWith('http')) {

0 commit comments

Comments
 (0)