8000 gemini/lib/tests-api.js at v0.5.0 Β· gemini-testing/gemini Β· GitHub
[go: up one dir, main page]

Skip to content
This repository was archived by the owner on Sep 21, 2022. It is now read-only.

Latest commit

Β 

History

History
139 lines (116 loc) Β· 4.22 KB

tests-api.js

File metadata and controls

139 lines (116 loc) Β· 4.22 KB
'use strict';
var Suite = require('./suite'),
State = require('./state');
function nothing() {}
function makeSkipBrowser(browser) {
if (typeof browser === 'string') {
return {browserName: browser};
}
if (typeof browser !== 'object' || browser === null) {
throw new TypeError('suite.skip browsers must be strings or object');
}
if (!browser.browserName) {
throw new Error('browserName is not specified');
}
if (typeof browser.browserName !== 'string') {
throw new TypeError('browserName must be a string');
}
if ('version' in browser && typeof browser.version !== 'string') {
throw new TypeError('version must be a string');
}
return browser;
}
function notString(arg) {
return typeof arg !== 'string';
}
function SuiteBuilder(suite) {
this.setCaptureElements = function() {
var selectors;
if (arguments.length === 1 && Array.isArray(arguments[0])) {
selectors = arguments[0];
} else {
selectors = Array.prototype.slice.call(arguments);
}
if (selectors.some(notString)) {
throw new TypeError('suite.captureElements accepts only strings or array of strings');
}
suite.captureSelectors = selectors;
return this;
};
this.before = function(hook) {
if (typeof hook !== 'function') {
throw new TypeError('before hook must be a function');
}
suite.beforeHook = hook;
return this;
};
this.after = function(hook) {
if (typeof hook !== 'function') {
throw new TypeError('after hook must be a function');
}
suite.afterHook = hook;
return this;
};
this.setUrl = function setUrl(url) {
if (typeof url !== 'string') {
throw new TypeError('URL must be string');
}
suite.url = url;
return this;
};
this.capture = function capture(name, cb) {
if (typeof name !== 'string') {
throw new TypeError('State name should be string');
}
cb = cb || nothing;
if (typeof cb !== 'function') {
throw new TypeError('Second argument of suite.capture must be a function');
}
if (suite.hasStateNamed(name)) {
throw new Error('State "' + name + '" already exists in suite "' + suite.name + '". ' +
'Choose different name');
}
suite.addState(new State(suite, name, cb));
return this;
};
this.skip = function skip(browser) {
if (!browser) {
suite.skip();
} else if (Array.isArray(browser)) {
suite.skip(browser.map(makeSkipBrowser));
} else if (typeof browser === 'string' || typeof browser === 'object') {
suite.skip([makeSkipBrowser(browser)]);
} else {
throw new TypeError('Unacceptable value for suite.skip method: ' + browser);
}
return this;
};
}
module.exports = function(context, rootSuite) {
context.suite = function suite(name, callback) {
if (typeof name !== 'string') {
throw new TypeError('First argument of the gemini.suite must be a string');
}
if (typeof callback !== 'function') {
throw new TypeError('Second argument of the gemini.suite must be a function');
}
if (rootSuite.hasChildNamed(name)) {
throw new Error('Suite ' + name + ' already exists at this level. Choose different name');
}
rootSuite = Suite.create(name, rootSuite);
callback(new SuiteBuilder(rootSuite));
if (rootSuite.hasStates) {
if (!rootSuite.url) {
throw new Error('URL is required to capture screenshots. ' +
'Call suite.setUrl(<DESIRED PATH>) on "' + rootSuite.name +
'" suite or one of its parents');
}
if (!rootSuite.captureSelectors) {
throw new Error('Capture region is required to capture screenshots. ' +
'Call suite.setCaptureElements(<CSS SELECTORS>) on "' + rootSuite.name +
'" suite or one of its parents');
}
}
rootSuite = rootSuite.parent;
};
};
0