Skip to main content
Get Automate insights in Microsoft Teams with our new integration! Get Started!.

Get Session Details of Selenium Tests

A Selenium session running on BrowserStack generates data and logs such as console logs, Selenium logs, etc. BrowserStack provides a custom JavaScriptExecutor to get the details of your BrowserStack session as a JSON response.

In this guide, you’ll learn:

Get session details of a test

Add the following code snippets, after initializing the web driver in your test script, to get details of your ongoing BrowserStack test session:

import org.openqa.selenium.JavascriptExecutor;

// start a remote browser with the desired capabilities
RemoteWebDriver driver = new RemoteWebDriver(new URL(URL), caps);
// declare the JavascriptExecutor class
JavascriptExecutor jse = (JavascriptExecutor)driver;

// ... some lines of your test script

// store the JSON response in the Object class
Object response = jse.executeScript("browserstack_executor: {\"action\": \"getSessionDetails\"}");
// print the Session details in your IDE's console
System.out.println(response);

// ... continue with your tests
// write the test capabilities
// ...

// initialize the driver
async function runTestWithCaps(capabilities){
    let driver = new webdriver.Builder().usingServer(`https://YOUR_USERNAME:YOUR_ACCESS_KEY@hub-cloud.browserstack.com/wd/hub`).withCapabilities(capabilities).build();

    // get details of the session
    const response = await driver.executeScript('browserstack_executor: {"action": "getSessionDetails"}');
    // print session details in your IDE's console if required
    console.log(response);

await driver.quit();
}
// write the test capabilities
// ...

// initialize the driver
driver = new RemoteWebDriver(new Uri("https://hub-cloud.browserstack.com/wd/hub/"), capability);

// get details of the session
Object sessionObject= ((IJavaScriptExecutor)driver).ExecuteScript("browserstack_executor: {\"action\": \"getSessionDetails\"}");
// print JSON response in your IDE's console if required
Console.WriteLine(sessionObject);
// write the test capabilities
// ...

// initialize the driver
$driver = RemoteWebDriver::create("https://YOUR_USERNAME:YOUR_ACCESS_KEY@hub-cloud.browserstack.com/wd/hub",$caps);

// write the test script
// ...

// get details of the session
$response = $driver->executeScript('browserstack_executor: {"action": "getSessionDetails"}' );
// print the session details in the IDE's console if required
echo $response;
# import the JSON package
import json

# initialize the driver
driver = webdriver.Remote(command_executor='https://YOUR_USERNAME:YOUR_ACCESS_KEY@hub-cloud.browserstack.com/wd/hub',desired_capabilities=desired_cap)

# get details of the session
response = driver.execute_script('browserstack_executor: {"action": "getSessionDetails"}')
# print the session details in the IDE's console
print(response)
# initialize the driver
driver = Selenium::WebDriver.for(
    :remote,
    :url => "https://YOUR_USERNAME:YOUR_ACCESS_KEY@hub-cloud.browserstack.com/wd/hub",
    :capabilities => options
)

response = driver.execute_script('browserstack_executor: {"action": "getSessionDetails"}')
# print the session details in the IDE's console if required
puts response

The following sample JSON response, after running the JavaScriptExecutor code, is displayed in the Text Logs section of your Automate session:

{
  "name": "BStack-[Ruby] Sample Test",
  "duration": null,
  "os": "ios",
  "os_version": "14.3",
  "browser_version": null,
  "browser": null,
  "device": "iPhone 12 Pro Max",
  "status": "running",
  "hashed_id": "0c9d1fc296db316f9004b866bd6b8032fa95b292",
  "reason": null,
  "build_name": "BStack Build Number 1",
  "project_name": "Untitled Project",
  "build_hashed_id": "6ef2223dd4cdcb44e738bcbe3ed3867f8ed48158",
  "test_priority": null,
  "logs": "https://automate.browserstack.com/builds/6ef2223dd4cdcb44e738bcbe3ed3867f8ed48158/sessions/0c9d1fc296db316f9004b866bd6b8032fa95b292/logs",
  "browserstack_status": "running",
  "created_at": "2022-02-04T06:16:48.000Z",
  "browser_url": "https://automate.browserstack.com/builds/6ef2223dd9cdcb44e739bcbe3ed3867f8ed48158/sessions/0c9d1fc296db316f9004b866bd6b8032fa95b292",
  "public_url": "https://automate.browserstack.com/builds/6ef2223dd9cdcb44e739bcbe3ed3867f8ed48158/sessions/0c9d1fc296db316f9004b866bd6b8032fa95b292?auth_token=ef9819b57b8756bd82de841d91b74d2eda5a81609d1bcf53z5236bd5b8b89945",
  "video_url": "https://automate.browserstack.com/sessions/0c9d1fc296db316f9004b866bd6b8032fa95b292/video?token=UCFaMG9Yb1B3TDF5V1M2c0ZVLmpaUTFYYVUwcGRicGVabjRQVytZNjlpOXh2cnR3ZXczaGpQRjQ2RHVMelN3elJ4VUtqL1JWZjIabkdTcTFVdWZU00E9PS0tRU14V0VMRjRlNWNSbTI2U252QlZjZz09--b580fa72ff41aa2a8ef20571d7629b82eac9278e&source=rest_api&diff=-1",
  "browser_console_logs_url": "https://automate.browserstack.com/s3-upload/bs-selenium-logs-aps/s3.ap-south-1/0c9d1fc296db316f9004b866bd6b8032fa95b292/0c9d1fc296db316f9004b866bd6b8032fa95b292-console-logs-v2.txt?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIA2XUQHUQMCEH5SPMK%2F20220204%2Fap-south-1%2Fs3%2Faws4_request&X-Amz-Date=20220204T061657Z&X-Amz-Expires=604800&X-Amz-SignedHeaders=host&X-Amz-Signature=98d8c3312f04f0df2aecce5e3397d0aea6c3006384168c6ce256a954ed612795",
  "har_logs_url": "https://automate.browserstack.com/s3-upload/bs-selenium-logs-euw/s3.eu-west-1/0c9d1fc296db316f9004b866bd6b8032fa95b292/0c9d1fc296db316f9004b866bd6b8032fa95b292-har-logs.txt?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIA2XUQHUQMCEH5SPMK%2F20220204%2Feu-west-1%2Fs3%2Faws4_request&X-Amz-Date=20220204T061657Z&X-Amz-Expires=604800&X-Amz-SignedHeaders=host&X-Amz-Signature=c69531d040702e1fb5a05361d4381cad43p1e0cd940c3a485b0e76d9c57b7027",
  "selenium_logs_url": "https://automate.browserstack.com/s3-upload/bs-selenium-logs-euw/s3.eu-west-1/0c9d1fc296db316f9004b866bd6b8032fa95b292/0c9d1fc296db316f9004b866bd6b8032fa95b292-selenium-logs.txt?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIA2XUQHUQMCEH5SPMK%2F20220204%2Feu-west-1%2Fs3%2Faws4_request&X-Amz-Date=20220204T061657Z&X-Amz-Expires=604800&X-Amz-SignedHeaders=host&X-Amz-Signature=61a7b668c343ecdc049zd583210bd3410b0b1316c919e1175f24da2ef7991b19"
}

Check out the REST API responses of a session to learn about all the log URLs and other parameters of a session.

Use session ID of a test

BrowserStack generates a unique ID for each session. The hashed_id in the JSON response represents the session ID. You can use this hashed_id value along with the session REST API endpoint to perform actions on a session, such as updating the status of a session, getting Selenium logs of a session, etc.

Check out the session endpoint section to learn about the supported actions that you can perform using a session ID.

The following code snippet shows how to get the session details, and then further parse it to get the session ID to use in your test:

import org.openqa.selenium.JavascriptExecutor;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;

// start a remote browser with the desired capabilities
RemoteWebDriver driver = new RemoteWebDriver(new URL(URL), caps);
// declare the JavascriptExecutor class
JavascriptExecutor jse = (JavascriptExecutor)driver;

// ... some lines of your test script

// store the JSON response in the Object class
Object response = jse.executeScript("browserstack_executor: {\"action\": \"getSessionDetails\"}");
// parse the JSON response
JSONObject json = (JSONObject) new JSONParser().parse((String) response);
// store session ID in a variable
String sessionID = (String) json.get("hashed_id");
// print session ID in your IDE's console
System.out.println(sessionID);

// ... Modify session details using the session 'hased_id'
// ... continue with your tests
// write the test capabilities
// ...

// initialize the driver
async function runTestWithCaps(capabilities){
    let driver = new webdriver.Builder().usingServer(`https://YOUR_USERNAME:YOUR_ACCESS_KEY@hub-cloud.browserstack.com/wd/hub`).withCapabilities(capabilities).build();

    // get details of the session
    const response = await driver.executeScript('browserstack_executor: {"action": "getSessionDetails"}');
    // parse the JSON response
    const session_details = JSON.parse(response);
    // print session ID in your IDE's console
    console.log(session_details.hashed_id);

await driver.quit();
}

// ... Modify session details using the session 'hased_id'
// ... continue with your tests
// write the test capabilities
// ...

// initialize the driver
driver = new RemoteWebDriver(new Uri("https://hub-cloud.browserstack.com/wd/hub/"), capability);

// get details of the session
Object sessionObject= ((IJavaScriptExecutor)driver).ExecuteScript("browserstack_executor: {\"action\": \"getSessionDetails\"}");
// convert Object to String for parsing
string json_resp = Convert.ToString(sessionObject);
// parse the data
var session_details = JObject.Parse(json_resp);
// print the session ID on IDE's console
Console.WriteLine(session_details["hashed_id"]);

// ... Modify session details using the session 'hased_id'
// ... continue with your tests
// write the test capabilities
// ...

// initialize the driver
$driver = RemoteWebDriver::create("https://YOUR_USERNAME:YOUR_ACCESS_KEY@hub-cloud.browserstack.com/wd/hub",$caps);

// write the test script
// ...

// get details of the session
$response = $driver->executeScript('browserstack_executor: {"action": "getSessionDetails"}' );
$session_details = json_decode($response);

// print the session ID in the IDE's console
echo $session_details->hashed_id;

// ... Modify session details using the session 'hased_id'
// ... continue with your tests
# import the JSON package
import json

# initialize the driver
driver = webdriver.Remote(command_executor='https://YOUR_USERNAME:YOUR_ACCESS_KEY@hub-cloud.browserstack.com/wd/hub',desired_capabilities=desired_cap)

# get details of the session
response = driver.execute_script('browserstack_executor: {"action": "getSessionDetails"}')
jsonResponse = json.loads(response)

# print the session ID in the IDE's console
print(jsonResponse["hashed_id"])

# ... Modify session details using the session 'hased_id'
# ... continue with your tests
# initialize the driver
driver = Selenium::WebDriver.for(
    :remote,
    :url => "https://YOUR_USERNAME:YOUR_ACCESS_KEY@hub-cloud.browserstack.com/wd/hub",
    :capabilities => options
)

# get details of the session
response = driver.execute_script('browserstack_executor: {"action": "getSessionDetails"}')
session_details = JSON.parse(response)

# print the session ID in the IDE's console
puts session_details["hashed_id"]

# ... Modify session details using the session 'hased_id'
# ... continue with your tests

We're sorry to hear that. Please share your feedback so we can do better

Contact our Support team for immediate help while we work on improving our docs.

We're continuously improving our docs. We'd love to know what you liked






Thank you for your valuable feedback

Is this page helping you?

Yes
No

We're sorry to hear that. Please share your feedback so we can do better

Contact our Support team for immediate help while we work on improving our docs.

We're continuously improving our docs. We'd love to know what you liked






Thank you for your valuable feedback!

Talk to an Expert
Talk to an Expert