我一直在尝试通过自动测试来覆盖<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"
}
在因果报应方面,问题是我找不到一种方法来添加可能在测试中使用的资源,所以没有文件可以在其中播放。如果有一种方法可以指向要播放的文件,那么这应该不是问题,因为我可以简单地检查元素的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?
});
我曾考虑过可能嘲笑音频API,并简单地伪造正在更新的属性,但我仍在针对我的代码进行测试,当我的最终目标是测试音频精灵上的声音时,currentTime
将很难准确地模仿。
理想情况下,我希望在单元测试中涵盖这一点,因为它应该在哪里,所以能够使用工作资源将是理想的。因此,一个简单的expect(!element[0].paused).toEqual(true);
就足以知道它正在播放。
如何在单元测试中提供用作音频源的文件?
假设您在后台使用HTML5 <audio />
播放声音,您可以执行以下操作-基本上可以使用browser.executeScript
访问pause
。您需要有一种方法来导航到您的<audio />
标签;在这种情况下,它是第一个。这在我的沙盒中有效注意:我不隶属于angular media player——这只是谷歌上我可以使用量角器的第一个结果——我可以重复使用
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);
});
});
此外,你可以使用其他人的指令,比如这个网站(或任何其他网站),而不必担心单元测试(他们可能是为你做的),只需评估他们对象的范围,并确保你在E2E测试中正确设置了其属性,如果你不喜欢executeScript
,甚至不测试声音。