refactor(web-test): этап B.5.2 — findFieldInputId хелпер (4 копии → 1)

В 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) <noreply@anthropic.com>
This commit is contained in:
Nick Shirokov
2026-05-25 22:33:15 +03:00
parent 5b6243bbcc
commit 3fe038277f
2 changed files with 24 additions and 25 deletions
+5 -25
View File
@@ -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);
@@ -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<string|null>}
*/
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;
})()`);
}