MCPcopy Index your code
hub / github.com/codeceptjs/CodeceptJS / attachFile

Method attachFile

lib/helper/Playwright.js:2346–2373  ·  view source on GitHub ↗

* {{> attachFile }} *

(locator, pathToFile, context = null)

Source from the content-addressed store, hash-verified

2344 *
2345 */
2346 async attachFile(locator, pathToFile, context = null) {
2347 const file = path.join(store.codeceptDir, pathToFile)
2348
2349 if (!fileExists(file)) {
2350 throw new Error(`File at ${file} can not be found on local system`)
2351 }
2352 const els = await findFields.call(this, locator, context)
2353 if (els.length) {
2354 const el = selectElement(els, locator, this)
2355 const tag = await el.evaluate(el => el.tagName)
2356 const type = await el.evaluate(el => el.type)
2357 if (tag === 'INPUT' && type === 'file') {
2358 await el.setInputFiles(file)
2359 return this._waitForAction()
2360 }
2361 }
2362
2363 const targetEls = els.length ? els : await this._locate(locator)
2364 assertElementExists(targetEls, locator, 'Element')
2365 const el = selectElement(targetEls, locator, this)
2366 const fileData = {
2367 base64Content: base64EncodeFile(file),
2368 fileName: path.basename(file),
2369 mimeType: getMimeType(path.basename(file)),
2370 }
2371 await el.evaluate(dropFile, fileData)
2372 return this._waitForAction()
2373 }
2374
2375 /**
2376 * {{> selectOption }}

Callers

nothing calls this directly

Calls 8

_waitForActionMethod · 0.95
_locateMethod · 0.95
fileExistsFunction · 0.90
selectElementFunction · 0.90
base64EncodeFileFunction · 0.90
getMimeTypeFunction · 0.90
evaluateMethod · 0.80
assertElementExistsFunction · 0.70

Tested by

no test coverage detected