MCPcopy
hub / github.com/codeceptjs/CodeceptJS / attachFile

Method attachFile

lib/helper/Puppeteer.js:1656–1683  ·  view source on GitHub ↗

* > ⚠ There is an [issue with file upload in Puppeteer 2.1.0 & 2.1.1](https://github.com/puppeteer/puppeteer/issues/5420), downgrade to 2.0.0 if you face it. * * {{> attachFile }}

(locator, pathToFile, context = null)

Source from the content-addressed store, hash-verified

1654 * {{> attachFile }}
1655 */
1656 async attachFile(locator, pathToFile, context = null) {
1657 const file = path.join(store.codeceptDir, pathToFile)
1658
1659 if (!fileExists(file)) {
1660 throw new Error(`File at ${file} can not be found on local system`)
1661 }
1662 const els = await findFields.call(this, locator, context)
1663 if (els.length) {
1664 const el = selectElement(els, locator, this)
1665 const tag = await el.evaluate(el => el.tagName)
1666 const type = await el.evaluate(el => el.type)
1667 if (tag === 'INPUT' && type === 'file') {
1668 await el.uploadFile(file)
1669 return this._waitForAction()
1670 }
1671 }
1672
1673 const targetEls = els.length ? els : await this._locate(locator)
1674 assertElementExists(targetEls, locator, 'Element')
1675 const el = selectElement(targetEls, locator, this)
1676 const fileData = {
1677 base64Content: base64EncodeFile(file),
1678 fileName: path.basename(file),
1679 mimeType: getMimeType(path.basename(file)),
1680 }
1681 await el.evaluate(dropFile, fileData)
1682 return this._waitForAction()
1683 }
1684
1685 /**
1686 * {{> selectOption }}

Callers 6

WebDriver_test.jsFile · 0.45
testsFunction · 0.45

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