Search code examples
pythone2e-testingplaywright

How to handle chromium microphone permission pop-ups in playwright?


What I'm trying to do

Test a website that requires microphone access with playwright

The problem

Pop-up in question comes up and seems to ignore supposedly granted permissions. Permission can be given manually, but this seems against the spirit of automation.

enter image description here

What I tried

with sync_playwright() as p:

    browser = p.chromium.launch(headless=False)
    context = browser.new_context(permissions=['microphone'])
...

Granting permissions via context doesn't work for some reason. The permission pop-up still comes up.

I also tried to record a walkthrough with playwrights record script, but it's not recording granting microphone permissions.


Solution

  • You're missing some command line flags that tell chrome to simulate having a microphone. Give this sample a shot.

    from playwright.sync_api import sync_playwright
    
    
    def run(playwright):
        chromium = playwright.chromium
        browser = chromium.launch(headless=False, args=['--use-fake-device-for-media-stream', '--use-fake-ui-for-media-stream'])
        context = browser.new_context()
        context.grant_permissions(permissions=['microphone'])
        page = context.new_page()
        page.goto("https://permission.site/")
        page.click('#microphone')
        page.pause()
        # other actions...
        browser.close()
    
    
    with sync_playwright() as playwright:
        run(playwright)