From 61edde02f46bc71497614c446f931e9c35d2f91e Mon Sep 17 00:00:00 2001 From: Sven Bursch-Osewold Date: Sat, 20 May 2017 11:36:59 +0200 Subject: [PATCH 1/2] Adding Support for Post-Request --- .gitignore | 1 + .../download/SeleniumDownloadHelper.java | 23 +++++++++++++++---- .../download/js/seleniumDownloadHelper.js | 10 ++++---- 3 files changed, 24 insertions(+), 10 deletions(-) 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..68e90ea 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(); @@ -14,14 +14,14 @@ var seleniumDownloadHelper = { 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); + xhr.send(param); if (xhr.status != 200) return ''; @@ -43,8 +43,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 From f181898b4802f8189727f4d96406e444fc38d647 Mon Sep 17 00:00:00 2001 From: Sven Bursch-Osewold Date: Sat, 20 May 2017 14:54:34 +0200 Subject: [PATCH 2/2] Content-type for Post-Requests --- .../selenium/helper/download/js/seleniumDownloadHelper.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) 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 68e90ea..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 @@ -13,14 +13,15 @@ var seleniumDownloadHelper = { } else if (window.ActiveXObject) { var xhr = new ActiveXObject("Microsoft.XMLHTTP"); } - xhr.open(method, url, false); if (xhr.overrideMimeType) { xhr.overrideMimeType('text/plain; charset=x-user-defined'); } else { xhr.setRequestHeader('Accept-Charset', 'x-user-defined'); } - + if (method == 'POST') { + xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded'); + } xhr.send(param); if (xhr.status != 200) return '';