Skip to content

Commit 1a51420

Browse files
cpAdmpavelfeldman
andauthored
feat: Expose types and documentation for Locator.toString (#38633)
Signed-off-by: Chris <57954026+cpAdm@users.noreply.github.com> Co-authored-by: Pavel Feldman <pavel.feldman@gmail.com>
1 parent 1eba405 commit 1a51420

File tree

7 files changed

+50
-16
lines changed

7 files changed

+50
-16
lines changed

docs/src/api/class-locator.md

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -628,20 +628,13 @@ Locator description.
628628

629629
## method: Locator.description
630630
* since: v1.57
631+
* langs: python, java, csharp
631632
- returns: <[null]|[string]>
632633

633-
Returns locator description previously set with [`method: Locator.describe`]. Returns `null` if no custom description has been set. Prefer `Locator.toString()` for a human-readable representation, as it uses the description when available.
634+
Returns locator description previously set with [`method: Locator.describe`]. Returns `null` if no custom description has been set.
634635

635636
**Usage**
636637

637-
```js
638-
const button = page.getByRole('button').describe('Subscribe button');
639-
console.log(button.description()); // "Subscribe button"
640-
641-
const input = page.getByRole('textbox');
642-
console.log(input.description()); // null
643-
```
644-
645638
```python async
646639
button = page.get_by_role("button").describe("Subscribe button")
647640
print(button.description()) # "Subscribe button"
@@ -674,6 +667,23 @@ var input = Page.GetByRole(AriaRole.Textbox);
674667
Console.WriteLine(input.Description()); // null
675668
```
676669

670+
## method: Locator.description
671+
* since: v1.57
672+
* langs: js
673+
- returns: <[null]|[string]>
674+
675+
Returns locator description previously set with [`method: Locator.describe`]. Returns `null` if no custom description has been set. Prefer [`method: Locator.toString`] for a human-readable representation, as it uses the description when available.
676+
677+
**Usage**
678+
679+
```js
680+
const button = page.getByRole('button').describe('Subscribe button');
681+
console.log(button.description()); // "Subscribe button"
682+
683+
const input = page.getByRole('textbox');
684+
console.log(input.description()); // null
685+
```
686+
677687
## async method: Locator.dispatchEvent
678688
* since: v1.14
679689

@@ -2534,6 +2544,13 @@ If you need to assert text on the page, prefer [`method: LocatorAssertions.toHav
25342544
### option: Locator.textContent.timeout = %%-input-timeout-js-%%
25352545
* since: v1.14
25362546

2547+
## method: Locator.toString
2548+
* since: v1.57
2549+
* langs: js
2550+
- returns: <[string]>
2551+
2552+
Returns a human-readable representation of the locator, using the [`method: Locator.description`] if one exists; otherwise, it generates a string based on the locator's selector.
2553+
25372554
## async method: Locator.type
25382555
* since: v1.14
25392556
* deprecated: In most cases, you should use [`method: Locator.fill`] instead. You only need to press keys one by one if there is special keyboard handling on the page - in this case use [`method: Locator.pressSequentially`].

docs/src/release-notes-js.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ After 3 years of being deprecated, we removed `page.accessibility` from our API.
6969
7070
- New property [`property: TestConfig.tag`] adds a tag to all tests in this run. This is useful when using [merge-reports](./test-sharding.md#merging-reports-from-multiple-shards).
7171
- [`event: Worker.console`] event is emitted when JavaScript within the worker calls one of console API methods, e.g. console.log or console.dir. [`method: Worker.waitForEvent`] can be used to wait for it.
72-
- [`method: Locator.description`] returns locator description previously set with [`method: Locator.describe`], and `Locator.toString()` now uses the description when available.
72+
- [`method: Locator.description`] returns locator description previously set with [`method: Locator.describe`], and [`method: Locator.toString`] now uses the description when available.
7373
- New option [`option: Locator.click.steps`] in [`method: Locator.click`] and [`method: Locator.dragTo`] that configures the number of `mousemove` events emitted while moving the mouse pointer to the target element.
7474
- Network requests issued by [Service Workers](./service-workers.md#network-events-and-routing) are now reported and can be routed through the [BrowserContext](./api/class-browsercontext.md), only in Chromium. You can opt out using the `PLAYWRIGHT_DISABLE_SERVICE_WORKER_NETWORK` environment variable.
7575
- Console messages from Service Workers are dispatched through [`event: Worker.console`]. You can opt out of this using the `PLAYWRIGHT_DISABLE_SERVICE_WORKER_CONSOLE` environment variable.

packages/playwright-client/types/types.d.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12644,6 +12644,12 @@ export interface Locator {
1264412644
elementHandle(options?: {
1264512645
timeout?: number;
1264612646
}): Promise<null|ElementHandle<SVGElement | HTMLElement>>;
12647+
/**
12648+
* Returns a human-readable representation of the locator, using the
12649+
* [locator.description()](https://playwright.dev/docs/api/class-locator#locator-description) if one exists;
12650+
* otherwise, it generates a string based on the locator's selector.
12651+
*/
12652+
toString(): string;
1264712653
/**
1264812654
* When the locator points to a list of elements, this returns an array of locators, pointing to their respective
1264912655
* elements.
@@ -13198,8 +13204,9 @@ export interface Locator {
1319813204
/**
1319913205
* Returns locator description previously set with
1320013206
* [locator.describe(description)](https://playwright.dev/docs/api/class-locator#locator-describe). Returns `null` if
13201-
* no custom description has been set. Prefer `Locator.toString()` for a human-readable representation, as it uses the
13202-
* description when available.
13207+
* no custom description has been set. Prefer
13208+
* [locator.toString()](https://playwright.dev/docs/api/class-locator#locator-to-string) for a human-readable
13209+
* representation, as it uses the description when available.
1320313210
*
1320413211
* **Usage**
1320513212
*

packages/playwright-core/src/utils/isomorphic/locatorGenerators.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export interface LocatorFactory {
3737
chainLocators(locators: string[]): string;
3838
}
3939

40-
export function asLocatorDescription(lang: Language, selector: string): string | undefined {
40+
export function asLocatorDescription(lang: Language, selector: string): string {
4141
try {
4242
const parsed = parseSelector(selector);
4343
const customDescription = parseCustomDescription(parsed);

packages/playwright-core/types/types.d.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12644,6 +12644,12 @@ export interface Locator {
1264412644
elementHandle(options?: {
1264512645
timeout?: number;
1264612646
}): Promise<null|ElementHandle<SVGElement | HTMLElement>>;
12647+
/**
12648+
* Returns a human-readable representation of the locator, using the
12649+
* [locator.description()](https://playwright.dev/docs/api/class-locator#locator-description) if one exists;
12650+
* otherwise, it generates a string based on the locator's selector.
12651+
*/
12652+
toString(): string;
1264712653
/**
1264812654
* When the locator points to a list of elements, this returns an array of locators, pointing to their respective
1264912655
* elements.
@@ -13198,8 +13204,9 @@ export interface Locator {
1319813204
/**
1319913205
* Returns locator description previously set with
1320013206
* [locator.describe(description)](https://playwright.dev/docs/api/class-locator#locator-describe). Returns `null` if
13201-
* no custom description has been set. Prefer `Locator.toString()` for a human-readable representation, as it uses the
13202-
* description when available.
13207+
* no custom description has been set. Prefer
13208+
* [locator.toString()](https://playwright.dev/docs/api/class-locator#locator-to-string) for a human-readable
13209+
* representation, as it uses the description when available.
1320313210
*
1320413211
* **Usage**
1320513212
*

utils/doclint/missingDocs.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,9 @@ function listMethods(rootNames, apiFileName) {
118118
* @param {string} methodName
119119
*/
120120
function shouldSkipMethodByName(className, methodName) {
121-
if (methodName.startsWith('_') || methodName === 'T' || methodName === 'toString')
121+
if (methodName.startsWith('_') || methodName === 'T')
122+
return true;
123+
if (methodName === 'toString' && className !== 'Locator')
122124
return true;
123125
if (EventEmitter.prototype.hasOwnProperty(methodName))
124126
return true;

utils/generate_types/overrides.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,7 @@ export interface Locator {
200200
elementHandle(options?: {
201201
timeout?: number;
202202
}): Promise<null|ElementHandle<SVGElement | HTMLElement>>;
203+
toString(): string;
203204
}
204205

205206
export interface BrowserType<Unused = {}> {

0 commit comments

Comments
 (0)