16

I have some 3rd party sites I load in an iframe for a project I'm working on, but I need to somehow detect if those sites are playing any sound. I'm not seeing any methods with WebDriver to see if sound is playing in the browser, is there some other way to query the VM itself?

Joe
  • 386
  • 2
  • 5
  • 1
    At lest related: http://stackoverflow.com/questions/17349345/check-if-audio-is-playing-correctly-using-selenium – alecxe Sep 25 '14 at 15:30
  • Can you explain "playing sound"? My desktop does not even have a sound card, can it "play sound" by your definition? – SiKing Sep 25 '14 at 15:50
  • Yes, say any flash application, java applet or whatever initiated a system call to the OS core audio service. Even if you don't have a sound card, the core audio service would still receive that request and try to process it, even if it were to a dummy audio device. This is how VM's work, then the host connects to that dummy audio device to send it to the real audio device. I want to detect if any web page I'm going to load in my iframe is going to be annoying to my users programmatically so I can just not show it there, automatically. – Joe Sep 25 '14 at 16:43
  • 3
    You know how Chrome puts a little speaker icon on a tab when sound is playing in that tab? That's all I want. Is there a way with selenium to query Chrome's tab names to check for that icon? – Joe Sep 25 '14 at 16:46
  • 1
    Selenium is a library that processes a browser DOM. For anything else, you will need other libraries. – SiKing Sep 25 '14 at 22:31
  • I guess you could with "strace" (or equivalent) grep system calls to the sound system. – Dorian Oct 11 '16 at 14:05
  • Somebody seems to have succeeded by capturing audio with Sunflower: http://stackoverflow.com/questions/25536303/detect-audio-with-selenium-webdriver-and-python – Dorian Oct 11 '16 at 14:06
  • Does this answer your question? [Check if audio is playing correctly using Selenium](https://stackoverflow.com/questions/17349345/check-if-audio-is-playing-correctly-using-selenium) – OrangeDog Sep 22 '20 at 08:46
  • Here you can find some more info: https://stackoverflow.com/a/40357198/10894791 – WKOW Dec 01 '20 at 15:15
  • https://stackoverflow.com/a/60700874/1387701: "Basically the only way that I found to access the entire window's audio is using MediaDevices.getDisplayMedia()." – DMart Dec 02 '20 at 05:22

1 Answers1

2

In the modern HTML5 age, for anyone checking to see if audio is playing, we can use the audio and video tag in html5 to check for sound playback using the javascript executor. Below code is in java and selenium

boolean isPlaying = false;
// find all frames and iterate over it.
// This finds only the top level frames. Frames inside frames are not considered for this answer
for (WebElement iframe: driver.findElements(By.cssSelector('iframe'))) {
    driver.switchTo().frame(iframe);
    // find all the video and audio tags within the frame
    for (WebElement player: driver.findElements(By.cssSelector('audio,video'))) {
       // check if the argument paused is true or false. 
       // In case audio or video is playing, paused is false
       String paused = (String) ((JavascriptExecutor) driver).executeScript('return arguments[0].paused', player);
       if (paused.equals('false')) {
           isPlaying = true;
           break;
       }
    }
    // switch out from the frame
    driver.switchTo().defaultContent();
    if (isPlaying)
        break;
}

The above code can be modified to track frames within frames as well by changing it into a recursive function

StrikerVillain
  • 3,719
  • 2
  • 24
  • 41