siesta-lite
Version:
Stress-free JavaScript unit testing and functional testing tool, works in NodeJS and browsers
482 lines (349 loc) • 16.2 kB
JavaScript
Role('Siesta.Test.Self', {
requires : [ 'typeOf' ],
has : {
},
methods : {
getDescriptionTree : function (test) {
var copyResults = function (results) {
var children = []
var copy = {
desc : results.description
}
Joose.A.each(results.children, function (child) {
if (child.meta.name != 'Siesta.Result.Summary') children.push(copyResults(child))
})
if (children.length) copy.children = children
return copy
}
return copyResults(test.results).children
},
cloneInstanceExceptAttributes : function (instance, exceptAttributes) {
// this will intentionally include _all_ properties, even from the prototype
var copy = Joose.O.copy(instance)
Joose.A.each(exceptAttributes || [], function (attrName) {
delete copy[ attrName ]
})
// only the properties that corresponds to actual attributes will be used from the config object
// others (like methods functions) will be ignored
return new instance.constructor(copy)
},
getSelfTest : function (meta, config, doNotTranslate) {
config = config || {}
var SIESTA = this.global.Siesta
if (!config.project) config.project = new SIESTA.Harness()
if (!config.run) config.run = function () {}
if (!config.url) config.url = this.url
config.project.isPuppeteer = this.project.isPuppeteer
config.project.isSelenium = this.project.isSelenium
config.project.isAutomated = this.project.isAutomated
config.project.showCursor = false;
config.project.generateLoaderPathHook = config.project.generateLoaderInstrumentationHook = function () {
return function () {}
}
config.project.browserWindowHasFocus = function () {
return true
}
config.generation = this.generation
config.needToCleanup = false
config.scopeProvider = this.scopeProvider
config.global = this.global
config.isTodo = this.isTodo
// copy only native simulator
if (this.simulator && !(this.simulator instanceof Siesta.Test.Simulator))
config.simulator = this.simulator
if (this.simulatorConfig)
config.simulatorConfig = this.simulatorConfig
config.overrideSetTimeout = false
config.originalSetTimeout = this.originalSetTimeout
config.originalClearTimeout = this.originalClearTimeout
config.exceptionCatcher = this.getExceptionCatcher()
config.testErrorClass = this.getTestErrorClass()
config.startTestAnchor = this.getStartTestAnchor()
// config.actionDelay = this.actionDelay
// config.mouseMovePrecision = this.mouseMovePrecision
// config.dragDelay = this.dragDelay
config.bowser = config.browser = this.bowser
if (this.mouseVisualizer)
config.mouseVisualizer = this.cloneInstanceExceptAttributes(this.mouseVisualizer)
if (!config.waitForTimeout) config.waitForTimeout = this.waitForTimeout
if (!config.defaultTimeout) config.defaultTimeout = this.defaultTimeout
if (!config.isReadyTimeout) config.isReadyTimeout = this.isReadyTimeout
if (!doNotTranslate) {
config.trait = SIESTA.Test.AssertionsTranslator
config.translateTo = this
}
return new meta(config)
},
getGenericTest : function (config, doNotTranslate) {
return this.getSelfTest(this.global.Siesta.Test, config, doNotTranslate)
},
getBrowserTest : function (config, doNotTranslate) {
return this.getSelfTest(this.global.Siesta.Test.Browser, config, doNotTranslate)
},
getExtJSTest : function (config, doNotTranslate) {
return this.getSelfTest(this.global.Siesta.Test.ExtJS, config, doNotTranslate)
},
getJQueryTest : function (config, doNotTranslate) {
return this.getSelfTest(this.global.Siesta.Test.jQuery, config, doNotTranslate)
},
testSelf : function (meta, config, defaultConfig, runFunc, callback) {
config = config || {}
for (var key in defaultConfig)
if (!config.hasOwnProperty(key)) config[ key ] = defaultConfig[ key ]
config.run = runFunc
var test = this.getSelfTest(meta, config, config.doNotTranslate)
var me = this
var async = this.beginAsync(90000);
test.on('testfinalize', function (event) {
// ignore bubbled events from sub-tests
if (event.source == test) {
me.endAsync(async);
callback && callback.call(me, test)
// some cleanup
test.cleanup()
}
});
test.on('testendbubbling', function (event, test) {
me.fireEvent('testendbubbling', test)
})
var Siesta = this.global.Siesta
// need to init the feature support singleton before starting the test, because doing that in the middle
// may affect the scrolling position
Siesta.Project.Browser.FeatureSupport();
test.start();
},
testGeneric : function (config, runFunc, callback) {
if (/Function/.test(this.typeOf(config))) {
callback = runFunc
runFunc = config
config = null
}
this.testSelf(this.global.Siesta.Test, config, { transparentEx : true }, runFunc, callback)
},
testBrowser : function (config, runFunc, callback) {
if (/Function/.test(this.typeOf(config))) {
callback = runFunc
runFunc = config
config = null
}
this.testSelf(this.global.Siesta.Test.Browser, config, {
transparentEx : true,
suppressEventsLog : true,
actionDelay : 1
}, runFunc, callback)
},
testJQuery : function (config, runFunc, callback) {
if (/Function/.test(this.typeOf(config))) {
callback = runFunc
runFunc = config
config = null
}
this.testSelf(this.global.Siesta.Test.jQuery, config, {
transparentEx : true,
suppressEventsLog : true,
actionDelay : 1
}, runFunc, callback)
},
testExtJS : function (config, runFunc, callback) {
if (/Function/.test(this.typeOf(config))) {
callback = runFunc
runFunc = config
config = null
}
this.testSelf(this.global.Siesta.Test.ExtJS, config, {
transparentEx : true,
suppressEventsLog : true,
actionDelay : 1
}, runFunc, callback)
},
testSenchaTouch : function (config, runFunc, callback) {
if (this.typeOf(config) == 'Function') {
callback = runFunc
runFunc = config
config = null
}
this.testSelf(this.global.Siesta.Test.SenchaTouch, config, {
transparentEx : true,
suppressEventsLog : true,
performSetup : false,
actionDelay : 1
}, runFunc, callback)
},
expectPass : function (func, meta, config) {
var me = this
var Siesta = this.global.Siesta
config = config || {}
Joose.O.extend(config, { doNotTranslate : true })
var callback
if (this.typeOf(meta) == 'Function') {
callback = meta
meta = null
} else
callback = config.callback
var check = function (test) {
test.eachAssertion(function (assertion) {
if (assertion.passed)
me.pass("Assertion passed: " + assertion.description)
else
me.fail("Assertion failed: " + assertion.description, assertion.annotation)
})
callback && callback()
}
if (!meta || meta == Siesta.Test.ExtJS)
this.testExtJS(config, func, check)
else if (meta == Siesta.Test.SenchaTouch)
this.testSenchaTouch(config, func, check)
else if (meta == Siesta.Test.Browser)
this.testBrowser(config, func, check)
else if (meta == Siesta.Test.jQuery)
this.testJQuery(config, func, check)
else if (meta == Siesta.Test)
this.testGeneric(config, func, check)
},
expectFail : function (func, meta, config) {
var me = this
var Siesta = this.global.Siesta
config = config || {}
Joose.O.extend(config, { doNotTranslate : true })
var callback
if (this.typeOf(meta) == 'Function') {
callback = meta
meta = null
} else
callback = config.callback
var check = function (test) {
test.eachAssertion(function (assertion) {
me.notOk(assertion.passed, "Assertion failed: " + assertion.description)
})
callback && callback()
}
if (!meta || meta == Siesta.Test.ExtJS)
this.testExtJS(config, func, check)
else if (meta == Siesta.Test.SenchaTouch)
this.testSenchaTouch(config, func, check)
else if (meta == Siesta.Test.Browser)
this.testBrowser(config, func, check)
else if (meta == Siesta.Test.jQuery)
this.testJQuery(config, func, check)
else if (meta == Siesta.Test)
this.testGeneric(config, func, check)
},
getHarness : function(config, tests, doNotStart) {
var Siesta = this.global.Siesta;
var Harness = this.global.Harness = new Siesta.Project.Browser.ExtJS()
if (arguments.length === 1) {
tests = config;
config = null;
}
config = config || {
viewDOM : true,
scaleToFit : false,
transparentEx : true,
autoCheckGlobals : false
}
config.title = 'SiestaSelf';
config.stateful = false
Harness.configure(config)
if (!doNotStart) Harness.start.apply(Harness, tests || [
'601_siesta_ui_failing.t.js',
'601_siesta_ui_passing.t.js'
]);
return Harness;
},
getTouchHarness : function(config, tests) {
var Siesta = this.global.Siesta;
var Harness = this.global.Harness = new Siesta.Project.Browser.SenchaTouch()
if (arguments.length === 1) {
tests = config;
config = null;
}
config = config || {
title : 'SiestaSelfSuite',
viewDOM : true,
transparentEx : true,
autoCheckGlobals : false
}
config.stateful = false
Harness.configure(config)
Harness.start.apply(Harness, tests || []);
return Harness;
},
waitForHarnessEvent : function(eventName, callback, scope, timeout) {
var eventFired = false
this.global.Harness.on(eventName, function () { eventFired = true }, null, { single : true });
this.waitFor({
method : function() { return eventFired; },
callback : callback,
scope : scope,
timeout : timeout,
assertionName : 'waitForHarnessEvent',
description : ' Harness to fire its "' + eventName + '" event'
});
},
copyResult : function (result) {
var me = this
var cls = eval(result.meta.name)
var copy = new cls({
id : result.id,
description : result.description,
test : result.test,
name : result.name,
passed : result.passed,
annotation : result.annotation,
index : result.index,
sourceLine : result.sourceLine,
isTodo : result.isTodo,
isWaitFor : result.isWaitFor,
completed : result.completed,
isWarning : result.isWarning,
length : result.length,
children : Joose.A.map(result.children, function (childResult) { return me.copyResult(childResult) })
})
Joose.A.each(copy.children, function (childResult) {
childResult.parent = copy
})
return copy
},
addTranslatedResult : function (result) {
var copy = this.copyResult(result)
var parent = this.getResults().findChildById(result.parent.id)
if (parent) {
parent.push(copy)
this.fireEvent('testupdate', this, copy, parent)
} else
this.addResult(copy)
},
runFirstTest : function(cb) {
var Harness = this.global.Harness;
var desc = Harness.descriptors[ 0 ];
var tree = this.cq1('treepanel', this.Ext());
// Select it in the UI too so we can easily inspect it
tree.getSelectionModel().select(tree.store.getById(desc.id));
Harness.launch([ desc ], cb)
},
waitForHarnessReady : function(callback) {
var Harness = this.global.Harness;
this.waitFor(function() { return Harness.setupDone; }, callback);
},
waitForHarnessIdle : function(callback) {
var Harness = this.global.Harness;
this.waitFor(function() { return Harness.endDate; }, callback);
},
getActiveTestWindow : function() {
return this.cq1('resultpanel').test.scopeProvider.scope;
},
getRecorderPanel : function (t, panelConfig, frame) {
var Siesta = this.global.Siesta
var recorderPanel = new Siesta.Recorder.UI.RecorderPanel(Joose.O.extend({
width : 600,
height : 200,
renderTo : this.global.document.body
}, panelConfig || {}));
recorderPanel.attachTo(t || this, frame);
var recorder = recorderPanel.recorder;
recorder.ignoreSynthetic = false;
recorder.start();
return recorderPanel;
}
}
})