How to get session ID of a running test via script in JS-Mocha?
Hey Toby,
To start a remote browser with the desired capabilities based on the LambdaTest platform and get the session ID of a running test using JavaScript with Mocha, you can utilize the LambdaTest JavaScript SDK along with Selenium WebDriver. Here’s a basic example:
const { remote } = require('webdriverio');
const LambdaTest = require('@lambdatest/node');
// LambdaTest credentials
const LT_USERNAME = 'your_username';
const LT_ACCESS_KEY = 'your_access_key';
// LambdaTest configuration
const lambdatestClient = new LambdaTest(LT_USERNAME, LT_ACCESS_KEY);
// Selenium WebDriver configuration for LambdaTest
const lambdatestConfig = {
user: LT_USERNAME,
key: LT_ACCESS_KEY,
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 () => {
// Start session
const browser = await remote(lambdatestConfig);
// Get session ID
const sessionId = await browser.sessionId;
console.log('Session ID:', sessionId);
// Perform your test actions here
// End session
await browser.deleteSession();
// Print session status
const sessionDetails = await lambdatestClient.getSessions(sessionId);
console.log('Session Status:', sessionDetails[0].status);
})();
Make sure to replace ‘your_username’ and ‘your_access_key’ with your actual LambdaTest username and access key. Adjust the desired capabilities according to your requirements.