如何在localhost
页面中读取无头的Jasmine规范,以便测试用例可以在DOM元素上运行?
My Gulp任务成功运行Jasmine规范进行单元测试,现在我需要构建集成测试来验证localhost
提供的完整网页。我使用gulp-jasmine-browser
插件运行PhantomJS。
示例:
gulpfile.js
var gulp = require('gulp');
var jasmineBrowser = require('gulp-jasmine-browser');
function specRunner() {
gulp.src(['node_modules/jquery/dist/jquery.js', 'src/js/*.js', 'spec/*.js'])
.pipe(jasmineBrowser.specRunner({ console: true }))
.pipe(jasmineBrowser.headless());
}
gulp.task('spec', specRunner);
的规格/购物车-spec.js
describe('Cart component', function() {
it('displays on the gateway page', function() {
var page = loadWebPage('http://localhost/'); //DOES NOT WORK
var cart = page.find('#cart');
expect(cart.length).toBe(1);
});
});
没有loadWebPage()
功能。它只是为了说明我认为需要的功能。
答案 0 :(得分:3)
Selenium,WebdriverIO,Nightwatch.js,Protractor之类的端到端测试框架在这种情况下更适合。
gulp-jasmine-browser
插件仍然是关于浏览器环境中的单元测试。无法在页面之间导航。
答案 1 :(得分:2)
我将以下似乎有用的代码放在一起。请随时查看my repo并在您自己的环境中确认。
{
"name": "40646680",
"version": "1.0.0",
"main": "index.js",
"scripts": {
"test": "gulp jasmine"
},
"devDependencies": {
"gulp": "^3.9.1",
"gulp-jasmine-browser": "^1.7.1",
"jasmine": "^2.5.2",
"phantomjs": "^2.1.7"
}
}
(() => {
"use strict";
var gulp = require("gulp"),
jasmineBrowser = require("gulp-jasmine-browser");
gulp.task("jasmine", () => {
return gulp.src("test/*.js")
.pipe(jasmineBrowser.specRunner({
console: true
}))
.pipe(jasmineBrowser.headless());
});
})();
describe("A suite", function() {
it("contains spec with an expectation", function() {
expect(true).toBe(true);
});
it("contains failing spec with an expectation", function() {
expect(true).toBe(false);
});
});
Bob Chatman@CHATBAG42 F:\Development\StackOverflow\40646680
> npm test
> 40646680@1.0.0 test F:\Development\StackOverflow\40646680
> gulp jasmine
[21:56:44] Using gulpfile F:\Development\StackOverflow\40646680\gulpfile.js
[21:56:44] Starting 'jasmine'...
[21:56:44] Jasmine server listening on port 8000
.F
Failures:
1) A suite contains failing spec with an expectation
1.1) Expected true to be false.
2 specs, 1 failure
Finished in 0 seconds
[21:56:49] 'jasmine' errored after 4.26 s
[21:56:49] Error in plugin 'gulp-jasmine-browser'
Message:
1 failure
npm ERR! Test failed. See above for more details.
node 7.2
npm 3.9.3
jasmine 2.5.2
phantomjs 2.1.7
gulp 3.9.1
答案 2 :(得分:0)
事实证明,将网页加载到无头的Jasmine规范中非常容易......但是你需要换掉PhantomJS for jsdom。
策略:
beforeAll()
来调用将运行JSDOM.fromURL()
以请求网页的功能。window
和jQuery
以便在您的测试用例中使用。done()
表示测试现已准备好运行。确保在测试运行后关闭window
。
const url = 'http://dnajs.org/';
const { JSDOM } = require('jsdom');
let window, $;
function loadWebPage(done) {
function handleWebPage(dom) {
function waitForScripts() {
window = dom.window;
$ = dom.window.jQuery;
done();
}
dom.window.onload = waitForScripts;
}
const options = { resources: 'usable', runScripts: 'dangerously' };
JSDOM.fromURL(url, options).then(handleWebPage);
}
function closeWebPage() { window.close(); }
describe('The web page', () => {
beforeAll(loadWebPage);
afterAll(closeWebPage);
it('has the correct URL', () => {
expect(window.location.href).toBe(url);
});
it('has exactly one header, main, and footer', () => {
const actual = {
header: $('body >header').length,
main: $('body >main').length,
footer: $('body >footer').length
};
const expected = { header: 1, main: 1, footer: 1 };
expect(actual).toEqual(expected);
});
});
注意:以上截图来自类似的Mocha规范,因为Mocha有一个不错的默认记者。
如果你想自己尝试一下,它就在GitHub上:
https://github.com/dnajs/load-web-page-jsdom-jasmine
已编辑:已更新为jsdom 11