audiochromiumplaywrightmicrophone

How can I send audio to fake mic input using Playwright on Chromium?


I am working on some automation and want to send a wav or mp3 file to the mic input on the browser. I've already found all of the command line switches for Chromium and I have the 'microphone' permission set on the Playwright browser context.

The UI correctly displays 'fake input device' for the mic so that part appears to be working.

The UI I'm testing has a [Test] button to start recording from the mic. Does anyone have any ideas on how to pipe my .wav file through the fake mic?

I've tried assigning the file (by using SetInputFilesAsync()) but that doesn't appear to work. I was thinking I could jump on the event but I still don't know how to pass the file in.

Does anyone know if it's possible to send an audio file to a fake input device? Thank you in advance!


Solution

  • I was working with the same task recently and I hope my code example can help you. You need to use a few specific parameters while launching your browser:

    from playwright.sync_api import expect
    from playwright.sync_api import Playwright
    import time
    
    def test_web_mic(playwright: Playwright):
        browser = playwright.chromium.launch(headless=False,
                                             args=[
                                                 # use Chrome's fake media streams
                                                 "--use-fake-device-for-media-stream",
                                                 # bypasses Chrome's cam/mic permissions dialog
                                                 "--use-fake-ui-for-media-stream",
                                                 # pass in your own custom media
                                                 "--use-file-for-fake-audio-capture=C:\\filepath\\audio.wav"]
                                             )
        context = browser.new_context()
        context.grant_permissions(permissions=["microphone"])
        page = context.new_page()
        page.goto("https://mictests.com/")
        page.get_by_role("button", name="Test my mic").click()
        time.sleep(40)
        page.get_by_role("button", name="Stop microphone").click()
        page.pause()
    
        context.close()
        browser.close()