Automation testing of web application with fake webcam

fake_web_cam

Automation testing is the buzz word in today’s World. Everybody is trying to upgrade their skills and learning automation testing. In one or our projects we have to do automation testing of a web application that uses web cam. Another challenge is, we have to test that application from multiple geographical locations because it supports multiple languages. Setting up a machine with web cam is difficult to maintain and hence we started looking at options to fake webcam. This post will explain how to do that using Chrome web driver and Python Splinter.

Fake webcam options in Chrome

Chrome web driver supports ChromeOptions where we can customize the Chrome browser under automation. There are few options which will simulate fake webcam. Below are the list of the options we have used to fake the webcam:

chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("--use-fake-device-for-media-stream")
chrome_options.add_argument("--use-file-for-fake-video-capture=./salma_hayek.y4m")
chrome_options.add_experimental_option("prefs", {
    "profile.default_content_setting_values.media_stream_camera": 1
})

We have used Python Splinter framework to do the automation.

from splinter import Browser
from selenium import webdriver
import time

chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("--use-fake-device-for-media-stream")
chrome_options.add_argument("--use-file-for-fake-video-capture=./salma_hayek.y4m")
chrome_options.add_experimental_option("prefs", {
    "profile.default_content_setting_values.media_stream_camera": 1
})
browser = Browser('chrome', options=chrome_options)
browser.visit("http://127.0.0.1:5000/")
time.sleep(10)
browser.quit()

I have created a sample web application using Python Flask framework to publish a HTML with webcam. You can download the source code from here.

Fake webcam video

In the above code snippet, in line #4, you see a file named, “salma_hayek.y4m“. So, what is this y4m file? y4m is an uncompressed video format with images frame by frame. The next question will be how do we create this video format? We can create y4m videos from mp4 videos using ffmpeg. But, keep in mind, y4m is an uncompressed format and hence will be huge in size when compared to mp4. So, always keep your mp4 files small. From my experience, even 2 seconds of mp4 video will give you 40MB of y4m video. This link will explain how to use ffmpeg to convert mp4 to y4m format. If you are an Ubuntu user, you can create mp4 videos with Openshot video editor.

Conclusion

We have successfully automated the testing of web applications that use webcam. We have used free proxies from different countries to even test the multi language feature. This application even works with headless mode so that you can deploy the application on cloud providers like Amazon, Google Cloud and Azure. Thanks for reading.

Comments

  1. Alta

    Hi Saysiam,

    Thanks for the article and solution.

    We are now trying to come up with a solution to test a sequence of 4 RANDOM videos.
    And this brings me here to consult with you as the fastest way 🙂

    Thanks in advance!

    1. Post
      Author

Leave a Reply

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