Skip to content

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.

chore: speed up strict mode violation suggestions (#37357)

Dmitry Gozman4b890fc063a4
diff --git a/packages/injected/src/injectedScript.ts b/packages/injected/src/injectedScript.ts
index dde268ee61..76969f4ed6 100644
--- a/packages/injected/src/injectedScript.ts
+++ b/packages/injected/src/injectedScript.ts
@@ -20,12 +20,12 @@ import { parseAttributeSelector, parseSelector, stringifySelector, visitAllSelec
import { cacheNormalizedWhitespaces, normalizeWhiteSpace, trimStringWithEllipsis } from '@isomorphic/stringUtils';
import { generateAriaTree, getAllElementsMatchingExpectAriaTemplate, matchesExpectAriaTemplate, renderAriaTree } from './ariaSnapshot';
-import { enclosingShadowRootOrDocument, isElementVisible, isInsideScope, parentElementOrShadowHost, setGlobalOptions } from './domUtils';
+import { beginDOMCaches, enclosingShadowRootOrDocument, endDOMCaches, isElementVisible, isInsideScope, parentElementOrShadowHost, setGlobalOptions } from './domUtils';
import { Highlight } from './highlight';
import { kLayoutSelectorNames, layoutSelectorScore } from './layoutSelectorUtils';
import { createReactEngine } from './reactSelectorEngine';
import { createRoleEngine } from './roleSelectorEngine';
-import { getAriaDisabled, getAriaRole, getCheckedAllowMixed, getCheckedWithoutMixed, getElementAccessibleDescription, getElementAccessibleErrorMessage, getElementAccessibleName, getReadonly } from './roleUtils';
+import { beginAriaCaches, endAriaCaches, getAriaDisabled, getAriaRole, getCheckedAllowMixed, getCheckedWithoutMixed, getElementAccessibleDescription, getElementAccessibleErrorMessage, getElementAccessibleName, getReadonly } from './roleUtils';
import { SelectorEvaluatorImpl, sortInDOMOrder } from './selectorEvaluator';
import { generateSelector } from './selectorGenerator';
import { elementMatchesText, elementText, getElementLabels } from './selectorUtils';
@@ -72,6 +72,7 @@ export type InjectedScriptOptions = {
testIdAttributeName: string;
stableRafCount: number;
browserName: string;
+ isUtilityWorld?: boolean;
customEngines: { name: string, source: string }[];
};
@@ -80,6 +81,7 @@ export class InjectedScript {
readonly _evaluator: SelectorEvaluatorImpl;
private _stableRafCount: number;
private _browserName: string;
+ private _isUtilityWorld: boolean;
readonly onGlobalListenersRemoved: Set<() => void>;
private _hitTargetInterceptor: undefined | ((event: MouseEvent | PointerEvent | TouchEvent) => void);
private _highlight: Highlight | undefined;
@@ -233,6 +235,7 @@ export class InjectedScript {
this._stableRafCount = options.stableRafCount;
this._browserName = options.browserName;
+ this._isUtilityWorld = !!options.isUtilityWorld;
setGlobalOptions({ browserNameForWorkarounds: options.browserName });
this._setupGlobalListenersRemovalDetection();
@@ -1210,14 +1213,25 @@ export class InjectedScript {
}
strictModeViolationError(selector: ParsedSelector, matches: Element[]): Error {
- const infos = matches.slice(0, 10).map(m => ({
- preview: this.previewNode(m),
- selector: this.generateSelectorSimple(m),
- }));
- const lines = infos.map((info, i) => `\n ${i + 1}) ${info.preview} aka ${asLocator(this._sdkLanguage, info.selector)}`);
- if (infos.length < matches.length)
- lines.push('\n ...');
- return this.createStacklessError(`strict mode violation: ${asLocator(this._sdkLanguage, stringifySelector(selector))} resolved to ${matches.length} elements:${lines.join('')}\n`);
+ this._evaluator.begin();
+ beginAriaCaches();
+ beginDOMCaches();
+ try {
+ // Firefox is slow to access DOM bindings in the utility world, making it very expensive to generate a lot of selectors.
+ const maxElements = this._isUtilityWorld && this._browserName === 'firefox' ? 2 : 10;
+ const infos = matches.slice(0, maxElements).map(m => ({
+ preview: this.previewNode(m),
+ selector: this.generateSelectorSimple(m),
+ }));
+ const lines = infos.map((info, i) => `\n ${i + 1}) ${info.preview} aka ${asLocator(this._sdkLanguage, info.selector)}`);
+ if (infos.length < matches.length)
+ lines.push('\n ...');
+ return this.createStacklessError(`strict mode violation: ${asLocator(this._sdkLanguage, stringifySelector(selector))} resolved to ${matches.length} elements:${lines.join('')}\n`);
+ } finally {
+ endDOMCaches();
+ endAriaCaches();
+ this._evaluator.end();
+ }
}
createStacklessError(message: string): Error {
diff --git a/packages/playwright-core/src/server/dom.ts b/packages/playwright-core/src/server/dom.ts
index 8aa3a6e864..d5a06c5c9a 100644
--- a/packages/playwright-core/src/server/dom.ts
+++ b/packages/playwright-core/src/server/dom.ts
@@ -91,6 +91,7 @@ export class FrameExecutionContext extends js.ExecutionContext {
testIdAttributeName: selectorsRegistry.testIdAttributeName(),
stableRafCount: this.frame._page.delegate.rafCountForStablePosition(),
browserName: this.frame._page.browserContext._browser.options.name,
+ isUtilityWorld: this.world === 'utility',
customEngines,
};
const source = `