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

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

Hey Emma,

Hope you are doing great.

To start a remote browser with the desired capabilities and get the session ID, you can use TestCafe’s browser provider mechanism and a service like LambdaTest. Here’s how you can achieve it:

Here’s how you can do it:

Set up LambdaTest: Ensure you have an account with LambdaTest and obtain your access credentials.

Configure LambdaTest with TestCafe: Configure TestCafe to use LambdaTest as a remote browser provider by leveraging LambdaTest’s Selenium grid.

Run your TestCafe tests: Run your TestCafe tests specifying LambdaTest capabilities and obtain the session ID.

Below is a basic example of how you could implement this:

const createTestCafe = require('testcafe');
const { execSync } = require('child_process');

(async () => {
    // Start LambdaTest tunnel
    execSync('LT_USERNAME=<YOUR_USERNAME> LT_ACCESS_KEY=<YOUR_ACCESS_KEY> ltc tunnel', { stdio: 'inherit' });

    // Wait for tunnel to be ready
    await new Promise(resolve => setTimeout(resolve, 10000)); // Adjust the wait time as needed

    const testcafe = await createTestCafe('localhost', 1337, 1338);
    const runner = testcafe.createRunner();

    try {
        const remoteBrowserConnection = await runner
            .src('path/to/your/test/file.js') // Replace with the path to your test file
            .browsers('remote:') // Use LambdaTest as the remote browser provider

        const sessionId = remoteBrowserConnection.browserConnection.browserInfo.sessionId;
        console.log('Session ID:', sessionId);

        // Perform your test actions here

    } finally {
        await testcafe.close();

    // Stop LambdaTest tunnel
    execSync('lt tunnel --stop', { stdio: 'inherit' });

Replace <YOUR_USERNAME> and <YOUR_ACCESS_KEY> with your actual LambdaTest username and access key.

Replace ‘path/to/your/test/file.js’ with the path to your TestCafe test file.

Ensure that your test file contains valid TestCafe tests that you want to run on the LambdaTest remote browser.

To learn more about TestCafe follow this detailed tutorial and get valuable insights wth examples.