Selenium C# – Web Cam Access Test

Selenium C# – Web Cam Access Test
Introduction
Automating browser-based webcam access is helpful for testing video input features in web applications. With ChromeOptions, Selenium lets you simulate webcam access using fake media streams. In this guide, I'll show you how to access a webcam test site and simulate webcam usage with Selenium in C#.
Code Implementation
Below is a C# Selenium script that opens a browser, navigates to a webcam test site, and simulates webcam access using ChromeOptions.
Example: Web Cam Access with Selenium in C#.
csharpCopyEditusing Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Interactions;
using OpenQA.Selenium.Support.UI;
using SeleniumExtras.WaitHelpers;
using System.Globalization;
namespace SACSSAuto.QA.SeleniumPractice
{
[TestClass]
public class Selenium_82
{
/****
* Sample Code Definition :- Selenium Test - Browser - Web Cam Access
* Developed By :- Pankaj Kumar
* Date of Creation :- 31-Jan-2025
* Project :- SACSSAUTO - C# [Web / Mobile / API]
*/
[TestMethod]
public void Selenium_Test()
{
Console.WriteLine("Selenium Test - Browser - Web Cam Access");
// Configure ChromeOptions to pass fake media stream
ChromeOptions options = new ChromeOptions();
options.AddArgument("use-fake-device-for-media-stream");
options.AddArgument("use-fake-ui-for-media-stream");
// Initialize Chrome WebDriver with options
IWebDriver driver = new ChromeDriver(options);
// Maximize the browser window
driver.Manage().Window.Maximize();
// Set implicit wait time
driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(30);
// Open the webcam test website
driver.Navigate().GoToUrl("https://webcamtests.com/check");
// Wait for 10 seconds
Thread.Sleep(10000);
// Click the \"Test my cam\" button
driver.FindElement(By.Id("webcam-launcher")).Click();
// Wait for another 10 seconds
Thread.Sleep(10000);
// Quit the driver
driver.Quit();
}
}
}
Steps Explained
1. Setting Up Fake Media Stream
ChromeOptions is configured to simulate webcam access without a real device.
2. Navigating to Webcam Test Site
The browser opens the test website and waits for it to fully load.
3. Starting Webcam Test
Clicks the “Test my cam” button to simulate webcam activation.
4. Clean Exit
The browser session is properly closed after the test.
Expected Outcome
Opens the webcam test website.
Simulates webcam usage using fake media stream.
Initiates webcam test via button click.
Closes the browser session.
Conclusion
Simulating webcam access in Selenium using ChromeOptions is a handy way to automate video input tests. This example shows you how to access and interact with webcam features in a browser, all without needing an actual webcam device.
Subscribe to my newsletter
Read articles from Pankaj Kumar directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
