Proven way to Fake Webcam in Test Automation with Python

P

We automate web application testing using Python Selenium or Splinter. How will you automate testing applications with webcam? You can fake a webcam device with Chrome Web driver. In this article we will see how to do that.

Challenge

The challenge here is to fake the webcam with proper video format. In this post I am using Chrome browser along with Splinter. If you are new to Splinter, check out my article and I promise you love it. We use Splinter not only for testing but also for web scraping. Chrome need Y4M format to replace the camera feed. To generate that format you need ffmpeg tool. Using that tool you can convert any MP4 video into Y4M format.

Y4M is an un-compressed format. The converted Y4M file size will be almost three times the MP4 file. So, take a small MP4 file or repeat the image to create the video.

Once you have the Y4M file you can follow the below mentioned steps to fake the webcam. Here is an article on how to create Y4M files using ffmpeg.

Chrome Settings to fake webcam

We will set webdriver.ChromeOptions to enable media stream using a fake device.

Conclusion

You are all set to test the web applications uing a fake video. The complete source code along with the sample web application (using Python Flask) is available as a gitlab project.

comments powered by Disqus

I am Saisyam

I am passionate software developer. I love exploring new technologies and programming languages. Other than coding I love cooking, travelling and photography.

Get in touch

Talk to me about anything related to programming and technology. Happy to help.