Selenium C# – Mic Access Automation

Selenium C# – Mic Access Automation
Introduction
Automating microphone access in Selenium is super helpful for testing apps that need audio input. In this example, I'll show you how to simulate microphone access using Chrome's browser capabilities with fake media streams for testing.
Code Implementation
Below is a C# Selenium script that configures Chrome to simulate a microphone input and accesses a mic test page.
Example: Mic Access with Selenium in C#.
using 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_81
{
/****
* Sample Code Definition :- Selenium - Mic 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 - Mic 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();
// Open the URL
driver.Navigate().GoToUrl("https://www.vidyard.com/mic-test/");
// Click the "Start Mic Test" button
driver.FindElement(By.XPath("//button[normalize-space()='Start mic Test']")).Click();
// Wait for 10 seconds
Thread.Sleep(10000);
// Wait for 5 seconds
Thread.Sleep(5000);
//Quit the driver
driver.Quit();
}
}
}
Steps Explained
1. Chrome Options Configuration
The script uses ChromeOptions
to simulate a fake mic input, allowing the test to proceed without manual permission prompts.
2. Navigation and Interaction
The browser opens the Vidyard mic test page and clicks the "Start Mic Test" button.
3. Wait and Close
After waiting for the test duration, the browser session is closed.
Expected Outcome
Opens the mic test site.
Starts the mic test using fake audio input.
Waits to simulate mic input duration.
Closes the browser.
Conclusion
By using Chrome's fake media stream features, Selenium makes it easy to automate and test microphone access without needing real hardware or user input.
Subscribe to my newsletter
Read articles from Pankaj Kumar directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
