* Appium: not tested * * {{> attachFile }}
(locator, pathToFile, context = null)
| 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 |
nothing calls this directly
no test coverage detected