Skip to content

Commit 29af2d9

Browse files
DavertMikclaude
andcommitted
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 <noreply@anthropic.com>
1 parent aec3876 commit 29af2d9

File tree

3 files changed

+18
-18
lines changed

3 files changed

+18
-18
lines changed

lib/helper/Playwright.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2294,8 +2294,8 @@ class Playwright extends Helper {
22942294
* @param {CodeceptJS.LocatorOrString} locator field located by label|name|CSS|XPath|strict locator.
22952295
* @param {any} [options] [Additional options](https://playwright.dev/docs/api/class-locator#locator-clear) for available options object as 2nd argument.
22962296
*/
2297-
async clearField(locator, options = {}) {
2298-
const els = await findFields.call(this, locator)
2297+
async clearField(locator, options = {}, context = null) {
2298+
const els = await findFields.call(this, locator, context)
22992299
assertElementExists(els, locator, 'Field to clear')
23002300
if (this.options.strict) assertOnlyOneElement(els, locator)
23012301

@@ -2311,8 +2311,8 @@ class Playwright extends Helper {
23112311
/**
23122312
* {{> appendField }}
23132313
*/
2314-
async appendField(field, value) {
2315-
const els = await findFields.call(this, field)
2314+
async appendField(field, value, context = null) {
2315+
const els = await findFields.call(this, field, context)
23162316
assertElementExists(els, field, 'Field')
23172317
if (this.options.strict) assertOnlyOneElement(els, field)
23182318
await highlightActiveElement.call(this, els[0])
@@ -2341,13 +2341,13 @@ class Playwright extends Helper {
23412341
* {{> attachFile }}
23422342
*
23432343
*/
2344-
async attachFile(locator, pathToFile) {
2344+
async attachFile(locator, pathToFile, context = null) {
23452345
const file = path.join(global.codecept_dir, pathToFile)
23462346

23472347
if (!fileExists(file)) {
23482348
throw new Error(`File at ${file} can not be found on local system`)
23492349
}
2350-
const els = await findFields.call(this, locator)
2350+
const els = await findFields.call(this, locator, context)
23512351
assertElementExists(els, locator, 'Field')
23522352
await els[0].setInputFiles(file)
23532353
return this._waitForAction()

lib/helper/Puppeteer.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1580,17 +1580,17 @@ class Puppeteer extends Helper {
15801580
/**
15811581
* {{> clearField }}
15821582
*/
1583-
async clearField(field) {
1584-
return this.fillField(field, '')
1583+
async clearField(field, context = null) {
1584+
return this.fillField(field, '', context)
15851585
}
15861586

15871587
/**
15881588
* {{> appendField }}
15891589
*
15901590
* {{ react }}
15911591
*/
1592-
async appendField(field, value) {
1593-
const els = await findVisibleFields.call(this, field)
1592+
async appendField(field, value, context = null) {
1593+
const els = await findVisibleFields.call(this, field, context)
15941594
assertElementExists(els, field, 'Field')
15951595
highlightActiveElement.call(this, els[0], await this._getContext())
15961596
await els[0].press('End')
@@ -1619,13 +1619,13 @@ class Puppeteer extends Helper {
16191619
*
16201620
* {{> attachFile }}
16211621
*/
1622-
async attachFile(locator, pathToFile) {
1622+
async attachFile(locator, pathToFile, context = null) {
16231623
const file = path.join(global.codecept_dir, pathToFile)
16241624

16251625
if (!fileExists(file)) {
16261626
throw new Error(`File at ${file} can not be found on local system`)
16271627
}
1628-
const els = await findFields.call(this, locator)
1628+
const els = await findFields.call(this, locator, context)
16291629
assertElementExists(els, locator, 'Field')
16301630
await els[0].uploadFile(file)
16311631
return this._waitForAction()

lib/helper/WebDriver.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1289,8 +1289,8 @@ class WebDriver extends Helper {
12891289
* {{> appendField }}
12901290
* {{ react }}
12911291
*/
1292-
async appendField(field, value) {
1293-
const res = await findFields.call(this, field)
1292+
async appendField(field, value, context = null) {
1293+
const res = await findFields.call(this, field, context)
12941294
assertElementExists(res, field, 'Field')
12951295
const elem = usingFirstElement(res)
12961296
highlightActiveElement.call(this, elem)
@@ -1301,8 +1301,8 @@ class WebDriver extends Helper {
13011301
* {{> clearField }}
13021302
*
13031303
*/
1304-
async clearField(field) {
1305-
const res = await findFields.call(this, field)
1304+
async clearField(field, context = null) {
1305+
const res = await findFields.call(this, field, context)
13061306
assertElementExists(res, field, 'Field')
13071307
const elem = usingFirstElement(res)
13081308
highlightActiveElement.call(this, elem)
@@ -1344,13 +1344,13 @@ class WebDriver extends Helper {
13441344
*
13451345
* {{> attachFile }}
13461346
*/
1347-
async attachFile(locator, pathToFile) {
1347+
async attachFile(locator, pathToFile, context = null) {
13481348
let file = path.join(global.codecept_dir, pathToFile)
13491349
if (!fileExists(file)) {
13501350
throw new Error(`File at ${file} can not be found on local system`)
13511351
}
13521352

1353-
const res = await findFields.call(this, locator)
1353+
const res = await findFields.call(this, locator, context)
13541354
this.debug(`Uploading ${file}`)
13551355
assertElementExists(res, locator, 'File field')
13561356
const el = usingFirstElement(res)

0 commit comments

Comments
 (0)