Hello,
I have this test case: I need to test uploading bigger file that is split into smaller chunks. I have 3 request in the collection:
- I request upload URL. It has to be run once.
- I need to upload all file chunks as a separate POST requests. It has to be iterated.
- I declare, that all chunks are uploaded. It has to be run once.
To upload a file chunk, I use form-data. To distinct chunks during upload I need to provide couple of parameters in URL, mostly {{chunk}} (chunk number) and {{chunks}} (total number of chunks).
My way of thinking is that I should:
- Store form-data file path as variable: {{chunk_path}};
-
For every iteration I need a different file so I set {{chunk_path}} = {{path}} + {{chunk}} where {{path}} (it is path to a file that doesn’t change, but without chunk number: C:\test\part) and {{chunk}} (chunk number). In the end {{chunk_path}} looks like that: “C:\test\part0”.
-
To create iteration I use if statement where I compare 2 variables and when statement is true, I increment one of variables and use it in next iteration. This is the step that causes some problems.
My Pre-Request Script:
pm.environment.get("chunks");
pm.environment.get("chunk");
pm.environment.set("chunk_path", pm.environment.get("path") + pm.environment.get("chunk"));
console.log(`This iteration chunk number is: ${pm.environment.get('chunk')}`);
console.log(`This iteration chunk path: ${pm.environment.get('chunk_path')}`);
This seems to be OK - I don’t get any errors.
My Tests:
pm.environment.get("chunks"); //100
pm.environment.get("chunk"); //0
if (chunk <= chunks) {
pm.environment.set("chunk", chunk++);
pm.setNextRequest("02. Perform the upload of chunks");
} else {
pm.setNextRequest("03. Finish the upload (no more chunks)");
}
Here I get the error: “ReferenceError: chunk is not defined”
How to correctly do the incrementation? Is my way of thinking correct? Maybe there’s some better way to achieve my goal?
I’m not a JS specialist. I used search on forum and google, tried some references like JavaScript Tutorial but I can’t make it work.