feat(console api): first/last/nth (#19485)

This commit is contained in:
Dmitry Gozman 2022-12-15 11:17:59 -08:00 committed by GitHub
parent 3afd83c8cc
commit 1263bc3edd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 0 deletions

View file

@ -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}`);
}
}
@ -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) {

View file

@ -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',
]);
});