Currently I have the following prefs
set in the capabilities
of wdio.conf
capabilities:[{
'goog:chromeOptions': {
prefs: {
'download.default_directory': downloadDir
}}
}]
downloadDir is created as a global variable as:
global.downloadDir = path.join(__dirname, 'localDownload');
I have created another global variable newDwnldDirPath
in beforeTest
to dynamically create a folder based on the test case number for each test case which is being executed.
global.newDwnldDirPath = path.join('./newDwnldDir/'+matches[0]);
Where +matches[0])
picks up the test case number by using a regex operation. This is working and I get the new download directory created with test case number each for the test case like the following:
..
/TEST001/
/TEST002/
..
What I would like to do is override the default download directory in the test case execution such that the downloaded file goes to the newDwnldDirPath
Test script (mochajs) sample:
it('Test case doing something TEST001', function () {
BasePage.clickDownloadFile();
}
So once this is run the downloaded file should go to newDwnldDirPath
which will be /TEST001/
and continues as so on.
Is there a way we can achieve this?
As of now what I found is the configuration in webdriverio doesn't allow us to change the download.default_directory
configured in prefs
in capabilities
dynamically based on the execution of each test case script. The reason for this is that the prefs
can't be modified or reload the session with new prefs
once the webdriverio starts executing the test cases.
I have found a workaround for the requirements that is by using the recursive-copy
npm package (FYR: https://www.npmjs.com/package/recursive-copy).
Currently I am creating a dynamic folder with the number of test case (test case id) and using it as a global variable newDwnldDirPath
in beforeTest
in wdio.conf.js
global.newDwnldDirPath = path.join('./newDwnldDir/'+matches[0]);
(matches[0] info on this is in the question
)
Then created a util
for doing a recursive copy
from source (default_directory
) to destination (which is the newDwnldDirPath
)
var copyFileRecursive = require('recursive-copy');
var options = {
filter: [
'filename*.txt'
],
rename: function(filePath) {
return 'newFilename.txt';
},
};
copyFileRecursive(src,dest){
copyFileRecursive(src, dest,options)
.on(copyFileRecursive.events.COPY_FILE_START, function(copyOperation) {
console.info('Copying file ' + copyOperation.src + '...');
})
.on(copyFileRecursive.events.COPY_FILE_COMPLETE, function(copyOperation) {
console.info('Copied to ' + copyOperation.dest);
})
.on(copyFileRecursive.events.ERROR, function(error, copyOperation) {
console.error('Unable to copy ' + copyOperation.dest);
})
.then(function(results) {
console.info(results.length + ' file(s) copied');
})
.catch(function(error) {
return console.error('Copy failed: ' + error);
});
}
Then used this copyFileRecursive
method in the test case script as
Util.copyFileRecursive(downloadDir,newDwnldDirPath);
Where downloadDir
is the default_directory for download which is the global variable and the newDwnldDirPath
is the new download directory like ex: newDwnldDir/TEST001
and so on.
The options parameter is optional and can be used if you would like to pass wildcard filename parameters, rename the files etc. There are many other options in that which can be found in the recursive-copy package documentation.