chore: generate get by title (#19119)

This commit is contained in:
Pavel Feldman 2022-11-28 19:55:34 -08:00 committed by GitHub
parent 65b0fb055d
commit 85467d1b7d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 0 deletions

View File

@ -35,6 +35,7 @@ const kLabelScore = 3;
const kRoleWithNameScore = 5;
const kAltTextScore = 10;
const kTextScore = 15;
const kTitleScore = 20;
const kCSSIdScore = 100;
const kRoleWithoutNameScore = 140;
const kCSSInputTypeNameScore = 150;
@ -193,6 +194,9 @@ function buildCandidates(injectedScript: InjectedScript, element: Element, testI
if (element.getAttribute('name') && ['BUTTON', 'FORM', 'FIELDSET', 'FRAME', 'IFRAME', 'INPUT', 'KEYGEN', 'OBJECT', 'OUTPUT', 'SELECT', 'TEXTAREA', 'MAP', 'META', 'PARAM'].includes(element.nodeName))
candidates.push({ engine: 'css', selector: `${cssEscape(element.nodeName.toLowerCase())}[name=${quoteAttributeValue(element.getAttribute('name')!)}]`, score: kCSSInputTypeNameScore });
if (element.getAttribute('title'))
candidates.push({ engine: 'internal:attr', selector: `[title=${escapeForAttributeSelector(element.getAttribute('title')!, false)}]`, score: kTitleScore });
if (['INPUT', 'TEXTAREA'].includes(element.nodeName) && element.getAttribute('type') !== 'hidden') {
if (element.getAttribute('type'))
candidates.push({ engine: 'css', selector: `${cssEscape(element.nodeName.toLowerCase())}[type=${quoteAttributeValue(element.getAttribute('type')!)}]`, score: kCSSInputTypeNameScore });

View File

@ -406,4 +406,12 @@ it.describe('selector generator', () => {
await page.setContent(`<input type=checkbox><div data-testid=wrapper><input type=checkbox></div>`);
expect(await generate(page, '[data-testid=wrapper] > input')).toBe('internal:testid=[data-testid="wrapper"s] >> internal:role=checkbox');
});
it('should generate title selector', async ({ page }) => {
await page.setContent(`<div>
<button title="Send to">Send</button>
<button>Send</button>
</div>`);
expect(await generate(page, 'button')).toBe('internal:attr=[title=\"Send to\"i]');
});
});