Get Session Id

In cases where you want to uniquely identify the current test, you might want to use the session ID that BrowserStack generates automatically for each of your test runs.

You can use these sessions IDs to internally map with your test cases, or logging, or automatically raising a bug report.

You can use the WebDrivers’ API to retrieve BrowserStack’s session ID as follows:

// Start a remote browser with the desired capabilities
RemoteWebDriver driver = new RemoteWebDriver(new URL(URL), caps);

// ... run some tests

// Get the session ID
driver.getSessionId();

// ... do something with the session ID

// ... continue with your tests


The following snippet will work in most NodeJS based Selenium scripts barring a few test frameworks:

driver.session_.then(function(sessionData) {
    console.log(sessionData.id_);
});
  • If you are using NightwatchJS, then you can get session ID by the following snippet:
    browser.session(function(session) {
      console.log(session.sessionId);
    });
    
  • If you are using WebdriverIO, then you can get session ID by:
    console.log(browser.sessionId);
    
  • If you are using Protractor, then you can get session ID by:
    browser.driver.getSession().then(function(session) {
      console.log(session.getId());
    });
    

Note: TestCafe does not expose the Session ID to the test script code.

// For selenium version 2.49.0 and above refer -

RemoteWebDriver driver = new RemoteWebDriver(new Uri("https://hub-cloud.browserstack.com/wd/hub/"), capability);
string sessionId = driver.SessionId.ToString();

// For selenium version 2.48.2 and below -

var sessionIdProperty = typeof(RemoteWebDriver).GetProperty("SessionId", BindingFlags.Instance | BindingFlags.NonPublic);
SessionId sessionId = sessionIdProperty.GetValue(driver, null) as SessionId;
Console.WriteLine(sessionId.ToString());
$web_driver->getSessionID();
driver.session_id
driver.session_id
$driver->id();
Note:
  • The above script works for the language bindings and most test frameworks. However, some test frameworks do not expose the Session ID to the test script.
  • If you need Session ID for marking test status, you can also mark the test status using Javascript Executor without needing Session IDs for the same.

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