-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #361 from dn0000001/file-download-js
Add support to download files using JavaScript
- Loading branch information
Showing
8 changed files
with
337 additions
and
4 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
26 changes: 26 additions & 0 deletions
26
taf/src/main/java/com/lazerycode/selenium/filedownloader/DownloadData.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package com.lazerycode.selenium.filedownloader; | ||
|
||
/** | ||
* Class to hold the status & the downloaded file | ||
*/ | ||
public class DownloadData { | ||
private int status; | ||
private byte[] file; | ||
|
||
public int getStatus() { | ||
return status; | ||
} | ||
|
||
public void setStatus(int status) { | ||
this.status = status; | ||
} | ||
|
||
public byte[] getFile() { | ||
return file; | ||
} | ||
|
||
public void setFile(byte[] file) { | ||
this.file = file; | ||
} | ||
|
||
} |
This file contains 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
49 changes: 49 additions & 0 deletions
49
taf/src/main/java/com/taf/automation/ui/support/util/DownloadUtils.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package com.taf.automation.ui.support.util; | ||
|
||
import org.apache.commons.io.FileUtils; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
|
||
@SuppressWarnings("java:S3252") | ||
public class DownloadUtils { | ||
private DownloadUtils() { | ||
// Prevent initialization of class as all public methods should be static | ||
} | ||
|
||
/** | ||
* Wrapper of File.createTempFile that does not require handling of the exception | ||
* | ||
* @param prefix - The prefix string to be used in generating the file's name; | ||
* must be at least three characters long | ||
* @param suffix - he suffix string to be used in generating the file's name; | ||
* may be null, in which case the suffix ".tmp" will be used | ||
* @return An abstract pathname denoting a newly-created empty file | ||
* @throws AssertionError if temp file cannot be created | ||
*/ | ||
public static File createTempFile(String prefix, String suffix) { | ||
try { | ||
return File.createTempFile(prefix, suffix); | ||
} catch (IOException io) { | ||
AssertJUtil.fail("Failed to create temp file to due exception: %s", io.getMessage()); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
/** | ||
* Wrapper of FileUtils.writeByteArrayToFile that does not require handling of the exception | ||
* | ||
* @param file - the file to write to | ||
* @param data - the content to write to the file | ||
* @throws AssertionError if file cannot be written to | ||
*/ | ||
public static void writeFile(File file, byte[] data) { | ||
try { | ||
FileUtils.writeByteArrayToFile(file, data); | ||
} catch (IOException io) { | ||
AssertJUtil.fail("Failed to write file to due exception: %s", io.getMessage()); | ||
} | ||
} | ||
|
||
} |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
var targetUrl = arguments[0]; | ||
var headers = JSON.parse(arguments[1]); | ||
var result; | ||
|
||
function bufferToBase64(buf) { | ||
var binstr = Array.prototype.map.call(buf, function(ch) { | ||
return String.fromCharCode(ch); | ||
}).join(''); | ||
|
||
// Note: This method is proprietary to the DOM | ||
return btoa(binstr); | ||
} | ||
|
||
function getPromise() { | ||
return new Promise(function(resolve, reject) { | ||
var xhr = new XMLHttpRequest(); | ||
xhr.open("GET", targetUrl, true); | ||
xhr.responseType = "arraybuffer"; | ||
xhr.onload = function() { | ||
result = new Object(); | ||
result.status = this.status; | ||
result.file = bufferToBase64(new Uint8Array(this.response)); | ||
resolve(result); | ||
}; | ||
|
||
for (let header of headers) { | ||
xhr.setRequestHeader(header.name, header.value); | ||
} | ||
|
||
xhr.send(null); | ||
}); | ||
} | ||
|
||
async function makeSynchronousRequest() { | ||
// wait to http request to finish | ||
await getPromise(); | ||
|
||
// below code will be executed after http request is finished | ||
return JSON.stringify(result); | ||
} | ||
|
||
return makeSynchronousRequest(); |
Oops, something went wrong.