thunderclient / thunder-client-support

Thunder Client is a lightweight Rest API Client Extension for VS Code.
https://www.thunderclient.com
Other
3.62k stars 128 forks source link

ThunderClient Tests | reading of multiple files and setting to env.variables #1512

Closed nurzhan-kukeyev closed 5 months ago

nurzhan-kukeyev commented 6 months ago

Question: The question is: how can we read (readFile) more than one file in parameters and set contents of files to env.variables ?

Are you using the free version/paid version/trial: paid user - collegue from erwin (deutsche bahn)

image
Artem0ne commented 6 months ago

In addition to the question: We want to get all .json files from project directory via JavaScript:

Example:

In env variable where .json files stored: myPath = "D:\Folder"

JavaScript:

myPath= tc.getVar("myPath");
    const fs = require('fs');
    const path = require('path');
    fs.readdir(myPath, (err, files) => {
    if (err) {
        console.error('Error reading directory:', err);
        return;
    }
});

JavaScript gives an error:

Script Log: Error reading directory: {
  "errno": -4058,
  "code": "ENOENT",
  "syscall": "scandir",
  "path": "C:\\Users\\<user>\\AppData\\Local\\Programs\\Microsoft VS Code\\Folder"
}

JS uses the project folder of VS Code

C:\Users\\AppData\Local\Programs\Microsoft VS Code

Question: How to pass from TC to JavaScript the correct path of the folder? Are there any ways?

rangav commented 6 months ago

You cannot read multiple files in the same test case in the UI.

from script it's possible, try using promises

const fs = require('fs').promises;

// Function to read directory
  try {
    // Directory path
    const directoryPath = tc.getVar('myPath')';

    // Read directory asynchronously with await
    const files = await fs.readdir(directoryPath);

    // List file names
    console.log('Files in the directory:');
    files.forEach(file => {
      console.log(file);
    });
  } catch (err) {
    console.log('Error reading directory:', err);
  }
Artem0ne commented 6 months ago

Hi Ranga, thanks for the fast reply. Unfortunately, this solution with premises didn't work for us: image

JS takes the path where VSCode installed (C:\User...\Appdata)

Maybe other solutions?

rangav commented 6 months ago

Looks like the path is wrong Ref - https://stackoverflow.com/a/43260706/4035948

can you open the path manually in file explorer and test it

rangav commented 5 months ago

I am closing this issue, feel free to continue discussion