From 3fe038277fb7d95557332c40440d8327df3f720b Mon Sep 17 00:00:00 2001 From: Nick Shirokov Date: Mon, 25 May 2026 22:33:15 +0300 Subject: [PATCH] =?UTF-8?q?refactor(web-test):=20=D1=8D=D1=82=D0=B0=D0=BF?= =?UTF-8?q?=20B.5.2=20=E2=80=94=20findFieldInputId=20=D1=85=D0=B5=D0=BB?= =?UTF-8?q?=D0=BF=D0=B5=D1=80=20(4=20=D0=BA=D0=BE=D0=BF=D0=B8=D0=B8=20?= =?UTF-8?q?=E2=86=92=201)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit В selectValue было 4 одинаковых блока поиска input-элемента поля по имени (form{N}_{name} либо form{N}_{name}_i0 для refs): clear-ветка, composite-type-ветка, F4-fallback, "last resort" F4. core/helpers.mjs: findFieldInputId(formNum, fieldName) → string|null. ~30 LOC дублей убрано, поведение 1-в-1. Co-Authored-By: Claude Opus 4.7 (1M context) --- .claude/skills/web-test/scripts/browser.mjs | 30 ++++--------------- .../skills/web-test/scripts/core/helpers.mjs | 19 ++++++++++++ 2 files changed, 24 insertions(+), 25 deletions(-) diff --git a/.claude/skills/web-test/scripts/browser.mjs b/.claude/skills/web-test/scripts/browser.mjs index 159d5e15..625085e1 100644 --- a/.claude/skills/web-test/scripts/browser.mjs +++ b/.claude/skills/web-test/scripts/browser.mjs @@ -142,7 +142,7 @@ import { closeModals, checkForErrors, dismissPendingErrors, fetchErrorStack, _detectPlatformDialogs, _closePlatformDialogs, } from './core/errors.mjs'; -import { safeClick } from './core/helpers.mjs'; +import { safeClick, findFieldInputId } from './core/helpers.mjs'; // Re-export only what was publicly exported before the refactor. // waitForStable/waitForCondition/startNetworkMonitor/closeModals/checkForErrors/ // dismissPendingErrors are internal helpers — imported above for local use only. @@ -2195,12 +2195,7 @@ export async function selectValue(fieldName, searchText, { type } = {}) { // === CLEAR FIELD if searchText is empty/null === if (!searchText && searchText !== 0) { - const inputId = await page.evaluate(`(() => { - const p = 'form${formNum}_'; - const name = ${JSON.stringify(btn.fieldName)}; - const el = document.querySelector('[id="' + p + name + '"], [id="' + p + name + '_i0"]'); - return el ? el.id : null; - })()`); + const inputId = await findFieldInputId(formNum, btn.fieldName); if (inputId) { await page.click(`[id="${inputId}"]`); await page.waitForTimeout(200); @@ -2219,12 +2214,7 @@ export async function selectValue(fieldName, searchText, { type } = {}) { // then open type selection dialog, pick the type, then pick the value. if (type) { // Find and focus the field input - const inputId = await page.evaluate(`(() => { - const p = 'form${formNum}_'; - const name = ${JSON.stringify(btn.fieldName)}; - const el = document.querySelector('[id="' + p + name + '"], [id="' + p + name + '_i0"]'); - return el ? el.id : null; - })()`); + const inputId = await findFieldInputId(formNum, btn.fieldName); if (!inputId) throw new Error(`selectValue: field "${btn.fieldName}" input not found`); // Clear cached type + value with Shift+F4 @@ -2434,12 +2424,7 @@ export async function selectValue(fieldName, searchText, { type } = {}) { await page.waitForTimeout(500); // Focus the field input and press F4 to open selection form - const inputId = await page.evaluate(`(() => { - const p = 'form${formNum}_'; - const name = ${JSON.stringify(btn.fieldName)}; - const el = document.querySelector('[id="' + p + name + '"], [id="' + p + name + '_i0"]'); - return el ? el.id : null; - })()`); + const inputId = await findFieldInputId(formNum, btn.fieldName); if (inputId) { await page.click(`[id="${inputId}"]`); await page.waitForTimeout(300); @@ -2489,12 +2474,7 @@ export async function selectValue(fieldName, searchText, { type } = {}) { await page.keyboard.press('Escape'); await page.waitForTimeout(300); - const inputId = await page.evaluate(`(() => { - const p = 'form${formNum}_'; - const name = ${JSON.stringify(btn.fieldName)}; - const el = document.querySelector('[id="' + p + name + '"], [id="' + p + name + '_i0"]'); - return el ? el.id : null; - })()`); + const inputId = await findFieldInputId(formNum, btn.fieldName); if (inputId) { await page.click(`[id="${inputId}"]`); await page.waitForTimeout(300); diff --git a/.claude/skills/web-test/scripts/core/helpers.mjs b/.claude/skills/web-test/scripts/core/helpers.mjs index 4e138e2f..7f1ecdc3 100644 --- a/.claude/skills/web-test/scripts/core/helpers.mjs +++ b/.claude/skills/web-test/scripts/core/helpers.mjs @@ -34,3 +34,22 @@ export async function safeClick(selector, { timeout, dismissErrors = false } = { } } } + +/** + * Find a form field's input element id by name. Tries `form{N}_{name}` first, + * then `form{N}_{name}_i0` (reference fields use the _i0 suffix). Returns the + * element id or null. Used in selectValue's clear/composite-type/F4 fallback + * branches. + * + * @param {number} formNum + * @param {string} fieldName + * @returns {Promise} + */ +export async function findFieldInputId(formNum, fieldName) { + return await page.evaluate(`(() => { + const p = 'form${formNum}_'; + const name = ${JSON.stringify(fieldName)}; + const el = document.querySelector('[id="' + p + name + '"], [id="' + p + name + '_i0"]'); + return el ? el.id : null; + })()`); +}