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

Method attachFile

lib/helper/WebDriver.js:1343–1378  ·  view source on GitHub ↗

* Appium: not tested * * {{> attachFile }}

(locator, pathToFile, context = null)

Source from the content-addressed store, hash-verified

1341 * {{> attachFile }}
1342 */
1343 async attachFile(locator, pathToFile, context = null) {
1344 let file = path.join(store.codeceptDir, pathToFile)
1345 if (!fileExists(file)) {
1346 throw new Error(`File at ${file} can not be found on local system`)
1347 }
1348
1349 const res = await findFields.call(this, locator, context)
1350 this.debug(`Uploading ${file}`)
1351
1352 if (res.length) {
1353 const el = selectElement(res, locator, this)
1354 const tag = await this.browser.execute(function (elem) { return elem.tagName }, el)
1355 const type = await this.browser.execute(function (elem) { return elem.type }, el)
1356 if (tag === 'INPUT' && type === 'file') {
1357 if (this.options.remoteFileUpload) {
1358 try {
1359 this.debugSection('File', 'Uploading file to remote server')
1360 file = await this.browser.uploadFile(file)
1361 } catch (err) {
1362 throw new Error(`File can't be transferred to remote server. Set \`remoteFileUpload: false\` in config to upload file locally.\n${err.message}`)
1363 }
1364 }
1365 return el.addValue(file)
1366 }
1367 }
1368
1369 const targetRes = res.length ? res : await this._locate(locator)
1370 assertElementExists(targetRes, locator, 'Element')
1371 const targetEl = selectElement(targetRes, locator, this)
1372 const fileData = {
1373 base64Content: base64EncodeFile(file),
1374 fileName: path.basename(file),
1375 mimeType: getMimeType(path.basename(file)),
1376 }
1377 return this.browser.execute(dropFile, targetEl, fileData)
1378 }
1379
1380 /**
1381 * Appium: not tested

Callers

nothing calls this directly

Calls 7

_locateMethod · 0.95
fileExistsFunction · 0.90
selectElementFunction · 0.90
base64EncodeFileFunction · 0.90
getMimeTypeFunction · 0.90
debugMethod · 0.80
assertElementExistsFunction · 0.70

Tested by

no test coverage detected