New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(puppeteer-core): Infer element type from complex selector #9253
feat(puppeteer-core): Infer element type from complex selector #9253
Conversation
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
06d83c6
to
15a49bf
Compare
6a3b7ca
to
d446359
Compare
@jrandolf could you please review? |
Hey Gomain, thanks for the PR. As of now, we do not plan to add this feature. We would like TypeScript to first resolve this problem on their side if they ever do: microsoft/TypeScript#29037 Resolving it ourselves may lead to incompatibility in resolution. |
@jrandolf can we have it as an intermediate solution? |
@gomain Could you add test cases for these types in the |
d446359
to
c89aa6b
Compare
c89aa6b
to
dd1a176
Compare
@gomain I think tests can be added along existing tests https://github.com/puppeteer/puppeteer/blob/main/test-d/ElementHandle.test-d.ts#L8 E.g., we don't need to use NodeFor directly but can you the public API and check the types. |
There are a few concerns I think we should discuss.
|
@gomain that kind of refactoring is not a priority for us right now. As for this change, I think it'd be sufficient if we add tests like |
@OrKoN Any comments on points 1. & 2. ? |
@gomain 1) private types are stripped from the resulting .d.ts file so NodeFor needs to be marked public to be available in return types. If we can test it with dts, let's. Otherwise, method-level test-d tests suffice. 2) test-d serves as a e2e test suite to prevent regressions so at least capturing the expectations for some of the methods is useful. |
@gomain what was the problem with using tsd?
Seems to work fine for me? Also, using Element instead of a more specific type throws an error: |
Sorry if I am misunderstanding the issue but it seems to work?:
|
remove dependency on `type-fest`
Breakdown nested composition into intermediate steps. This is preferred because typescript does not typecheck possibility of intermediate nesting evaluating to non-constrainted types.
@OrKoN My apologies, false alarm on tsd behaviour. I must have been doing it wrong and jumped to that conclusion too soon. |
3ced5c7
to
70bf827
Compare
Many thanks! @gomain |
🤖 I have created a release *beep* *boop* --- <details><summary>ng-schematics: 0.1.0</summary> ## 0.1.0 (2022-11-23) ### Features * **ng-schematics:** Release @puppeteer/ng-schematics ([#9244](#9244)) ([be33929](be33929)) </details> <details><summary>puppeteer: 19.3.0</summary> ## [19.3.0](puppeteer-v19.2.2...puppeteer-v19.3.0) (2022-11-23) ### Miscellaneous Chores * **puppeteer:** Synchronize puppeteer versions ### Dependencies * The following workspace dependencies were updated * dependencies * puppeteer-core bumped from 19.2.2 to 19.3.0 </details> <details><summary>puppeteer-core: 19.3.0</summary> ## [19.3.0](puppeteer-core-v19.2.2...puppeteer-core-v19.3.0) (2022-11-23) ### Features * **puppeteer-core:** Infer element type from complex selector ([#9253](#9253)) ([bef1061](bef1061)) * **puppeteer-core:** update Chrome launcher flags ([#9239](#9239)) ([ae87bfc](ae87bfc)) ### Bug Fixes * remove boundary conditions for visibility ([#9249](#9249)) ([e003513](e003513)) </details> --- This PR was generated with [Release Please](https://github.com/googleapis/release-please). See [documentation](https://github.com/googleapis/release-please#release-please). Co-authored-by: release-please[bot] <55107282+release-please[bot]@users.noreply.github.com>
What kind of change does this PR introduce?
Better type inference.
Did you add tests for your changes?
Not yet.Yes.If relevant, did you update the documentation?
Not yet.
Summary
Currently methods that return an element handle, i.e.
.$
,.waitForSelector
attempt to infer the node element type from the selector string. However, this only works when the selector is an exact match of the element tag, i.e. a selector"a"
would be inferred asHTMLAnchorElement
. And not when the selector is complex, i.e. selectors"a#some-id"
,div > a
,a:nth-child(2)
would all fallback onElement
.This is due to simply looking up the the selector in
HTMLElementTagNameMap
andSVGElementTagNameMap
without any attempt to parse the selector string.This PR is an attempt to do so.
Does this PR introduce a breaking change?
This could break existing incorrect assertions using the
as
keyword.Other information
This PR introduces a dependency on thetype-fest
package.This PR is far from complete (no tests, no docs). Put out early for feedback and discussion.