如何测试声音?

Gil*_*esC 9 javascript angularjs karma-runner protractor karma-jasmine

我一直试图<audio>通过自动化测试覆盖标签,首先确认它正在播放.

我正在使用通常的角度测试套件,业力和量角器.

"devDependencies": {
    "karma": "~0.10",
    "protractor": "~0.20.1",
    "http-server": "^0.6.1",
    "bower": "^1.3.1",
    "shelljs": "^0.2.6",
    "karma-junit-reporter": "^0.2.2",
    "grunt": "~0.4.1",
    "grunt-contrib-uglify": "~0.2.0",
    "grunt-contrib-concat": "~0.3.0",
    "grunt-contrib-watch": "~0.4.3"
}
Run Code Online (Sandbox Code Playgroud)

在业力方面,问题是我无法找到一种方法来添加资源以便在测试中使用,因此没有文件可以在那里播放.如果有一种方法指向要播放的文件,那么它应该不是问题,因为我可以简单地检查paused元素的属性.

在2e2方面,有一个演示应用程序可以完美地运行,测试可以正常加载它并单击其中一个按钮不会产生任何错误(如果您手动尝试它会触发声音).然而,在查看量角器API时,我找不到任何可以让我确保声音实际正在播放的内容,或者允许我像偶数一样访问该元素document并且angular在这里不可用(这在2e2测试中是有意义的)或者只是一个API检查元素属性.

beforeEach(function() {
    browser.get("index.html")
});

it("Ensure the player is playing", function () {

    $$("button").first().click();

    // what to do?

});
Run Code Online (Sandbox Code Playgroud)

我曾经想过可能会嘲笑音频API并简单地伪造正在更新的属性但是我仍在测试我的代码,currentTime当我的最终目标是在音频精灵上测试声音时,很难准确地模拟并在预期时停止.

理想情况下,我想在单元测试中覆盖它应该是的,因此能够使用工作资源将是理想的.这样一个简单的expect(!element[0].paused).toEqual(true);意志足以让人知道它正在播放.

如何在单元测试中提供文件以用作音频源?

Wor*_*red 12

假设您正在使用HTML5播放声音<audio />,您可以执行以下操作 - 主要用于browser.executeScript访问pause您想要的内容.你需要有办法导航到你的<audio />标签; 在这种情况下,它是第一个.这适用于我的沙箱. 注意:我不是角度媒体播放器的附属品 - 这只是谷歌的第一个结果,我可以使用量角器 - 我会尽可能地重复使用.

describe('angularjs homepage', function() {
  it('should have a title', function() {
    browser.get('http://mrgamer.github.io/angular-media-player/interactive.html');

    // add a song to the playlist
    element(by.repeater('song in prefabPlaylist')).click();

    // hook into the browser
    var isPaused = function () {
        return browser.executeScript(function () {
            return document.getElementsByTagName('audio')[0].paused;
        });
    };

    // make sure it's not playing
    expect(isPaused()).toBe(true);

    // start playing
    element(by.css('div[ng-click="mediaPlayer.playPause()"]')).click();

    // for some reason these were needed for me; maybe it's the way the directive is implemented?
    browser.waitForAngular();
    browser.waitForAngular();
    browser.waitForAngular();

    // make sure it's playing
    expect(isPaused()).toBe(false);

    // pause
    element(by.css('div[ng-click="mediaPlayer.playPause()"]')).click();

    // make sure it's paused
    expect(isPaused()).toBe(true);
  });
});
Run Code Online (Sandbox Code Playgroud)

你也可以使用别人的指令,比如这个网站(或任何其他网站),而不用担心单元测试(他们可能会为你做这个),只是评估他们的对象的范围,并确保你正确设置其属性在您的E2E测试中,如果您不喜欢,甚至不测试声音executeScript.