Created
May 8, 2018 00:17
-
-
Save sauceaaron/9c25ab50e7885b89ab2a9d069f526782 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| package saucelabs; | |
| import com.saucelabs.saucerest.SauceREST; | |
| import org.apache.commons.io.FileUtils; | |
| import org.apache.commons.io.IOUtils; | |
| import java.io.File; | |
| import java.io.IOException; | |
| import java.io.InputStream; | |
| import java.net.URL; | |
| import java.net.URLConnection; | |
| import java.nio.charset.Charset; | |
| import java.util.Base64; | |
| public class SauceRESTWithHARFile extends SauceREST | |
| { | |
| public SauceRESTWithHARFile(String username, String accessKey) | |
| { | |
| super(username, accessKey); | |
| } | |
| public String getHarFileContents(String sessionId) throws IOException | |
| { | |
| String harFileLocation = "https://eds.saucelabs.com/SESSION_ID/network.har".replace("SESSION_ID", sessionId); | |
| // set up request | |
| URL url = new URL(harFileLocation); | |
| URLConnection connection = url.openConnection(); | |
| // add user / access key authorization header | |
| String basic64encodedCredentials = Base64.getEncoder().encodeToString((username + ":" + accessKey).getBytes()); | |
| connection.setRequestProperty("Authorization", "Basic " + basic64encodedCredentials); | |
| // read response | |
| InputStream inputStream = connection.getInputStream(); | |
| String harFileContents = IOUtils.toString(inputStream, Charset.defaultCharset()); | |
| inputStream.close(); | |
| return harFileContents; | |
| } | |
| public void saveHarFile(String sessionId, File file) throws IOException | |
| { | |
| String harFileContents = getHarFileContents(sessionId); | |
| FileUtils.writeStringToFile(file, harFileContents, Charset.defaultCharset()); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment