Headless JavaScript Testing HTML5 audio/ video
Asked Answered
G

1

8

I know that there is a lot of other similar questions, however the answers don't provide a way round this problem.

I have a JavaScript file used on my website that uses the HTML 5 Web Audio and want to unit test it.

I have looked at using QUnit with PhantomJS and before you say anything I know that Phantom doesn't support it (http://phantomjs.org/supported-web-standards.html) however I want to know if there is a way around this?

Testing it using QUnit in the browser works as you would expect but I don't want to have to test it using the browser every time, I want it to be automated on the server.

An example of one of the tests that fails:

QUnit.test("isPlaying", function(assert){

    // true case
    My.Sound.play("background");
    assert.ok(My.Sound.isPlaying("background"), "The background audio is playing");

    // false case
    My.Sound.pause("background");
    assert.ok(!My.Sound.isPlaying("background"), "The background audio is not playing");
});
Godroon answered 9/1, 2015 at 11:29 Comment(3)
I don't know if QUnit runs with SlimerJS, but if it does you may be able to do this with SlimerJS+xvfbWeakkneed
You can always mock out the entire Web Audio API... but seems like overkill. What you're testing here is the HTML5 Web Audio works, not your own code.Transudation
Related for automated testing with SlimerJS and xvfb: askubuntu.com/questions/430247/…Coreycorf
S
0

As @jakerella already pointed out, it makes no sense to test third party apis. Just focus on your functionality. In this case, you should test that whenever you want to play / pause a sound, you invoke the correct api methods for your sound object (play / pause), which should be stubs of the original implementations.

QUnit.test("play button should play sound when clicked", (assert) => {
    const button =  someButton; //...get your button / play trigger here
    const playStub = stub(My.Sound, 'play');

    //trigger button click

    assert.ok(playStub.called);
});

Take a look here for stubbing functionality with QUnit.

Septarium answered 11/4, 2017 at 6:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.