Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detecting if sound is playing in selenium

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?

like image 907
Joe Avatar asked Sep 25 '14 15:09

Joe


Video Answer


1 Answers

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

like image 146
Sighil Avatar answered Sep 20 '22 08:09

Sighil