Skip to content

Commit 6ac05f1

Browse files
committed
Implemented filename guessing, headerdata first and then url
1 parent 80bc287 commit 6ac05f1

5 files changed

Lines changed: 74 additions & 13 deletions

File tree

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
<groupId>ch.racic.selenium.helper</groupId>
1313
<artifactId>SeleniumDownloadHelper</artifactId>
14-
<version>0.2-SNAPSHOT</version>
14+
<version>0.3-SNAPSHOT</version>
1515
<packaging>jar</packaging>
1616

1717
<name>Selenium Download Helper library</name>
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Copyleft (c) 2014. This code is for learning purposes only. Do whatever you like with it but don't take it as perfect code.
3+
* Michel Racic (http://rac.su/+) => github.com/rac2030
4+
*/
5+
6+
package ch.racic.selenium.helper.download;
7+
8+
/**
9+
* Transport wrapper for raw data
10+
*/
11+
public class FileData {
12+
private String guessedFilename;
13+
private byte[] data;
14+
15+
public FileData(String guessedFilename, byte[] data) {
16+
this.guessedFilename = guessedFilename;
17+
this.data = data;
18+
}
19+
20+
public String getGuessedFilename() {
21+
return guessedFilename;
22+
}
23+
24+
public byte[] getData() {
25+
return data;
26+
}
27+
}

src/main/java/ch/racic/selenium/helper/download/SeleniumDownloadHelper.java

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,10 @@
1212
import org.openqa.selenium.WebDriver;
1313
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
1414
import org.openqa.selenium.ie.InternetExplorerDriver;
15-
import sun.plugin.dom.exception.BrowserNotSupportedException;
1615

1716
import java.io.File;
1817
import java.io.IOException;
19-
import java.io.UnsupportedEncodingException;
2018
import java.net.URL;
21-
import java.nio.charset.Charset;
2219

2320
/**
2421
* Created by rac on 08.06.14.
@@ -89,7 +86,7 @@ private String getDownloadJsCallScript(URL url) {
8986
* @param url
9087
* @return raw data
9188
*/
92-
public byte[] getFileFromUrl(URL url) {
89+
public FileData getFileFromUrlRaw(URL url) {
9390
String scriptCollection;
9491
if (js instanceof InternetExplorerDriver) {
9592
scriptCollection = ieHackTestJs + ieHackJs + base64Js + dlHelperJs + getDownloadJsCallScript(url);
@@ -99,8 +96,14 @@ public byte[] getFileFromUrl(URL url) {
9996
} else {
10097
scriptCollection = ieHackTestJs + base64Js + dlHelperJs + getDownloadJsCallScript(url);
10198
}
102-
String encodedContent = (String) js.executeScript(scriptCollection);
103-
return Base64.decodeBase64(encodedContent);
99+
String jsRetVal = (String) js.executeScript(scriptCollection);
100+
String[] jsRetArr = jsRetVal.split(":contentstarts:", 2);
101+
String encodedContent = jsRetArr[1];
102+
String fileName = jsRetArr[0];
103+
if (fileName.equals("")) {
104+
fileName = url.getFile().replaceAll("/", "");
105+
}
106+
return new FileData(fileName, Base64.decodeBase64(encodedContent));
104107
}
105108

106109
/**
@@ -113,9 +116,25 @@ public byte[] getFileFromUrl(URL url) {
113116
* @throws IOException
114117
*/
115118
public File getFileFromUrl(URL url, File outputFile) throws IOException {
116-
byte[] content = getFileFromUrl(url);
119+
byte[] content = getFileFromUrlRaw(url).getData();
117120
FileUtils.writeByteArrayToFile(outputFile, content);
118121
return outputFile;
119122
}
120123

124+
/**
125+
* Executes XHR request trough JavaScript to download the given file in the context of the current WebDriver
126+
* session. This method returns a tmp file and takes a guessed filename
127+
*
128+
* @param url
129+
* @return outpuFile
130+
* @throws IOException
131+
*/
132+
public File getFileFromUrl(URL url) throws IOException {
133+
FileData fd = getFileFromUrlRaw(url);
134+
byte[] content = fd.getData();
135+
File tmpFile = File.createTempFile("", fd.getGuessedFilename());
136+
FileUtils.writeByteArrayToFile(tmpFile, content);
137+
return tmpFile;
138+
}
139+
121140
}

src/main/resources/ch/racic/selenium/helper/download/js/seleniumDownloadHelper.js

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* Michel Racic (http://rac.su/+) => github.com/rac2030
44
*/
55

6+
67
var seleniumDownloadHelper = {
78
getBinary: function (url) {
89
// Mozilla/Safari/IE7+
@@ -21,16 +22,29 @@ var seleniumDownloadHelper = {
2122
}
2223

2324
xhr.send(null);
25+
2426
if (xhr.status != 200) return '';
27+
28+
// Code taken from http://stackoverflow.com/q/16086162
29+
var disp = xhr.getResponseHeader('Content-Disposition');
30+
var fileName = "";
31+
if (disp && disp.search('filename') != -1) {
32+
var filenamePattern = "filename=\"(.*)\".*";
33+
var re = new RegExp(filenamePattern);
34+
if (re.test(disp)) {
35+
fileName = re.exec(disp)[1];
36+
}
37+
}
38+
2539
if (IE_HACK) {
26-
return bin2arr(xhr.responseBody);
40+
return [fileName, bin2arr(xhr.responseBody)];
2741
} else {
28-
return xhr.responseText;
42+
return [fileName, xhr.responseText];
2943
}
3044
},
3145

3246
getB64Binary: function (url) {
3347
var content = seleniumDownloadHelper.getBinary(url);
34-
return base64Encode(content);
48+
return content[0] + ":contentstarts:" + base64Encode(content[1]);
3549
}
3650
};

src/test/java/ch/racic/selenium/helper/download/SeleniumDownloadHelperTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,9 @@ public void testGetFileFromUrlPhantomJS() throws Exception {
116116
private void invokeGetFileDataFromUrl() throws Exception {
117117
driver.get(baseUrl + indexPage);
118118
SeleniumDownloadHelper sdlh = new SeleniumDownloadHelper(driver);
119-
byte[] fileContent = sdlh.getFileFromUrl(new URL(baseUrl + testPdf));
120-
Assert.assertArrayEquals("Raw data is correct", referenceContent, fileContent);
119+
FileData testFileData = sdlh.getFileFromUrlRaw(new URL(baseUrl + testPdf));
120+
Assert.assertArrayEquals("Raw data is correct", referenceContent, testFileData.getData());
121+
Assert.assertTrue("Guessed name from URL is correct", testPdf.equals(testFileData.getGuessedFilename()));
121122
}
122123

123124
private void invokeGetFileFromUrl() throws Exception {

0 commit comments

Comments
 (0)