Skip to content
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

Export Locator #130

Open
jaydenseric opened this issue Jan 20, 2025 · 3 comments
Open

Export Locator #130

jaydenseric opened this issue Jan 20, 2025 · 3 comments
Labels
enhancement New feature or request good first issue Good for newcomers

Comments

@jaydenseric
Copy link

It would be good to export (perhaps as just a type) the Locator class, to make it easier to cast the results in type-safe JS:

await /** @type {import("@astral/astral").Locator<HTMLAnchorElement>} */ (
  page.locator('a[href="/b"]')
).focus();

I can't figure out the most elegant way, or any way really to get things working with the current API.

For some reason this results in a deno-ts error that } is expected where the < after the ['locator'] is:

await /** @type {import("@astral/astral").Page['locator']<HTMLAnchorElement>} */ (
  page.locator
)('a[href="/b"]').focus();
@jaydenseric
Copy link
Author

Ok, so this syntactically works:

await /** @type {typeof page.locator<HTMLAnchorElement>} */ (
  page.locator
)('a[href="/b"]').focus();

But has the deno-ts error Property 'focus' does not exist on type 'Locator<HTMLAnchorElement>'..

@lino-levan
Copy link
Owner

Is there a reason you don't want to do

await page.locator< HTMLAnchorElement>('a[href="/b"]').focus();

? I do think the locator class should be exported in case you want to pass it around places.

@lino-levan lino-levan added enhancement New feature or request good first issue Good for newcomers labels Jan 20, 2025
@jaydenseric
Copy link
Author

This is what I finally came up with that works (at least according to TypeScript, I haven't got my test suit to progress to this point yet with Astral):

await /** @type {typeof page.locator<HTMLAnchorElement>} */ (
  page.locator
)('a[href="/b"]').evaluate((element) => {
  element.focus();
});

Locators don't have a .focus() method.

Is there a reason you don't want to do

Yes, because TypeScript syntax is unavailable in JavaScript modules in no-build projects that have type safety via TypeScript JSDoc. This type of thing would become less cumbersome if microsoft/TypeScript#27387 is ever resolved.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

2 participants