Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions deno.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,8 @@
"docs/static/icon.png",
"bindings/celestial.ts"
]
},
"lint": {
"exclude": ["docs"]
}
}
2 changes: 1 addition & 1 deletion docs/pages/showcase.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { launch } from "../../mod.ts";
import type { PageProps } from "https://deno.land/x/pyro@0.6.1/page.ts";
import { ensureFileSync } from "https://deno.land/std@0.215.0/fs/ensure_file.ts";
import { ensureFileSync } from "@std/fs/ensure-file";
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question: is this change necessary for this PR?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The lint check fails for both of those direct https imports, so CI won't pass. You already have the JSR @std/fs in the import map, so I thought this must just be legacy. Couldn't find Pyro on JSR, so just ignored that one.

Would be happy to handle it differently!

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah I see, new deno lints are stricter. Could you instead have the linter ignore the entire docs folder? It's getting rewritten by a friend of mine anyways.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, that's done. The linter will now completely ignore the docs folder.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The commit to update the import from https://deno.land/std is still in there, but we can drop it if you prefer @lino-levan


export const config = {
title: "Showcase",
Expand Down
33 changes: 33 additions & 0 deletions src/locator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,37 @@ export class Locator<T> {
async wait(): Promise<ElementHandle> {
return await this.#page.waitForSelector(this.#selector);
}

/** Creates a derived Locator for a descendant element. */
locator<T>(selector: string): Locator<T> {
return new Locator(
this.#page,
`${this.#selector} ${selector}`,
this.#timeout,
);
}

/**
* Queries the locator for an element matching the given selector.
*
* @example
* ```ts
* const elementWithClass = await locator.$(".class");
* ```
*/
$(selector: string): Promise<ElementHandle | null> {
return this.#runLocator((handle) => handle.$(selector));
}

/**
* Queries the locator for all elements matching the given selector.
*
* @example
* ```ts
* const elementsWithClass = await locator.$$(".class");
* ```
*/
$$(selector: string): Promise<ElementHandle[]> {
return this.#runLocator((handle) => handle.$$(selector));
}
}
49 changes: 48 additions & 1 deletion tests/locator_test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/// <reference lib="dom" />
import { assertEquals } from "@std/assert";
import { assertEquals, assertExists } from "@std/assert";
import { launch } from "../mod.ts";
import * as path from "@std/path";

Expand Down Expand Up @@ -101,3 +101,50 @@ Deno.test("Locator - fill()", async () => {
await page.close();
await browser.close();
});

Deno.test("Locator - locator()", async () => {
await using server = await serveFixture("fixtures/wait_for_element.html");

await using browser = await launch();
await using page = await browser.newPage(server.address);
const targetLocator = page.locator<HTMLDivElement>("#target");

const h1Locator = targetLocator.locator<HTMLHeadingElement>("h1");

const res = await h1Locator.evaluate((el) => {
return document.querySelector("h1") === el;
});
assertEquals(res, true);
});

Deno.test("Locator - $()", async () => {
await using server = await serveFixture("fixtures/wait_for_element.html");

await using browser = await launch();
await using page = await browser.newPage(server.address);
const targetLocator = page.locator<HTMLDivElement>("#target");

const h1El = await targetLocator.$("h1");
assertExists(h1El);

const res = await h1El.evaluate((el) => {
return document.querySelector("h1") === el;
});
assertEquals(res, true);
});

Deno.test("Locator - $$()", async () => {
await using server = await serveFixture("fixtures/wait_for_element.html");

await using browser = await launch();
await using page = await browser.newPage(server.address);
const targetLocator = page.locator<HTMLDivElement>("#target");

const children = await targetLocator.$$("h1");
assertEquals(children.length, 1);

const res = await children[0].evaluate((el) => {
return document.querySelector("h1") === el;
});
assertEquals(res, true);
});