From 29af2d91c2fe4f289d3e59124dd60d7c8379abfd Mon Sep 17 00:00:00 2001 From: DavertMik Date: Sat, 7 Mar 2026 16:54:47 +0200 Subject: [PATCH 1/3] feat: add context parameter to appendField, clearField, attachFile Add optional context parameter to remaining form-filling methods (appendField, clearField, attachFile) across Playwright, Puppeteer, and WebDriver helpers for consistency with fillField, selectOption, checkOption, and uncheckOption which already support it. This allows scoping element search to a specific DOM container: I.appendField('Name', 'jon', '.form-container') I.clearField('Name', '.form-container') I.attachFile('Avatar', 'data/avatar.jpg', '.form-container') Co-Authored-By: Claude Opus 4.6 --- lib/helper/Playwright.js | 12 ++++++------ lib/helper/Puppeteer.js | 12 ++++++------ lib/helper/WebDriver.js | 12 ++++++------ 3 files changed, 18 insertions(+), 18 deletions(-) diff --git a/lib/helper/Playwright.js b/lib/helper/Playwright.js index de2925bda..ae33b8d31 100644 --- a/lib/helper/Playwright.js +++ b/lib/helper/Playwright.js @@ -2294,8 +2294,8 @@ class Playwright extends Helper { * @param {CodeceptJS.LocatorOrString} locator field located by label|name|CSS|XPath|strict locator. * @param {any} [options] [Additional options](https://playwright.dev/docs/api/class-locator#locator-clear) for available options object as 2nd argument. */ - async clearField(locator, options = {}) { - const els = await findFields.call(this, locator) + async clearField(locator, options = {}, context = null) { + const els = await findFields.call(this, locator, context) assertElementExists(els, locator, 'Field to clear') if (this.options.strict) assertOnlyOneElement(els, locator) @@ -2311,8 +2311,8 @@ class Playwright extends Helper { /** * {{> appendField }} */ - async appendField(field, value) { - const els = await findFields.call(this, field) + async appendField(field, value, context = null) { + const els = await findFields.call(this, field, context) assertElementExists(els, field, 'Field') if (this.options.strict) assertOnlyOneElement(els, field) await highlightActiveElement.call(this, els[0]) @@ -2341,13 +2341,13 @@ class Playwright extends Helper { * {{> attachFile }} * */ - async attachFile(locator, pathToFile) { + async attachFile(locator, pathToFile, context = null) { const file = path.join(global.codecept_dir, pathToFile) if (!fileExists(file)) { throw new Error(`File at ${file} can not be found on local system`) } - const els = await findFields.call(this, locator) + const els = await findFields.call(this, locator, context) assertElementExists(els, locator, 'Field') await els[0].setInputFiles(file) return this._waitForAction() diff --git a/lib/helper/Puppeteer.js b/lib/helper/Puppeteer.js index ba6b324bd..f230e83f0 100644 --- a/lib/helper/Puppeteer.js +++ b/lib/helper/Puppeteer.js @@ -1580,8 +1580,8 @@ class Puppeteer extends Helper { /** * {{> clearField }} */ - async clearField(field) { - return this.fillField(field, '') + async clearField(field, context = null) { + return this.fillField(field, '', context) } /** @@ -1589,8 +1589,8 @@ class Puppeteer extends Helper { * * {{ react }} */ - async appendField(field, value) { - const els = await findVisibleFields.call(this, field) + async appendField(field, value, context = null) { + const els = await findVisibleFields.call(this, field, context) assertElementExists(els, field, 'Field') highlightActiveElement.call(this, els[0], await this._getContext()) await els[0].press('End') @@ -1619,13 +1619,13 @@ class Puppeteer extends Helper { * * {{> attachFile }} */ - async attachFile(locator, pathToFile) { + async attachFile(locator, pathToFile, context = null) { const file = path.join(global.codecept_dir, pathToFile) if (!fileExists(file)) { throw new Error(`File at ${file} can not be found on local system`) } - const els = await findFields.call(this, locator) + const els = await findFields.call(this, locator, context) assertElementExists(els, locator, 'Field') await els[0].uploadFile(file) return this._waitForAction() diff --git a/lib/helper/WebDriver.js b/lib/helper/WebDriver.js index 667b47af0..8719ae346 100644 --- a/lib/helper/WebDriver.js +++ b/lib/helper/WebDriver.js @@ -1289,8 +1289,8 @@ class WebDriver extends Helper { * {{> appendField }} * {{ react }} */ - async appendField(field, value) { - const res = await findFields.call(this, field) + async appendField(field, value, context = null) { + const res = await findFields.call(this, field, context) assertElementExists(res, field, 'Field') const elem = usingFirstElement(res) highlightActiveElement.call(this, elem) @@ -1301,8 +1301,8 @@ class WebDriver extends Helper { * {{> clearField }} * */ - async clearField(field) { - const res = await findFields.call(this, field) + async clearField(field, context = null) { + const res = await findFields.call(this, field, context) assertElementExists(res, field, 'Field') const elem = usingFirstElement(res) highlightActiveElement.call(this, elem) @@ -1344,13 +1344,13 @@ class WebDriver extends Helper { * * {{> attachFile }} */ - async attachFile(locator, pathToFile) { + async attachFile(locator, pathToFile, context = null) { let file = path.join(global.codecept_dir, pathToFile) if (!fileExists(file)) { throw new Error(`File at ${file} can not be found on local system`) } - const res = await findFields.call(this, locator) + const res = await findFields.call(this, locator, context) this.debug(`Uploading ${file}`) assertElementExists(res, locator, 'File field') const el = usingFirstElement(res) From 7851d210bfc40a63ff28fa21f62c5c0cbcc08ba9 Mon Sep 17 00:00:00 2001 From: DavertMik Date: Sat, 7 Mar 2026 18:46:49 +0200 Subject: [PATCH 2/3] test: add tests for context parameter on appendField, clearField, attachFile Add shared webapi tests verifying that context scoping works for appendField, clearField, and attachFile. Extended the context.php test page with pre-filled values and file inputs to support the tests. Co-Authored-By: Claude Opus 4.6 --- test/data/app/view/form/context.php | 8 ++++++-- test/helper/webapi.js | 29 +++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/test/data/app/view/form/context.php b/test/data/app/view/form/context.php index 9abcf8129..dfaec0768 100644 --- a/test/data/app/view/form/context.php +++ b/test/data/app/view/form/context.php @@ -2,22 +2,26 @@
- + + +
- + + + Only here
diff --git a/test/helper/webapi.js b/test/helper/webapi.js index 26220f973..dafcb9a63 100644 --- a/test/helper/webapi.js +++ b/test/helper/webapi.js @@ -634,6 +634,35 @@ export function tests() { const val1 = await I.grabValueFrom('#age1') assert.equal(val1, 'child') }) + + it('should append field within context', async () => { + await I.amOnPage('/form/context') + await I.appendField('Name', '_appended', '#area2') + const val = await I.grabValueFrom('#name2') + assert.equal(val, 'old2_appended') + const val1 = await I.grabValueFrom('#name1') + assert.equal(val1, 'old1') + }) + + it('should clear field within context', async () => { + await I.amOnPage('/form/context') + if (isHelper('Playwright')) { + await I.clearField('Name', {}, '#area2') + } else { + await I.clearField('Name', '#area2') + } + await I.seeInField('#name2', '') + await I.seeInField('#name1', 'old1') + }) + + it('should attach file within context', async () => { + await I.amOnPage('/form/context') + await I.attachFile('Avatar', 'app/avatar.jpg', '#area2') + const val2 = await I.executeScript(() => document.getElementById('file2').files.length) + assert.equal(val2, 1, 'file2 should have a file attached') + const val1 = await I.executeScript(() => document.getElementById('file1').files.length) + assert.equal(val1, 0, 'file1 should have no files') + }) }) describe('#shadow DOM', () => { From 551ab941ed61fbebaba4afc389aa30b0eb290f7b Mon Sep 17 00:00:00 2001 From: DavertMik Date: Sat, 7 Mar 2026 20:42:15 +0200 Subject: [PATCH 3/3] refactor: simplify Playwright clearField signature to match other helpers Remove unused `options` parameter from Playwright's clearField. The options were accepted but never passed to the underlying clear() call. Now all helpers have consistent `clearField(locator, context)` signature. Co-Authored-By: Claude Opus 4.6 --- lib/helper/Playwright.js | 18 ++---------------- test/helper/webapi.js | 6 +----- 2 files changed, 3 insertions(+), 21 deletions(-) diff --git a/lib/helper/Playwright.js b/lib/helper/Playwright.js index ae33b8d31..1357d9965 100644 --- a/lib/helper/Playwright.js +++ b/lib/helper/Playwright.js @@ -2278,23 +2278,9 @@ class Playwright extends Helper { } /** - * Clears the text input element: ``, `