Proven way to Fake Webcam in Test Automation with Python

Posted by

This article explains how to test web application that use web cam. We use Python Splinter and fake a webcam device using Chrome web driver.

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 using a fake video. The complete source code along with the sample web application (using Python Flask) is available as a gitlab project.

Leave a Reply

Your email address will not be published. Required fields are marked *