Skip to content

Instantly share code, notes, and snippets.

@mireshsonkamble
Created April 25, 2020 20:13
Show Gist options
  • Select an option

  • Save mireshsonkamble/ee61e353d5cd7ce517d69f1eee7ba01a to your computer and use it in GitHub Desktop.

Select an option

Save mireshsonkamble/ee61e353d5cd7ce517d69f1eee7ba01a to your computer and use it in GitHub Desktop.
package BStackTest;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import org.json.JSONObject;
import org.apache.commons.codec.binary.Base64;
public class sample {
public static void main(String[] args) throws Exception {
sample http = new sample();
http.sendGet();
}
private void sendGet() throws Exception {
String userPassword = "<USERNAME>:<PASSWORD>";
String encoding = new String(Base64.encodeBase64(userPassword.getBytes()));
String url = "https://api.browserstack.com/automate/sessions/<session_id>.json";
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestProperty("Authorization", "Basic " + encoding);
con.setRequestMethod("GET");
BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
JSONObject jsonObject = new JSONObject(response.toString());
JSONObject jsonObject2 = new JSONObject(jsonObject.get("automation_session").toString());
String browser_url=jsonObject2.getString("browser_url");
String[] parts = browser_url.split("https://automate.browserstack.com/builds/");
String[] build = parts[1].split("/sessions/");
String build_id= build[0].toString();
System.out.println(build_id);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment