Skip to content

Commit

Permalink
feat(console api): first/last/nth (#19485)
Browse files Browse the repository at this point in the history
  • Loading branch information
dgozman authored Dec 15, 2022
1 parent 3afd83c commit 1263bc3
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
6 changes: 6 additions & 0 deletions packages/playwright-core/src/server/injected/consoleApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ class Locator {
self.getByTitle = (text: string | RegExp, options?: { exact?: boolean }): Locator => self.locator(getByTitleSelector(text, options));
self.getByRole = (role: string, options: ByRoleOptions = {}): Locator => self.locator(getByRoleSelector(role, options));
self.filter = (options?: { hasText?: string | RegExp, has?: Locator }): Locator => new Locator(injectedScript, selector, options);
self.first = (): Locator => self.locator('nth=0');
self.last = (): Locator => self.locator('nth=-1');
self.nth = (index: number): Locator => self.locator(`nth=${index}`);
}
}

Expand Down Expand Up @@ -82,6 +85,9 @@ class ConsoleAPI {
...new Locator(injectedScript, ''),
};
delete window.playwright.filter;
delete window.playwright.first;
delete window.playwright.last;
delete window.playwright.nth;
}

private _querySelector(selector: string, strict: boolean): (Element | undefined) {
Expand Down
22 changes: 22 additions & 0 deletions tests/library/inspector/console-api.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,26 @@ it('should support playwright.getBy*', async ({ page }) => {
expect(await page.evaluate(`playwright.getByText('hello').element.innerHTML`)).toContain('Hello');
expect(await page.evaluate(`playwright.getByTitle('world').element.innerHTML`)).toContain('World');
expect(await page.evaluate(`playwright.locator('span').filter({ hasText: 'hello' }).element.innerHTML`)).toContain('Hello');
expect(await page.evaluate(`playwright.locator('span').first().element.innerHTML`)).toContain('Hello');
expect(await page.evaluate(`playwright.locator('span').last().element.innerHTML`)).toContain('World');
expect(await page.evaluate(`playwright.locator('span').nth(1).element.innerHTML`)).toContain('World');
});

it('expected properties on playwright object', async ({ page }) => {
expect(await page.evaluate(`Object.keys(playwright)`)).toEqual([
'$',
'$$',
'inspect',
'selector',
'generateLocator',
'resume',
'locator',
'getByTestId',
'getByAltText',
'getByLabel',
'getByPlaceholder',
'getByText',
'getByTitle',
'getByRole',
]);
});

0 comments on commit 1263bc3

Please sign in to comment.