Skip to content

Commit 61edde0

Browse files
committed
Adding Support for Post-Request
1 parent 77f647d commit 61edde0

3 files changed

Lines changed: 24 additions & 10 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ pom.xml.releaseBackup
66
pom.xml.versionsBackup
77
pom.xml.next
88
release.properties
9+
/bin/

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

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,8 @@ private void loadJSHelperFiles() throws IOException {
7575
* @param url
7676
* @return Javascript string
7777
*/
78-
private String getDownloadJsCallScript(URL url) {
79-
return ";return seleniumDownloadHelper.getB64Binary('" + url + "');";
78+
private String getDownloadJsCallScript(URL url, String method, String param) {
79+
return ";return seleniumDownloadHelper.getB64Binary('" + url + "','" + method + "','" + param + "');";
8080
}
8181

8282
/**
@@ -87,14 +87,27 @@ private String getDownloadJsCallScript(URL url) {
8787
* @return raw data
8888
*/
8989
public FileData getFileFromUrlRaw(URL url) {
90+
return getFileFromUrlRaw(url, "GET", "null");
91+
}
92+
93+
/**
94+
* Executes XHR request trough JavaScript to download the given file in the context of the current WebDriver
95+
* session.
96+
*
97+
* @param url
98+
* @param method POST or GET
99+
* @param param Parameters for the Post Request
100+
* @return raw data
101+
*/
102+
public FileData getFileFromUrlRaw(URL url, String method, String param) {
90103
String scriptCollection;
91104
if (js instanceof InternetExplorerDriver) {
92-
scriptCollection = ieHackTestJs + ieHackJs + base64Js + dlHelperJs + getDownloadJsCallScript(url);
105+
scriptCollection = ieHackTestJs + ieHackJs + base64Js + dlHelperJs + getDownloadJsCallScript(url, method, param);
93106
} else if (js instanceof HtmlUnitDriver) {
94107
//throw new BrowserNotSupportedException("JS download hack is not working on HTMLUnit");
95-
scriptCollection = skipIeHackTestJS + base64Js + dlHelperJs + getDownloadJsCallScript(url);
108+
scriptCollection = skipIeHackTestJS + base64Js + dlHelperJs + getDownloadJsCallScript(url, method, param);
96109
} else {
97-
scriptCollection = ieHackTestJs + base64Js + dlHelperJs + getDownloadJsCallScript(url);
110+
scriptCollection = ieHackTestJs + base64Js + dlHelperJs + getDownloadJsCallScript(url, method, param);
98111
}
99112
String jsRetVal = (String) js.executeScript(scriptCollection);
100113
String[] jsRetArr = jsRetVal.split(":contentstarts:", 2);

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66

77
var seleniumDownloadHelper = {
8-
getBinary: function (url) {
8+
getBinary: function (url, method, param) {
99
// Mozilla/Safari/IE7+
1010
if (window.XMLHttpRequest) {
1111
var xhr = new XMLHttpRequest();
@@ -14,14 +14,14 @@ var seleniumDownloadHelper = {
1414
var xhr = new ActiveXObject("Microsoft.XMLHTTP");
1515
}
1616

17-
xhr.open("GET", url, false);
17+
xhr.open(method, url, false);
1818
if (xhr.overrideMimeType) {
1919
xhr.overrideMimeType('text/plain; charset=x-user-defined');
2020
} else {
2121
xhr.setRequestHeader('Accept-Charset', 'x-user-defined');
2222
}
2323

24-
xhr.send(null);
24+
xhr.send(param);
2525

2626
if (xhr.status != 200) return '';
2727

@@ -43,8 +43,8 @@ var seleniumDownloadHelper = {
4343
}
4444
},
4545

46-
getB64Binary: function (url) {
47-
var content = seleniumDownloadHelper.getBinary(url);
46+
getB64Binary: function (url, method, param) {
47+
var content = seleniumDownloadHelper.getBinary(url, method, param);
4848
return content[0] + ":contentstarts:" + base64Encode(content[1]);
4949
}
5050
};

0 commit comments

Comments
 (0)