---
title: Fake Webcam and Microphone in Chrome for Selenium Tests
description: How to feed Chrome a fake webcam and microphone during automated tests,
  so Selenium, Puppeteer, Playwright and Cypress can drive media features.
source_url:
  html: https://testingbot.com/resources/articles/fake-webcam-microphone-chrome
  md: https://testingbot.com/resources/articles/fake-webcam-microphone-chrome.md
---

![Fake Webcam and Microphone in Chrome for Selenium Tests](https://testingbot.com/assets/resources/articles/40.webp)

# Fake webcam and microphone input during automated testing

How to feed Chrome a fake webcam and microphone during automated tests, so Selenium, Puppeteer, Playwright and Cypress can drive media features.

By [Jochen D.](https://testingbot.com/about/jochen-d)2024-06-27

 Share on Facebook 

 Share on Twitter 

 Post on Reddit 

 Share link 

Chrome allows to specify a fake Webcam and Microphone which can be used during automated tests with Selenium, Puppeteer, Playwright or Cypress.

- [Faking a Webcam with Chrome](https://testingbot.com#webcam)
- [Faking a Microphone with Chrome](https://testingbot.com#microphone)
- [Running automated tests with fake camera and microphone input](https://testingbot.com#automated)

## Faking a Webcam with Chrome

During automated or manual testing, you might want to simulate a webcam input in Google Chrome. This can be particularly useful for testing functionality that requires a webcam.

Google Chrome provides a handy command-line flag `--use-file-for-fake-video-capture` that allows you to use a video file as a fake webcam input.

When your browser navigates to a website that wants to use your Webcam, the website will be tricked into thinking that the video file specified with the flag is a webcam feed.

### Y4M file

Chrome allows you to specify a `.y4m` file, which is a raw video file that can be created with ffmpeg from any existing video file, such as a MP4 file:

```bash
ffmpeg -i video.mp4 -pix_fmt yuv420p selfie.y4m
```

Because this is a raw video file, the file size might get very large. A different approach is to use a mjpeg video file, which is smaller in filesize.

### MJPEG video file

`--use-file-for-fake-video-capture` also accepts a path to a `.mjpeg` file, which you can create with ffmpeg or another video encoder.

```bash
ffmpeg -i video.mp4 selfie.mjpeg
```

Make sure to only embed the video track in the mjpeg file, you can check with ffmpeg that only the videotrack is included, or use ImageMagick's `identify`

```bash
ffmpeg -i selfie.mjpeg
```

```bash
identify selfie.mjpeg
```

Identify should show a `JPEG` type for this file.

### Fake device for Media Stream

During automated testing, you might want to automatically allow a website to access your camera and microphone. This can be achieved by specifying the `--use-fake-ui-for-media-stream` commandline flag.

## Faking a Microphone with Chrome

Similar to [faking a webcam](https://testingbot.com#webcam) we can also inject a dummy audio file and make any website or app believe it is a microphone feed.

Google Chrome provides a command-line flag `--use-file-for-fake-audio-capture` which allows you to pass it a file path to an audio file.

This audio file will be played when a website listens to your microphone feed. This can be very useful when testing multimedia websites or apps that require input from a user by voice.

During the test, when the website under test listens to your microphone, it will receive the audio file feed. You can have a pre-recorded `.wav` audio file, Chrome will inject this as a fake microphone input and make the website believe it's from a real microphone.

### WAV file

The audio file that needs to be supplied needs to be a `WAV` file with 4.1 kHz stereo 16 bit.

If you have an existing video with audio, you can simply extract this into a separate wav audio file:

```bash
ffmpeg -i video.mp4 selfie.wav
```

## Running automated tests with fake camera and microphone input

Now that we know how to [fake a webcam feed](https://testingbot.com#webcam) and a [microphone feed](https://testingbot.com#microphone), we can combine this in one automated Selenium test and run this on the TestingBot browser grid.

In the following example, we will combine this into one test. At the start of the test, we specify a [uploadMultiple TestingBot capability](https://testingbot.com/support/web-automate/selenium/test-options#uploadMultiple") that uploads multiple files (a video and audio file) to the remote machine.

> Make sure you upload the video/audio file to a publicly available URL and that that the `content-type` is correctly set (`image/jpeg` or `audio/wav`).

The browser will use both files as fake input. We will then navigate to a website that tests whether your webcam works. The website will be tricked into thinking the webcam feed is showing the injected video file.

 ![Webcam injection example](https://testingbot.com/assets/resources/articles/images/webcam-test.webp)

```ruby
require "rubygems"
require "selenium-webdriver" 

tb_options = {
  "name" => "My Webcam Test",
  "browserName" => "chrome",
  "platform" => "WIN10",
  "version" => "latest",
  "uploadMultiple" => [
    { url: 'https://pub-0c7a5d7f43dc46d69f0b2ff9aa6c7c91.r2.dev/selfie.mjpeg', filePath: 'C:\\test\\selfie.mjpeg' },
    { url: 'https://pub-0c7a5d7f43dc46d69f0b2ff9aa6c7c91.r2.dev/selfie.wav', filePath: 'C:\\test\\selfie.wav' }
  ]
}

options = Selenium::WebDriver::Chrome::Options.new
options.add_argument('--use-fake-device-for-media-stream')
options.add_argument('--use-fake-ui-for-media-stream')
options.add_argument('--use-file-for-fake-video-capture=C:\\test\\selfie.mjpeg')
options.add_argument('--use-file-for-fake-audio-capture=C:\\test\\selfie.wav')
options.add_option('tb:options', tb_options)

driver = Selenium::WebDriver.for(
  :remote,
  :url => "https://key:secret@hub.testingbot.com/wd/hub",
  :capabilities => options)
driver.navigate.to "https://webcamtests.com/"

wait = Selenium::WebDriver::Wait.new(timeout: 10)
consent_button = wait.until {
  element = driver.find_element(css: '.fc-cta-consent')
  element if element.displayed?
}
consent_button.click

sleep 5

webcam_allow_button = wait.until {
  element = driver.find_element(:id, 'webcam-launcher')
  element if element.displayed? && element.enabled?
}
webcam_allow_button.click

sleep 10
driver.quit
```

## Sidebar

### TestingBot Cloud Testing

Run automated, manual and visual tests on remote browsers and devices. Sign up for a free trial.

[Free Trial](https://testingbot.com/users/sign_up)

### Latest articles

[![Javascript Test Frameworks: Top 15](https://testingbot.com/assets/resources/articles/37-807810cc7f5d4bcc71dae6cdb137625fe748e38e0194033318d7b269de8ef407.webp)](https://testingbot.com/resources/articles/top-15-javascript-testing-frameworks)

#### [Javascript Test Frameworks: Top 15](https://testingbot.com/resources/articles/top-15-javascript-testing-frameworks)

The 15 most popular JavaScript testing frameworks ranked,...

[Read article →](https://testingbot.com/resources/articles/top-15-javascript-testing-frameworks)

[![Automated Browser Extension Testing](https://testingbot.com/assets/resources/articles/36-0533b9ac97e8fa44703058e149be135278cb5e1a9e29c273eb37b7c70c47c973.webp)](https://testingbot.com/resources/articles/automated-browser-extension-testing)

#### [Automated Browser Extension Testing](https://testingbot.com/resources/articles/automated-browser-extension-testing)

How to install and drive a browser extension end to end, ...

[Read article →](https://testingbot.com/resources/articles/automated-browser-extension-testing)

[![The best Python Web Testing frameworks](https://testingbot.com/assets/resources/articles/35-027df4e213761b025d3d612772682c8f7477b3adecaec14083f32c6c3bbec93d.webp)](https://testingbot.com/resources/articles/python-top-5-test-frameworks)

#### [The best Python Web Testing frameworks](https://testingbot.com/resources/articles/python-top-5-test-frameworks)

Five Python test frameworks worth knowing, PyTest, unitte...

[Read article →](https://testingbot.com/resources/articles/python-top-5-test-frameworks)

## Other Articles

[![Javascript Test Frameworks: Top 15](https://testingbot.com/assets/resources/articles/37-807810cc7f5d4bcc71dae6cdb137625fe748e38e0194033318d7b269de8ef407.webp)](https://testingbot.com/resources/articles/top-15-javascript-testing-frameworks "Javascript Test Frameworks: Top 15")

### [Javascript Test Frameworks: Top 15](https://testingbot.com/resources/articles/top-15-javascript-testing-frameworks)

The 15 most popular JavaScript testing frameworks ranked, with the features, pros and cons of each, from Jest and Mocha through to Cypress.

[Read article →](https://testingbot.com/resources/articles/top-15-javascript-testing-frameworks)

[![Automated Browser Extension Testing](https://testingbot.com/assets/resources/articles/36-0533b9ac97e8fa44703058e149be135278cb5e1a9e29c273eb37b7c70c47c973.webp)](https://testingbot.com/resources/articles/automated-browser-extension-testing "Automated Browser Extension Testing")

### [Automated Browser Extension Testing](https://testingbot.com/resources/articles/automated-browser-extension-testing)

How to install and drive a browser extension end to end, with working examples for Selenium, Puppeteer and Playwright on the major browsers.

[Read article →](https://testingbot.com/resources/articles/automated-browser-extension-testing)

[![The best Python Web Testing frameworks](https://testingbot.com/assets/resources/articles/35-027df4e213761b025d3d612772682c8f7477b3adecaec14083f32c6c3bbec93d.webp)](https://testingbot.com/resources/articles/python-top-5-test-frameworks "The best Python Web Testing frameworks")

### [The best Python Web Testing frameworks](https://testingbot.com/resources/articles/python-top-5-test-frameworks)

Five Python test frameworks worth knowing, PyTest, unittest, Robot Framework, Behave and Nose, and how each one pairs with Selenium.

[Read article →](https://testingbot.com/resources/articles/python-top-5-test-frameworks)

[![Cucumber testing with Selenium and IntelliJ or Eclipse](https://testingbot.com/assets/resources/articles/34-aa3115db36ed77849b9407f6d941da71f058c07bcee8a0fe4e2c19fd59726ba4.webp)](https://testingbot.com/resources/articles/cucumber-eclipse-intellij "Cucumber testing with Selenium and IntelliJ or Eclipse")

### [Cucumber testing with Selenium and IntelliJ or Eclipse](https://testingbot.com/resources/articles/cucumber-eclipse-intellij)

How to run Cucumber and Selenium tests straight from IntelliJ or Eclipse, with worked examples for both IDEs and for Cucumber in JavaScript.

[Read article →](https://testingbot.com/resources/articles/cucumber-eclipse-intellij)

## Ready to start testing?
[Start a free trial](https://testingbot.com/users/sign_up)
