'Audio play in cypress
<audio id="audio" controls>
<source type="audio/mp3" src="https://my-app-domain.com/uploads/2021/03/model-audio1614939065612file732.mp3"> </source>
</audio>
The above code snipped is from my app. I need to test the audio is playable with cypress.
Below is my code to play the audio:
cy.visit('/');
cy.get("source")
.invoke("src")
.then((audiofile) => {
const audio = new Audio(audiofile);
audio.play();
});
With the above code I get the following error.
How can I test play audio with cypress ?
Solution 1:[1]
Answering my own question:
The following code block got the job done for me.
cy.get('source')
.invoke('attr', 'src')
.then((audiofile) => {
const audio = new Audio(audiofile);
audio.play();
});
Solution 2:[2]
cy.wait(1).then(_=>{
new Audio('https://s3.amazonaws.com/freecodecamp/drums/Chord_1.mp3').play();
})
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Pawan Rai |
| Solution 2 | fruitloaf |

