diff --git a/.gitignore b/.gitignore index 542f938..a041349 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,4 @@ pom.xml.releaseBackup pom.xml.versionsBackup pom.xml.next release.properties +/bin/ diff --git a/src/main/java/ch/racic/selenium/helper/download/SeleniumDownloadHelper.java b/src/main/java/ch/racic/selenium/helper/download/SeleniumDownloadHelper.java index c99b0d5..e92ee45 100644 --- a/src/main/java/ch/racic/selenium/helper/download/SeleniumDownloadHelper.java +++ b/src/main/java/ch/racic/selenium/helper/download/SeleniumDownloadHelper.java @@ -75,8 +75,8 @@ private void loadJSHelperFiles() throws IOException { * @param url * @return Javascript string */ - private String getDownloadJsCallScript(URL url) { - return ";return seleniumDownloadHelper.getB64Binary('" + url + "');"; + private String getDownloadJsCallScript(URL url, String method, String param) { + return ";return seleniumDownloadHelper.getB64Binary('" + url + "','" + method + "','" + param + "');"; } /** @@ -87,14 +87,27 @@ private String getDownloadJsCallScript(URL url) { * @return raw data */ public FileData getFileFromUrlRaw(URL url) { + return getFileFromUrlRaw(url, "GET", "null"); + } + + /** + * Executes XHR request trough JavaScript to download the given file in the context of the current WebDriver + * session. + * + * @param url + * @param method POST or GET + * @param param Parameters for the Post Request + * @return raw data + */ + public FileData getFileFromUrlRaw(URL url, String method, String param) { String scriptCollection; if (js instanceof InternetExplorerDriver) { - scriptCollection = ieHackTestJs + ieHackJs + base64Js + dlHelperJs + getDownloadJsCallScript(url); + scriptCollection = ieHackTestJs + ieHackJs + base64Js + dlHelperJs + getDownloadJsCallScript(url, method, param); } else if (js instanceof HtmlUnitDriver) { //throw new BrowserNotSupportedException("JS download hack is not working on HTMLUnit"); - scriptCollection = skipIeHackTestJS + base64Js + dlHelperJs + getDownloadJsCallScript(url); + scriptCollection = skipIeHackTestJS + base64Js + dlHelperJs + getDownloadJsCallScript(url, method, param); } else { - scriptCollection = ieHackTestJs + base64Js + dlHelperJs + getDownloadJsCallScript(url); + scriptCollection = ieHackTestJs + base64Js + dlHelperJs + getDownloadJsCallScript(url, method, param); } String jsRetVal = (String) js.executeScript(scriptCollection); String[] jsRetArr = jsRetVal.split(":contentstarts:", 2); diff --git a/src/main/resources/ch/racic/selenium/helper/download/js/seleniumDownloadHelper.js b/src/main/resources/ch/racic/selenium/helper/download/js/seleniumDownloadHelper.js index c1b4f84..400d8df 100644 --- a/src/main/resources/ch/racic/selenium/helper/download/js/seleniumDownloadHelper.js +++ b/src/main/resources/ch/racic/selenium/helper/download/js/seleniumDownloadHelper.js @@ -5,7 +5,7 @@ var seleniumDownloadHelper = { - getBinary: function (url) { + getBinary: function (url, method, param) { // Mozilla/Safari/IE7+ if (window.XMLHttpRequest) { var xhr = new XMLHttpRequest(); @@ -13,15 +13,16 @@ var seleniumDownloadHelper = { } else if (window.ActiveXObject) { var xhr = new ActiveXObject("Microsoft.XMLHTTP"); } - - xhr.open("GET", url, false); + xhr.open(method, url, false); if (xhr.overrideMimeType) { xhr.overrideMimeType('text/plain; charset=x-user-defined'); } else { xhr.setRequestHeader('Accept-Charset', 'x-user-defined'); } - - xhr.send(null); + if (method == 'POST') { + xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded'); + } + xhr.send(param); if (xhr.status != 200) return ''; @@ -43,8 +44,8 @@ var seleniumDownloadHelper = { } }, - getB64Binary: function (url) { - var content = seleniumDownloadHelper.getBinary(url); + getB64Binary: function (url, method, param) { + var content = seleniumDownloadHelper.getBinary(url, method, param); return content[0] + ":contentstarts:" + base64Encode(content[1]); } }; \ No newline at end of file