Download a File

How to download a file to a specific folder and then delete the file .

I have tried all the possibilities mentioned in the blog , also tried the selenium code but it didnt work and the file was downloaded to download folder itself .

Can anyone help me on this

Hello @nacchicool2007
you can delete the file by giving the extension of the file you downloaded as follows.
You can add this to the test case or it can be defined as a separate keyword.

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

File filename = new File('C:\\Users\\test\\Desktop\\test.txt')
filename.delete();
1 Like

You can define where you want files to download using desired capabilities. For Chrome it would be:

1.) Open Project > Settings > Desired Capabilities > Web UI > Chrome:

2.) Add a new desired capability, and set Name = prefs, Type = Dictionary, then click on the ellipses in the Value column:

image

3.) In the Dictionary Property Builder, add a row and set Name = download.default_directory, Type = String, and set Value to the location you would like files to go to:

Now, any files downloaded by your browser will go to that directory. Then, you can follow @emine’s recommendation on how to delete that file when you’re done.

Hope this helps!

Thanks for reply . This works on my local machine . How to make to work with Jenkins .

Scripts are getting failed with jenkins.

Hi,
Currently using chrome desired capabilities am downloading all the files into one particular directory but my requirement is like all the CSV downloading files has to be in one folder and PDF should be in another folder is it possible?

No, that’s not possible. Chrome has only one download directory, and the same is true for chromedriver. What you could do though is set a default directory as shown above, then just write some code to copy certain file types to whichever directories you prefer.

Is there any any reference if yes could you please share me.

Sure:

Thank You @Brandon_Hein.

Hi @Brandon_Hein In my test case i’m calling batch file but it’s throwing an error like(Method definition not expected here. Please define the method at an appropriate place or perhaps try using a block/Closure instead. at line: 74 column: 2. File: script1593420794488.groovy @ line 74, column 2.),Can please help me how i can resolve.

This is a separate issue and should be treated as such. Please post a new topic. Thanks!

1 Like