Skip to content

Instantly share code, notes, and snippets.

@j4gd33p
Last active June 10, 2025 04:48
Show Gist options
  • Select an option

  • Save j4gd33p/d74b6c3053c30ba21fc41fed1bc8dd68 to your computer and use it in GitHub Desktop.

Select an option

Save j4gd33p/d74b6c3053c30ba21fc41fed1bc8dd68 to your computer and use it in GitHub Desktop.
Play External Video using Selenium in webRTC webApp
CHECK FIRST COMMENT
@j4gd33p
Copy link
Author

j4gd33p commented Oct 24, 2024

To use Selenium to play external video in different sessions (like testRTC does), you'll need to automate the browser sessions and inject the external video source into a WebRTC session using the MediaStream API. Here's a step-by-step guide to simulate this:

1. Set Up Your WebRTC App to Handle External Video Streams

First, modify your WebRTC app to accept an external video stream, either by using the MediaStream API or by controlling the video elements in the browser.

Here’s a sample of injecting an external video source in a WebRTC session using JavaScript:

async function injectExternalVideo(videoUrl) {
    const videoElement = document.createElement('video');
    videoElement.src = videoUrl; // External video URL
    videoElement.autoplay = true;
    videoElement.muted = true; // Mute to avoid feedback
    await videoElement.play();

    const mediaStream = videoElement.captureStream(); // Capture video stream
    return mediaStream;
}

// Add the external video stream to a WebRTC PeerConnection
async function addExternalStreamToPeerConnection(peerConnection, videoUrl) {
    const mediaStream = await injectExternalVideo(videoUrl);
    mediaStream.getTracks().forEach(track => peerConnection.addTrack(track, mediaStream));
}

2. Install Selenium WebDriver

You’ll need Selenium and the browser-specific WebDriver (for example, ChromeDriver) to automate the browser sessions.

  1. Install Selenium in your Node.js project (or Python if you're using Python):

    npm install selenium-webdriver
  2. Download ChromeDriver or the relevant WebDriver for your browser:

3. Write Selenium Script to Automate Multiple Sessions

Now, you can automate multiple browser sessions with Selenium and inject the external video into different users (sessions) joining the WebRTC room.

Here’s an example using Node.js and Selenium WebDriver:

Example: Automating Two Sessions with External Video

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

// Function to simulate a WebRTC session with an external video stream
async function startWebRTCSession(driver, videoUrl, roomUrl) {
    // Navigate to the WebRTC app
    await driver.get(roomUrl);

    // Inject external video stream into the WebRTC session
    await driver.executeScript(`(${injectExternalVideo.toString()})('${videoUrl}')`);

    // Simulate actions like joining a room, etc.
    // Example: Click join room button
    await driver.findElement(By.id('joinRoomButton')).click();
}

async function runTest() {
    // Start first session
    let driver1 = await new Builder().forBrowser('chrome').build();

    // Start second session
    let driver2 = await new Builder().forBrowser('chrome').build();

    try {
        // External video file or stream URL
        const videoUrl = 'https://path-to-external-video.mp4';
        const roomUrl = 'https://your-webrtc-app-url.com/room';

        // First user joins with an external video stream
        await startWebRTCSession(driver1, videoUrl, roomUrl);

        // Second user joins the same WebRTC room (simulating another peer)
        await startWebRTCSession(driver2, videoUrl, roomUrl);

        // Wait for testing (e.g., give them time to stream)
        await driver1.sleep(15000); // Let the sessions run for 15 seconds
    } finally {
        // Close browsers after test
        await driver1.quit();
        await driver2.quit();
    }
}

// Start the WebRTC test
runTest();

4. Run the Selenium Script

  1. Save the script to a file, e.g., webrtc-test.js.
  2. Run the script:
    node webrtc-test.js

5. Analyze the Sessions

You can now analyze how the external video stream is handled by your WebRTC app. If you want to run multiple sessions (like testRTC does), you can increase the number of browser instances (e.g., driver1, driver2, driver3, etc.) and simulate multiple peers in the WebRTC room.

6. Capture WebRTC Stats (Optional)

If you need detailed WebRTC stats, you can capture them using getStats() in the browser or by accessing the WebRTC internals via chrome://webrtc-internals (in Chrome) to measure performance, latency, packet loss, etc.

Key Points:

  • Injecting external video: Use the captureStream() method to stream an external video as a media stream into the WebRTC session.
  • Automating with Selenium: Automate multiple browser sessions with Selenium WebDriver and control WebRTC actions like joining rooms and streaming video.
  • Multiple sessions: Create as many browser instances as needed to simulate different users (like testRTC’s parallel WebRTC sessions).

By automating your WebRTC app with Selenium, you can create a flexible, customizable solution that simulates multiple users with external media streams.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment