Skip to content

Commit 30946ae

Browse files
authored
docs: render links taking alias into account (microsoft#433)
1 parent 473ba26 commit 30946ae

File tree

3 files changed

+51
-44
lines changed

3 files changed

+51
-44
lines changed

playwright/async_api/_generated.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -693,7 +693,7 @@ async def wait_for_event(
693693
) -> typing.Any:
694694
"""WebSocket.wait_for_event
695695
696-
> NOTE: In most cases, you should use `web_socket.wait_for_event()`.
696+
> NOTE: In most cases, you should use `web_socket.expect_event()`.
697697
698698
Waits for given `event` to fire. If predicate is provided, it passes event's value into the `predicate` function and
699699
waits for `predicate(event)` to return a truthy value. Will throw an error if the socket is closed before the `event` is
@@ -5494,7 +5494,7 @@ def set_default_navigation_timeout(self, timeout: float) -> NoneType:
54945494
- `page.goto()`
54955495
- `page.reload()`
54965496
- `page.set_content()`
5497-
- `page.wait_for_navigation()`
5497+
- `page.expect_navigation()`
54985498
54995499
> NOTE: `page.set_default_navigation_timeout()` takes priority over `page.set_default_timeout()`,
55005500
`browser_context.set_default_timeout()` and `browser_context.set_default_navigation_timeout()`.
@@ -5548,7 +5548,7 @@ async def query_selector(
55485548
The method finds an element matching the specified selector within the page. If no elements match the selector, the
55495549
return value resolves to `null`.
55505550
5551-
Shortcut for main frame's `frame.$()`.
5551+
Shortcut for main frame's `frame.query_selector()`.
55525552
55535553
Parameters
55545554
----------
@@ -5577,7 +5577,7 @@ async def query_selector_all(self, selector: str) -> typing.List["ElementHandle"
55775577
The method finds all elements matching the specified selector within the page. If no elements match the selector, the
55785578
return value resolves to `[]`.
55795579
5580-
Shortcut for main frame's `frame.$$()`.
5580+
Shortcut for main frame's `frame.query_selector_all()`.
55815581
55825582
Parameters
55835583
----------
@@ -6065,7 +6065,7 @@ async def eval_on_selector(
60656065
The method finds an element matching the specified selector within the page and passes it as a first argument to
60666066
`pageFunction`. If no elements match the selector, the method throws an error. Returns the value of `pageFunction`.
60676067
6068-
If `pageFunction` returns a [Promise], then `page.$eval()` would wait for the promise to resolve and return its
6068+
If `pageFunction` returns a [Promise], then `page.eval_on_selector()` would wait for the promise to resolve and return its
60696069
value.
60706070
60716071
Examples:
@@ -6076,7 +6076,7 @@ async def eval_on_selector(
60766076
html = await page.eval_on_selector(\".main-container\", \"(e, suffix) => e.outer_html + suffix\", \"hello\")
60776077
```
60786078
6079-
Shortcut for main frame's `frame.$eval()`.
6079+
Shortcut for main frame's `frame.eval_on_selector()`.
60806080
60816081
Parameters
60826082
----------
@@ -6124,7 +6124,7 @@ async def eval_on_selector_all(
61246124
The method finds all elements matching the specified selector within the page and passes an array of matched elements as
61256125
a first argument to `pageFunction`. Returns the result of `pageFunction` invocation.
61266126
6127-
If `pageFunction` returns a [Promise], then `page.$$eval()` would wait for the promise to resolve and return its
6127+
If `pageFunction` returns a [Promise], then `page.eval_on_selector_all()` would wait for the promise to resolve and return its
61286128
value.
61296129
61306130
Examples:
@@ -6649,7 +6649,7 @@ async def wait_for_event(
66496649
) -> typing.Any:
66506650
"""Page.wait_for_event
66516651
6652-
> NOTE: In most cases, you should use `page.wait_for_event()`.
6652+
> NOTE: In most cases, you should use `page.expect_event()`.
66536653
66546654
Waits for given `event` to fire. If predicate is provided, it passes event's value into the `predicate` function and
66556655
waits for `predicate(event)` to return a truthy value. Will throw an error if the socket is closed before the `event` is
@@ -7100,7 +7100,7 @@ async def close(self, run_before_unload: bool = None) -> NoneType:
71007100
By default, `page.close()` **does not** run `beforeunload` handlers.
71017101
71027102
> NOTE: if `runBeforeUnload` is passed as true, a `beforeunload` dialog might be summoned and should be handled manually
7103-
via [`event: Page.dialog`] event.
7103+
via `page.on('dialog')` event.
71047104
71057105
Parameters
71067106
----------
@@ -8396,7 +8396,7 @@ def expect_navigation(
83968396
> NOTE: Usage of the [History API](https://developer.mozilla.org/en-US/docs/Web/API/History_API) to change the URL is
83978397
considered a navigation.
83988398
8399-
Shortcut for main frame's `frame.wait_for_navigation()`.
8399+
Shortcut for main frame's `frame.expect_navigation()`.
84008400
84018401
Parameters
84028402
----------
@@ -8602,7 +8602,7 @@ def set_default_navigation_timeout(self, timeout: float) -> NoneType:
86028602
- `page.goto()`
86038603
- `page.reload()`
86048604
- `page.set_content()`
8605-
- `page.wait_for_navigation()`
8605+
- `page.expect_navigation()`
86068606
86078607
> NOTE: `page.set_default_navigation_timeout()` and `page.set_default_timeout()` take priority over
86088608
`browser_context.set_default_navigation_timeout()`.
@@ -9251,7 +9251,7 @@ async def wait_for_event(
92519251
) -> typing.Any:
92529252
"""BrowserContext.wait_for_event
92539253
9254-
> NOTE: In most cases, you should use `browser_context.wait_for_event()`.
9254+
> NOTE: In most cases, you should use `browser_context.expect_event()`.
92559255
92569256
Waits for given `event` to fire. If predicate is provided, it passes event's value into the `predicate` function and
92579257
waits for `predicate(event)` to return a truthy value. Will throw an error if the socket is closed before the `event` is

playwright/sync_api/_generated.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -697,7 +697,7 @@ def wait_for_event(
697697
) -> typing.Any:
698698
"""WebSocket.wait_for_event
699699
700-
> NOTE: In most cases, you should use `web_socket.wait_for_event()`.
700+
> NOTE: In most cases, you should use `web_socket.expect_event()`.
701701
702702
Waits for given `event` to fire. If predicate is provided, it passes event's value into the `predicate` function and
703703
waits for `predicate(event)` to return a truthy value. Will throw an error if the socket is closed before the `event` is
@@ -5629,7 +5629,7 @@ def set_default_navigation_timeout(self, timeout: float) -> NoneType:
56295629
- `page.goto()`
56305630
- `page.reload()`
56315631
- `page.set_content()`
5632-
- `page.wait_for_navigation()`
5632+
- `page.expect_navigation()`
56335633
56345634
> NOTE: `page.set_default_navigation_timeout()` takes priority over `page.set_default_timeout()`,
56355635
`browser_context.set_default_timeout()` and `browser_context.set_default_navigation_timeout()`.
@@ -5681,7 +5681,7 @@ def query_selector(self, selector: str) -> typing.Union["ElementHandle", NoneTyp
56815681
The method finds an element matching the specified selector within the page. If no elements match the selector, the
56825682
return value resolves to `null`.
56835683
5684-
Shortcut for main frame's `frame.$()`.
5684+
Shortcut for main frame's `frame.query_selector()`.
56855685
56865686
Parameters
56875687
----------
@@ -5710,7 +5710,7 @@ def query_selector_all(self, selector: str) -> typing.List["ElementHandle"]:
57105710
The method finds all elements matching the specified selector within the page. If no elements match the selector, the
57115711
return value resolves to `[]`.
57125712
5713-
Shortcut for main frame's `frame.$$()`.
5713+
Shortcut for main frame's `frame.query_selector_all()`.
57145714
57155715
Parameters
57165716
----------
@@ -6212,7 +6212,7 @@ def eval_on_selector(
62126212
The method finds an element matching the specified selector within the page and passes it as a first argument to
62136213
`pageFunction`. If no elements match the selector, the method throws an error. Returns the value of `pageFunction`.
62146214
6215-
If `pageFunction` returns a [Promise], then `page.$eval()` would wait for the promise to resolve and return its
6215+
If `pageFunction` returns a [Promise], then `page.eval_on_selector()` would wait for the promise to resolve and return its
62166216
value.
62176217
62186218
Examples:
@@ -6223,7 +6223,7 @@ def eval_on_selector(
62236223
html = page.eval_on_selector(\".main-container\", \"(e, suffix) => e.outer_html + suffix\", \"hello\")
62246224
```
62256225
6226-
Shortcut for main frame's `frame.$eval()`.
6226+
Shortcut for main frame's `frame.eval_on_selector()`.
62276227
62286228
Parameters
62296229
----------
@@ -6273,7 +6273,7 @@ def eval_on_selector_all(
62736273
The method finds all elements matching the specified selector within the page and passes an array of matched elements as
62746274
a first argument to `pageFunction`. Returns the result of `pageFunction` invocation.
62756275
6276-
If `pageFunction` returns a [Promise], then `page.$$eval()` would wait for the promise to resolve and return its
6276+
If `pageFunction` returns a [Promise], then `page.eval_on_selector_all()` would wait for the promise to resolve and return its
62776277
value.
62786278
62796279
Examples:
@@ -6810,7 +6810,7 @@ def wait_for_event(
68106810
) -> typing.Any:
68116811
"""Page.wait_for_event
68126812
6813-
> NOTE: In most cases, you should use `page.wait_for_event()`.
6813+
> NOTE: In most cases, you should use `page.expect_event()`.
68146814
68156815
Waits for given `event` to fire. If predicate is provided, it passes event's value into the `predicate` function and
68166816
waits for `predicate(event)` to return a truthy value. Will throw an error if the socket is closed before the `event` is
@@ -7274,7 +7274,7 @@ def close(self, run_before_unload: bool = None) -> NoneType:
72747274
By default, `page.close()` **does not** run `beforeunload` handlers.
72757275
72767276
> NOTE: if `runBeforeUnload` is passed as true, a `beforeunload` dialog might be summoned and should be handled manually
7277-
via [`event: Page.dialog`] event.
7277+
via `page.on('dialog')` event.
72787278
72797279
Parameters
72807280
----------
@@ -8601,7 +8601,7 @@ def expect_navigation(
86018601
> NOTE: Usage of the [History API](https://developer.mozilla.org/en-US/docs/Web/API/History_API) to change the URL is
86028602
considered a navigation.
86038603
8604-
Shortcut for main frame's `frame.wait_for_navigation()`.
8604+
Shortcut for main frame's `frame.expect_navigation()`.
86058605
86068606
Parameters
86078607
----------
@@ -8807,7 +8807,7 @@ def set_default_navigation_timeout(self, timeout: float) -> NoneType:
88078807
- `page.goto()`
88088808
- `page.reload()`
88098809
- `page.set_content()`
8810-
- `page.wait_for_navigation()`
8810+
- `page.expect_navigation()`
88118811
88128812
> NOTE: `page.set_default_navigation_timeout()` and `page.set_default_timeout()` take priority over
88138813
`browser_context.set_default_navigation_timeout()`.
@@ -9470,7 +9470,7 @@ def wait_for_event(
94709470
) -> typing.Any:
94719471
"""BrowserContext.wait_for_event
94729472
9473-
> NOTE: In most cases, you should use `browser_context.wait_for_event()`.
9473+
> NOTE: In most cases, you should use `browser_context.expect_event()`.
94749474
94759475
Waits for given `event` to fire. If predicate is provided, it passes event's value into the `predicate` function and
94769476
waits for `predicate(event)` to return a truthy value. Will throw an error if the socket is closed before the `event` is

scripts/documentation_provider.py

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ class DocumentationProvider:
3737
def __init__(self, is_async: bool) -> None:
3838
self.is_async = is_async
3939
self.api: Any = {}
40+
self.links: Dict[str, str] = {}
4041
self.printed_entries: List[str] = []
4142
process_output = subprocess.run(
4243
["python", "-m", "playwright", "print-api-json"],
@@ -53,19 +54,23 @@ def _patch_case(self) -> None:
5354
members = {}
5455
self.classes[clazz["name"]] = clazz
5556
for member in clazz["members"]:
56-
if member["kind"] == "event":
57-
continue
58-
method_name = member["name"]
59-
new_name = to_snake_case(method_name)
57+
member_name = member["name"]
6058
alias = (
6159
member["langs"].get("aliases").get("python")
6260
if member["langs"].get("aliases")
6361
else None
6462
)
63+
new_name = member_name
6564
if alias:
6665
new_name = alias
67-
members[new_name] = member
66+
self._add_link(member["kind"], clazz["name"], member_name, new_name)
67+
68+
if member["kind"] == "event":
69+
continue
70+
71+
new_name = to_snake_case(new_name)
6872
member["name"] = new_name
73+
members[new_name] = member
6974
if member["langs"].get("types") and member["langs"]["types"].get(
7075
"python"
7176
):
@@ -90,6 +95,21 @@ def _patch_case(self) -> None:
9095

9196
clazz["members"] = members
9297

98+
def _add_link(self, kind: str, clazz: str, member: str, alias: str) -> None:
99+
match = re.match(r"(JS|CDP|[A-Z])([^.]+)", clazz)
100+
if not match:
101+
raise Exception("Invalid class " + clazz)
102+
var_name = to_snake_case(f"{match.group(1).lower()}{match.group(2)}")
103+
new_name = to_snake_case(alias)
104+
if kind == "event":
105+
self.links[
106+
f"[`event: {clazz}.{member}`]"
107+
] = f"`{var_name}.on('{new_name}')`"
108+
elif kind == "property":
109+
self.links[f"[`property: {clazz}.{member}`]"] = f"`{var_name}.{new_name}`"
110+
else:
111+
self.links[f"[`method: {clazz}.{member}`]"] = f"`{var_name}.{new_name}()`"
112+
93113
def print_entry(
94114
self,
95115
class_name: str,
@@ -223,21 +243,8 @@ def beautify_method_comment(self, comment: str, indent: str) -> str:
223243
return self.indent_paragraph("\n".join(result), indent)
224244

225245
def render_links(self, comment: str) -> str:
226-
def render(match: Any) -> str:
227-
return f"{to_snake_case(match.group(1).lower() + match.group(2))}.{to_snake_case(match.group(3))}"
228-
229-
def render_property(match: Any) -> str:
230-
return f"`{render(match)}`"
231-
232-
def render_method(match: Any) -> str:
233-
return f"`{render(match)}()`"
234-
235-
comment = re.sub(
236-
r"\[`method: (JS|CDP|[A-Z])([^.]+)\.([^`]+)`\]", render_method, comment
237-
)
238-
comment = re.sub(
239-
r"\[`property: (JS|CDP|[A-Z])([^.]+)\.([^`]+)`\]", render_property, comment
240-
)
246+
for [old, new] in self.links.items():
247+
comment = comment.replace(old, new)
241248
return comment
242249

243250
def make_optional(self, text: str) -> str:

0 commit comments

Comments
 (0)