Last active
November 19, 2020 10:17
-
-
Save daluu/4411221 to your computer and use it in GitHub Desktop.
A simple example of downloading file with Java outside of Selenium to workaround Selenium limitation in file downloads.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* an example of file download in Java w/ minimal amount of code, library imports, | |
* and object instantiations especially if you wrap/encapsulate code like example below. | |
* NOTE: sample code has been tested to work but not thoroughly tested. Use at your own risk. | |
*/ | |
import java.net.*; | |
import java.io.*; | |
//be sure to delete file after working with it. filenamePrefix ~ "test_", file extension ~ ".jpg", include the "." | |
public String downloadFile(String url, String filenamePrefix, String fileExtension) throws Exception{ | |
//request setup... | |
URLConnection request = null; | |
request = new URL(url).openConnection(); | |
//extract session cookie from Selenium and use with HTTP request calls in Java | |
//example below assumes server is running PHP hence we get PHP session ID | |
//assumes you have direct access to WebDriver here | |
request.setRequestProperty("Cookie", "PHPSESSID="+driver.manage().getCookieNamed("PHPSESSID").getValue()); | |
//add other headers as needed... | |
//make the request to download file to disk as temp file and return path to file | |
//can add a check for HTTP status code 200 if needed, but sample here skips, just check response output (file) | |
InputStream in = request.getInputStream(); | |
File downloadedFile = File.createTempFile(filenamePrefix, fileExtension); | |
FileOutputStream out = new FileOutputStream(downloadedFile); | |
byte[] buffer = new byte[1024]; | |
int len = in.read(buffer); | |
while (len != -1) { | |
out.write(buffer, 0, len); | |
len = in.read(buffer); | |
if (Thread.interrupted()) { | |
throw new InterruptedException(); | |
} | |
} | |
in.close(); | |
out.close(); | |
return downloadedFile.getAbsolutePath(); | |
} |
This is a simpler example to http://ardesco.lazerycode.com/index.php/2012/07/how-to-download-files-with-selenium-and-why-you-shouldnt/
for those that don't need the full features of that solution or have trouble integrating it into their code/framework.
How about multiple cookies?
I handle multiple cookies in the following way:
HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
Set<Cookie> cookies = driver.manage().getCookies();
if (cookies != null) {
StringBuilder cookiesString = new StringBuilder();
for (Cookie cookie : cookies) {
cookiesString.append(cookie.getName()).append("=").append(cookie.getValue()).append("; ");
}
connection.setRequestProperty("Cookie", cookiesString.toString());
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is simple working code. Can be fine tuned to do HTTP status code checking, do try/catch/finally error handling, and check that downloaded file is greater than 0 kb.