How to get session ID of a running test via script?

How can I get session ID of a running test via script?

Hello Tim,

To get the session ID of a running test via script and start a remote browser with the desired capabilities, you can use the Selenium WebDriver library in JavaScript. Here’s an example using WebDriver and Node.js:

First, make sure you have installed the necessary dependencies:

npm install selenium-webdriver

Then, you can use the following script:

const { Builder } = require('selenium-webdriver');

// LambdaTest credentials
const LT_USERNAME = 'your_username';
const LT_ACCESS_KEY = 'your_access_key';

// Desired capabilities for LambdaTest
const capabilities = {
    browserName: 'chrome', // or any other browser supported by LambdaTest
    platform: 'win10', // or any other platform supported by LambdaTest
    version: 'latest', // or any other browser version supported by LambdaTest
    name: 'Your Test Name' // your test name
};

(async () => {
    // Create Selenium WebDriver instance with LambdaTest capabilities
    const driver = await new Builder()
        .usingServer(`https://${LT_USERNAME}:${LT_ACCESS_KEY}@hub.lambdatest.com/wd/hub`)
        .withCapabilities(capabilities)
        .build();

    try {
        // Get session ID
        const sessionId = await driver.getSession().getId();
        console.log('Session ID:', sessionId);

        // Perform your test actions here
        
    } finally {
        // Quit the driver
        await driver.quit();
    }
})();

Replace ‘your_username’ and 'your_access_key’ with your actual LambdaTest username and access key. Adjust the desired capabilities according to your requirements.

I hope this helped.