MCPcopy Create free account
hub / github.com/webdriverio/webdriverio / downloadFile

Function downloadFile

packages/webdriverio/src/node/downloadFile.ts:11–65  ·  view source on GitHub ↗
(
    this: WebdriverIO.Browser,
    fileName: string,
    targetDirectory: string
)

Source from the content-addressed store, hash-verified

9 * Command implementation of the `downloadFile` command.
10 */
11export async function downloadFile(
12 this: WebdriverIO.Browser,
13 fileName: string,
14 targetDirectory: string
15): Promise<object> {
16 /**
17 * parameter check
18 */
19 if (typeof fileName !== 'string' || typeof targetDirectory !== 'string') {
20 throw new Error('number or type of arguments don\'t agree with downloadFile command')
21 }
22
23 /**
24 * check if command is available
25 */
26 if (typeof this.download !== 'function') {
27 throw new Error(`The downloadFile command is not available in ${(this.capabilities as WebdriverIO.Capabilities).browserName} and only available when using Selenium Grid`)
28 }
29
30 const response = await this.download(fileName)
31 const base64Content = response.contents
32
33 if (!targetDirectory.endsWith('/')) {
34 targetDirectory += '/'
35 }
36
37 fs.mkdirSync(targetDirectory, { recursive: true })
38 const zipFilePath = path.join(targetDirectory, `${fileName}.zip`)
39 const binaryString = atob(base64Content as string)
40 const bytes = Uint8Array.from(binaryString, char => char.charCodeAt(0))
41 fs.writeFileSync(zipFilePath, bytes)
42
43 const zipData = fs.readFileSync(zipFilePath)
44 const filesData: string[] = []
45
46 try {
47 const zip = await JSZip.loadAsync(zipData)
48 const keys = Object.keys(zip.files)
49
50 // Iterate through each file in the zip archive
51 for (let i = 0; i < keys.length; i++) {
52 const fileData = await zip.files[keys[i]].async('nodebuffer')
53 const dir = path.resolve(targetDirectory, keys[i])
54 fs.writeFileSync(dir, fileData)
55 log.info(`File extracted: ${keys[i]}`)
56 filesData.push(dir)
57 }
58 } catch (error) {
59 log.error('Error unzipping file:', error)
60 }
61
62 return Promise.resolve({
63 files: filesData
64 })
65}

Callers

nothing calls this directly

Calls 3

downloadMethod · 0.80
infoMethod · 0.45
errorMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…