4

I have a Protractor test to validate that an excel file is correctly downloaded in the Downloads folder and then I am able to upload the same. Since I will be running the test multiple times, I need a way to clear my Downloads directory before I run the Protractor tests. That way any duplicate files from the previous runs are cleared. How can I do that? This is how my test to upload an excel file looks:

var fileToUpload = 'Test.xlsm';

it('Test Case: to Validate the Excel Upload', function() {

    var absolutePath = path.resolve('C:/Users/.../Downloads', fileToUpload);

    $('input[type="file"]').sendKeys(absolutePath); 


    });
});
4

You just need to remove all files in the "Downloads" directory inside the onPrepare() function which you define inside your Protractor configuration file.

Let's use the synchronous rmDir function suggested here (feel free to use a different method):

var fs = require('fs');

function rmDir (dirPath) {
  try { var files = fs.readdirSync(dirPath); }
  catch(e) { return; }
  if (files.length > 0)
    for (var i = 0; i < files.length; i++) {
      var filePath = dirPath + '/' + files[i];
      if (fs.statSync(filePath).isFile())
        fs.unlinkSync(filePath);
      else
        rmDir(filePath);
    }
  fs.rmdirSync(dirPath);
}; 

module.exports = {
  // ...

  onPrepare: function () {
    rmDir('C:/Users/.../Downloads');
  },
};

It is also probably a good idea to avoid specifying the path to the "Downloads" directory multiple times and in multiple places (see: DRY principle) - see if you can have it inside a configuration file and reuse, or define as an environment variable.

  • I am also trying to do the same changes in my Config file, but when I run the test it fails saying : export const config = { ^^^^^^SyntaxError: Unexpected token export at new Script (vm.js:80:7) at createScript (vm.js:274:10) at Object.runInThisContext (vm.js:326:10) at Module._compile (internal/modules/cjs/loader.js:664:28) at Object.Module._extensions..js (internal/modules/cjs/loader.js:712:10) at Module.load (internal/modules/cjs/loader.js:600:32) at tryModuleLoad (internal/modules/cjs/loader.js:539:12) at Function.Module._load (internal – ssharma Jul 2 at 19:40
0

Try the below code and share your feedback

Arrays.stream(Objects.requireNonNull(
                new File(String.valueOf(dir)).listFiles())).forEach(File::delete);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.