microsoft / playwright

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.
https://playwright.dev
Apache License 2.0
67.2k stars 3.7k forks source link

[Feature] Add an option for mute that is similar to headless. #19534

Open matecsaj opened 1 year ago

matecsaj commented 1 year ago

The Playwright documentation has a link to args for Chromium but not WebKit. I looked on the WebKit site but could not find a list. I googled for a similar code but found none that dealt with WebKit and sound. No matter what args I try, the sound keeps playing. How can the sound be muted? Help, please.

from playwright.sync_api import sync_playwright

with sync_playwright() as playwright:
    # TODO mute the browser; I can't find a list of WebKit args, and none of my guesses stop the sound
    # args=['--nosound'] ignore_default_args=['--nosound']) args=["--mute-audio"]) ignore_default_args=["--mute-audio"]
    with playwright.webkit.launch(headless=False, ignore_default_args=["--mute-audio"]) as browser:
        with browser.new_context() as context:
            with context.new_page() as page:
                page.goto("https://www.youtube.com/watch?v=J2O_xa8cems&list=RDJ2O_xa8cems&start_radio=1")
                page.wait_for_timeout(40000)        # 30 seconds of ads then 10 seconds of the music video
yury-s commented 1 year ago

There is no such option in WebKit. I'll leave it open as a feature request. Can you tell us a bit more about your use case and why you need it?

matecsaj commented 1 year ago

I use Playwright to automate something repetitive that I need to do on a noisy website. I settled on using WebKit because the website renders inconsistently with Chromium and Firefox. I run the automation headless while doing other work, and the random noises disturb my concentration.

I get that most people probably use Playwriter for testing. Imagine someone running current tests on a noisy website. Oh my, the overlapping audio would be awful.

Turning off all the sound on the computer is not practical because someone could, for example, initiate a video conference while you happen to be running Playwright.

A website has the stuff to see and stuff to hear. Currently, Playwright has a headless option to suppress visuals but nothing for sounds.

Instead of a WebKit-specific muting arg, it would be lovely if the launch method had a browser-agnostic keyword argument mute=True or False, which defaults to False for backward compatibility.

blordpluto commented 1 year ago

+1 for this. I want to be able to demo concurrently running tests on a video call. The audio and text-to-speech involved is overwhelming, and there's no apparent workaround.

aiaimimi0920 commented 7 months ago

If you just want to temporarily avoid this issue, you can consider injecting a JavaScript script

like this:

AUDIO_MUTE_JS_SCRIPT = """() => {
function disableVideoFeatures(video) {
  video.autoplay = false;
  video.muted = true;
  video.src = '';
}

let videos_playwright = document.getElementsByTagName('video');
for (let i = 0; i < videos_playwright.length; i++) {
  disableVideoFeatures(videos_playwright[i]);
}

let observer = new MutationObserver(function(mutations) {
  mutations.forEach(function(mutation) {
  mutation.addedNodes.forEach(function(node) {
    if (node instanceof HTMLVideoElement) {
      disableVideoFeatures(node);
      } else if (node.getElementsByTagName) {
      let videos = node.getElementsByTagName('video');
        for (let i = 0; i < videos.length; i++) {
          disableVideoFeatures(videos[i]);
        }
      }
    });
  });
});

let config_playwright = { childList: true, subtree: true };

observer.observe(document.body, config_playwright);
}"""

await self.page.goto(base_url)
await self.page.evaluate(AUDIO_MUTE_JS_SCRIPT)
mbUSC commented 4 months ago

+1

anitamc commented 3 months ago

+1