Skip to content

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

feat(api): expose asLocatorDescription via LocalUtils for language ports (#43188)

Pavel Feldman16a8826a800c
diff --git a/packages/isomorphic/protocolMetainfo.ts b/packages/isomorphic/protocolMetainfo.ts
index 6869dcb458..e8b0ae8c32 100644
--- a/packages/isomorphic/protocolMetainfo.ts
+++ b/packages/isomorphic/protocolMetainfo.ts
@@ -235,6 +235,7 @@ export const methodMetainfo = new Map<string, MethodMetainfo>([
['LocalUtils.addStackToTracingNoReply', { internal: true, }],
['LocalUtils.traceDiscarded', { internal: true, }],
['LocalUtils.globToRegex', { internal: true, }],
+ ['LocalUtils.asLocatorDescription', { internal: true, }],
['Request.response', { internal: true, }],
['Request.rawRequestHeaders', { internal: true, }],
['Route.redirectNavigationRequest', { internal: true, }],
diff --git a/packages/playwright-core/src/client/channels.d.ts b/packages/playwright-core/src/client/channels.d.ts
index 2b95064f65..dd93e916e0 100644
--- a/packages/playwright-core/src/client/channels.d.ts
+++ b/packages/playwright-core/src/client/channels.d.ts
@@ -3556,6 +3556,7 @@ export interface LocalUtilsChannel extends LocalUtilsEventTarget, Channel {
addStackToTracingNoReply(params: LocalUtilsAddStackToTracingNoReplyParams, options: TimeoutOptions): Promise<LocalUtilsAddStackToTracingNoReplyResult>;
traceDiscarded(params: LocalUtilsTraceDiscardedParams, options: TimeoutOptions): Promise<LocalUtilsTraceDiscardedResult>;
globToRegex(params: LocalUtilsGlobToRegexParams, options: TimeoutOptions): Promise<LocalUtilsGlobToRegexResult>;
+ asLocatorDescription(params: LocalUtilsAsLocatorDescriptionParams, options: TimeoutOptions): Promise<LocalUtilsAsLocatorDescriptionResult>;
}
export type LocalUtilsZipParams = {
zipFile: string,
@@ -3670,6 +3671,16 @@ export type LocalUtilsGlobToRegexOptions = {
export type LocalUtilsGlobToRegexResult = {
regex: string,
};
+export type LocalUtilsAsLocatorDescriptionParams = {
+ selector: string,
+ sdkLanguage: SDKLanguage,
+};
+export type LocalUtilsAsLocatorDescriptionOptions = {
+
+};
+export type LocalUtilsAsLocatorDescriptionResult = {
+ description: string,
+};
export interface LocalUtilsEvents {
}
diff --git a/packages/playwright-core/src/server/channels.d.ts b/packages/playwright-core/src/server/channels.d.ts
index 6e5a31d79e..8e61916cde 100644
--- a/packages/playwright-core/src/server/channels.d.ts
+++ b/packages/playwright-core/src/server/channels.d.ts
@@ -3557,6 +3557,7 @@ export interface LocalUtilsChannel extends LocalUtilsEventTarget, Channel {
addStackToTracingNoReply(params: LocalUtilsAddStackToTracingNoReplyParams, progress: Progress): Promise<LocalUtilsAddStackToTracingNoReplyResult>;
traceDiscarded(params: LocalUtilsTraceDiscardedParams, progress: Progress): Promise<LocalUtilsTraceDiscardedResult>;
globToRegex(params: LocalUtilsGlobToRegexParams, progress: Progress): Promise<LocalUtilsGlobToRegexResult>;
+ asLocatorDescription(params: LocalUtilsAsLocatorDescriptionParams, progress: Progress): Promise<LocalUtilsAsLocatorDescriptionResult>;
}
export type LocalUtilsZipParams = {
zipFile: string,
@@ -3671,6 +3672,16 @@ export type LocalUtilsGlobToRegexOptions = {
export type LocalUtilsGlobToRegexResult = {
regex: string,
};
+export type LocalUtilsAsLocatorDescriptionParams = {
+ selector: string,
+ sdkLanguage: SDKLanguage,
+};
+export type LocalUtilsAsLocatorDescriptionOptions = {
+
+};
+export type LocalUtilsAsLocatorDescriptionResult = {
+ description: string,
+};
export interface LocalUtilsEvents {
}
diff --git a/packages/playwright-core/src/server/dispatchers/localUtilsDispatcher.ts b/packages/playwright-core/src/server/dispatchers/localUtilsDispatcher.ts
index 8051658f48..64fd7d1a36 100644
--- a/packages/playwright-core/src/server/dispatchers/localUtilsDispatcher.ts
+++ b/packages/playwright-core/src/server/dispatchers/localUtilsDispatcher.ts
@@ -16,6 +16,7 @@
import net from 'net';
import { resolveGlobToRegexPattern } from '@isomorphic/urlMatch';
+import { asLocatorDescription } from '@isomorphic/locatorGenerators';
import { deviceDescriptors as descriptors } from '@isomorphic/deviceDescriptors';
import { fetchData } from '../utils';
import { getUserAgent } from '../userAgent';
@@ -160,6 +161,11 @@ export class LocalUtilsDispatcher extends Dispatcher<SdkObject, channels.LocalUt
const regex = resolveGlobToRegexPattern(params.baseURL, params.glob, params.webSocketUrl);
return { regex };
}
+
+ async asLocatorDescription(params: channels.LocalUtilsAsLocatorDescriptionParams, progress: Progress): Promise<channels.LocalUtilsAsLocatorDescriptionResult> {
+ const description = asLocatorDescription(params.sdkLanguage, params.selector);
+ return { description };
+ }
}
async function urlToWSEndpoint(progress: Progress, endpointURL: string): Promise<string> {
diff --git a/packages/protocol/spec/localUtils.yml b/packages/protocol/spec/localUtils.yml
index 6d5eb8a8a4..8228d6633e 100644
--- a/packages/protocol/spec/localUtils.yml
+++ b/packages/protocol/spec/localUtils.yml
@@ -154,3 +154,11 @@ LocalUtils:
webSocketUrl: boolean?
returns:
regex: string
+
+ asLocatorDescription:
+ internal: true
+ parameters:
+ selector: string
+ sdkLanguage: SDKLanguage
+ returns:
+ description: string
diff --git a/packages/protocol/src/validator.ts b/packages/protocol/src/validator.ts
index a383a1f995..4b7dbdbbf3 100644
--- a/packages/protocol/src/validator.ts
+++ b/packages/protocol/src/validator.ts
@@ -2080,6 +2080,13 @@ scheme.LocalUtilsGlobToRegexParams = tObject({
scheme.LocalUtilsGlobToRegexResult = tObject({
regex: tString,
});
+scheme.LocalUtilsAsLocatorDescriptionParams = tObject({
+ selector: tString,
+ sdkLanguage: tType('SDKLanguage'),
+});
+scheme.LocalUtilsAsLocatorDescriptionResult = tObject({
+ description: tString,
+});
scheme.HttpCredentials = tObject({
username: tString,
password: tString,
diff --git a/tests/library/locator-generator.spec.ts b/tests/library/locator-generator.spec.ts
index 507b902343..31b9fa89d0 100644
--- a/tests/library/locator-generator.spec.ts
+++ b/tests/library/locator-generator.spec.ts
@@ -928,3 +928,15 @@ it('resolveSelector returns locatorCode for sdkLanguage', async ({ page }) => {
expect((await channel.resolveSelector({ selector, sdkLanguage: 'java' })).locatorCode).toBe(`getByRole(AriaRole.BUTTON, new Page.GetByRoleOptions().setName("Submit"))`);
expect((await channel.resolveSelector({ selector, sdkLanguage: 'csharp' })).locatorCode).toBe(`GetByRole(AriaRole.Button, new() { Name = "Submit" })`);
});
+
+it('asLocatorDescription via LocalUtils', async ({ page }) => {
+ const channel = (page as any)._connection.localUtils()._channel;
+ const selector = `internal:role=button[name="Submit"i]`;
+ expect((await channel.asLocatorDescription({ selector, sdkLanguage: 'javascript' })).description).toBe(`getByRole('button', { name: 'Submit' })`);
+ expect((await channel.asLocatorDescription({ selector, sdkLanguage: 'python' })).description).toBe(`get_by_role("button", name="Submit")`);
+ expect((await channel.asLocatorDescription({ selector, sdkLanguage: 'java' })).description).toBe(`getByRole(AriaRole.BUTTON, new Page.GetByRoleOptions().setName("Submit"))`);
+ expect((await channel.asLocatorDescription({ selector, sdkLanguage: 'csharp' })).description).toBe(`GetByRole(AriaRole.Button, new() { Name = "Submit" })`);
+
+ const described = await channel.asLocatorDescription({ selector: `#submit >> internal:describe="Submit button"`, sdkLanguage: 'python' });
+ expect(described.description).toBe(`Submit button`);
+});