better getHtml function. keep test ids

This commit is contained in:
Tommy D. Rossi
2026-01-31 18:49:51 +01:00
parent b03b6f6a12
commit ec7ed4e590
3 changed files with 42 additions and 4 deletions
+18
View File
@@ -13965,6 +13965,24 @@ test('always keeps test ID attributes', async () => {
`)
})
test('keeps all common e2e test ID attributes', async () => {
const html = `
<div tid="short" qa="quality" qa-id="qa1" e2e="test" e2e-id="e2e1" class="hidden">
<button automation-id="auto1" automationid="auto2" selenium="sel1" pw="play1">Click</button>
<span data-qa="dataqa" data-automation="dataauto" data-pw="datapw">Text</span>
</div>
`
const result = await formatHtmlForPrompt({ html })
expect(result).toMatchInlineSnapshot(`
"<div tid="short" qa="quality" qa-id="qa1" e2e="test" e2e-id="e2e1">
<button automation-id="auto1" automationid="auto2" selenium="sel1" pw="play1">Click</button>
<span data-qa="dataqa" data-automation="dataauto" data-pw="datapw">Text</span>
</div>
"
`)
})
test('processes x.com.html with size savings', async () => {
const html = readFileSync(new URL('./assets/x.com.html', import.meta.url), 'utf-8')
+10 -1
View File
@@ -52,9 +52,18 @@ export async function formatHtmlForPrompt({
'aria-pressed',
'aria-required',
'aria-current',
// Test IDs (data-testid, data-test, data-cy are covered by data-* prefix)
// Test IDs (data-testid, data-test, data-cy, data-qa are covered by data-* prefix)
'testid',
'test-id',
'tid',
'qa',
'qa-id',
'e2e',
'e2e-id',
'automation-id',
'automationid',
'selenium',
'pw',
'vimium-label',
// Conditionally added: 'style', 'class'
]
+14 -3
View File
@@ -469,12 +469,23 @@ const html = await getCleanHTML({ locator: page, search: /button/i })
const diff = await getCleanHTML({ locator: page, showDiffSinceLastCall: true })
```
**Parameters:**
- `locator` - Playwright Locator or Page to get HTML from
- `search` - string/regex to filter results (returns first 10 matching lines)
- `showDiffSinceLastCall` - returns diff since last snapshot
- `search` - string/regex to filter results (returns first 10 matching lines with 5 lines context)
- `showDiffSinceLastCall` - returns unified diff since last call for same locator/page. First call stores snapshot and returns message; subsequent calls return the diff. Useful for tracking DOM changes after actions.
- `includeStyles` - keep style and class attributes (default: false)
Returns cleaned HTML with only essential attributes (aria-*, data-*, href, role, title, alt, etc.). Removes script, style, svg, head tags.
**HTML processing:**
The function cleans HTML for compact, readable output:
- **Removes tags**: script, style, link, meta, noscript, svg, head
- **Unwraps nested wrappers**: Empty divs/spans with no attributes that only wrap a single child are collapsed (e.g., `<div><div><div><p>text</p></div></div></div>``<div><p>text</p></div>`)
- **Removes empty elements**: Elements with no attributes and no content are removed
- **Truncates long values**: Attribute values >200 chars and text content >500 chars are truncated
**Attributes kept (summary):**
- Common semantic and ARIA attributes (e.g., `href`, `name`, `type`, `aria-*`)
- All `data-*` test attributes
- Frequently used test IDs and special attributes (e.g., `testid`, `qa`, `e2e`, `vimium-label`)
For pagination, use `.split('\n').slice(offset, offset + limit).join('\n')`:
```js