How to wait until file is download.

6,230 views
Skip to first unread message

raja

unread,
Jun 19, 2017, 12:31:24 AM6/19/17
to webdriver
Hi Everyone,

I need to wait until file is downloaded.

Downloaded time is change as per the size of the download file. 

I am using webdriver and java.

Anyone have any idea how to wait until file get downloaded.

Thanks in advance for your time and suggestions. 

Regards,
Rajasekhar. 

Bill Ross

unread,
Jun 19, 2017, 12:33:49 AM6/19/17
to webd...@googlegroups.com

How are you causing the download to happen?

Bill

--
You received this message because you are subscribed to the Google Groups "webdriver" group.
To unsubscribe from this group and stop receiving emails from it, send an email to webdriver+...@googlegroups.com.
To post to this group, send email to webd...@googlegroups.com.
Visit this group at https://groups.google.com/group/webdriver.
For more options, visit https://groups.google.com/d/optout.

raja

unread,
Jun 19, 2017, 1:08:44 AM6/19/17
to webdriver
I need to check some verification points on downloaded file like file extension is correct or not.  Every page footer having company logo or not. 

So, for doing these verification's i need to wait until file get downloaded. 

Bill Ross

unread,
Jun 19, 2017, 1:20:13 AM6/19/17
to webd...@googlegroups.com

How are you causing the download to happen?


Bill Ross

unread,
Jun 19, 2017, 1:53:15 AM6/19/17
to webd...@googlegroups.com

I.e. what action do you perform to initiate the download?

SuperKevy

unread,
Jun 19, 2017, 5:09:44 PM6/19/17
to webdriver
The simple answer is to loop waiting for the file existence of the download file and expect a timeout for some overt time as a failure.
Obviously you'll need to know where the download will land or you'll need to set the browsers landing folder..
Typically an intermediate file is created is created in chrome with a  .crdownload file extension. 
Other web browsers may store in-progress downloads in a different folder and move them to your downloads folder when they're finished.

Ruby example:

# file? will only return true for files
File.file?(filename)
and

# Will also return true for directories - watch out!
File.exist?(filename)

Raja Sekhar

unread,
Jun 20, 2017, 12:25:09 AM6/20/17
to webdriver
Thanks to every one for taking time and giving valuable suggestions to my post.

Please find my comments below which highlighted for your questions.

@Mahendra Reddy,

1. Verify downloaded file existing or not
        Downloaded file will have not same filename every time which we downloaded.
2.  If file existence true then verify file size if it has more then 1kb then go for verification's.
       I can't start read the file if it is more than 1kb as file may not download completely.. if I start reading i will get exceptions.

@Ross,


I.e. what action do you perform to initiate the download?
       I will click one link say download, whatever records available in the web table... all the records will get download.

@Kevy,

I will try your solution.

Thanks every one for your valuable time. 

Regards,
Rajasekhar.

--
You received this message because you are subscribed to the Google Groups "webdriver" group.
To unsubscribe from this group and stop receiving emails from it, send an email to webdriver+unsubscribe@googlegroups.com.

sandee...@highq.com

unread,
Jun 20, 2017, 8:33:18 AM6/20/17
to webdriver
Hi Rajeshkhar,

You can do one, thing, 
just click on download button, and verify that given file is saved or not at given directory.
for that, you just need to verify the file name, into given directory, with file name and extension.

and once the file with the extension matched, then bingo.

I tihnk this logic should work.

Jazz

unread,
Jul 10, 2017, 9:30:36 PM7/10/17
to webdriver
The main logic behind this functionality would:
Step 1. Getting the file size which you want to download using HTTP headers content length.
Step 2. Create input and output streams.
Step 3. Checking if the input stream have reached that byte value(meaning if the total bytes of the file is received)
Step 4. Check the percentage for 100 when your file is completed downloading and do what you want to do.

See the code below:
                URL url= new URL(URLOfFileToDownload);
InputStream in = new BufferedInputStream(url.openStream());
       ByteArrayOutputStream out = new ByteArrayOutputStream();
       byte[] buf = new byte[1024];
       int n = 0;
       String fileToDownload = null;
       double updatedFileSize=0;
       double totalFileSize = tryGetFileSizeInKB(url)*1024;
       int percent;
       String[] pathContents = url.getPath().split("[\\\\/]");
            if(pathContents != null){
            fileToDownload =pathContents[pathContents.length-1];
            System.out.println("File Name : " + fileToDownload );
            }
            
                 System.out.println("Size : "+ totalFileSize/1024 + " KB");
      
    while (-1 != (n = in.read(buf))) {
            updatedFileSize = updatedFileSize +n;
           out.write(buf, 0, n);
           percent = (int)Math.round((updatedFileSize/totalFileSize)*100);
            if(percent == 100)
{
DO YOUR STUFF 
}
       }
       out.close();
       in.close();
       byte[] response = out.toByteArray();
       FileOutputStream fos = new FileOutputStream(FileLocationToSave);
       fos.write(response);
       fos.close();

private int tryGetFileSizeInKB(URL url) {
HttpURLConnection conn = null;
        try {
            conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("HEAD");
            conn.getInputStream();
            return conn.getContentLength()/1024;
        } catch (IOException e) {
            return -1;
        } finally {
            conn.disconnect();
        }
    }

Let me know if you need any help.
Reply all
Reply to author
Forward
0 new messages