2 * @license AngularJS v0.10.6-5cdfe45a
3 * (c) 2010-2011 AngularJS http://angularjs.org
10 * JSTestDriver adapter for angular scenario tests
12 * Example of jsTestDriver.conf for running scenario tests with JSTD:
14 server: http://localhost:9877
17 - lib/angular-scenario.js
18 - lib/jstd-scenario-adapter-config.js
19 - lib/jstd-scenario-adapter.js
20 # your test files go here #
23 - {matcher: "/your-prefix/*", server: "http://localhost:8000/"}
26 * For more information on how to configure jstd proxy, see {@link http://code.google.com/p/js-test-driver/wiki/Proxy}
27 * Note the order of files - it's important !
29 * Example of jstd-scenario-adapter-config.js
31 var jstdScenarioAdapter = {
32 relativeUrlPrefix: '/your-prefix/'
36 * Whenever you use <code>browser().navigateTo('relativeUrl')</code> in your scenario test, the relativeUrlPrefix will be prepended.
37 * You have to configure this to work together with JSTD proxy.
39 * Let's assume you are using the above configuration (jsTestDriver.conf and jstd-scenario-adapter-config.js):
40 * Now, when you call <code>browser().navigateTo('index.html')</code> in your scenario test, the browser will open /your-prefix/index.html.
41 * That matches the proxy, so JSTD will proxy this request to http://localhost:8000/index.html.
45 * Custom type of test case
48 * @see jstestdriver.TestCaseInfo
50 var SCENARIO_TYPE = 'scenario';
53 * Plugin for JSTestDriver
54 * Connection point between scenario's jstd output and jstestdriver.
56 * @see jstestdriver.PluginRegistrar
58 function JstdPlugin() {
59 var nop = function() {};
61 this.reportResult = nop;
63 this.runScenario = nop;
65 this.name = 'Angular Scenario Adapter';
68 * Called for each JSTD TestCase
70 * Handles only SCENARIO_TYPE test cases. There should be only one fake TestCase.
71 * Runs all scenario tests (under one fake TestCase) and report all results to JSTD.
73 * @param {jstestdriver.TestRunConfiguration} configuration
74 * @param {Function} onTestDone
75 * @param {Function} onAllTestsComplete
76 * @returns {boolean} True if this type of test is handled by this plugin, false otherwise
78 this.runTestConfiguration = function(configuration, onTestDone, onAllTestsComplete) {
79 if (configuration.getTestCaseInfo().getType() != SCENARIO_TYPE) return false;
81 this.reportResult = onTestDone;
82 this.reportEnd = onAllTestsComplete;
88 this.getTestRunsConfigurationFor = function(testCaseInfos, expressions, testRunsConfiguration) {
89 testRunsConfiguration.push(
90 new jstestdriver.TestRunConfiguration(
91 new jstestdriver.TestCaseInfo(
92 'Angular Scenario Tests', function() {}, SCENARIO_TYPE), []));
99 * Singleton instance of the plugin
100 * Accessed using closure by:
101 * - jstd output (reports to this plugin)
102 * - initScenarioAdapter (register the plugin to jstd)
104 var plugin = new JstdPlugin();
107 * Initialise scenario jstd-adapter
108 * (only if jstestdriver is defined)
110 * @param {Object} jstestdriver Undefined when run from browser (without jstd)
111 * @param {Function} initScenarioAndRun Function that inits scenario and runs all the tests
112 * @param {Object=} config Configuration object, supported properties:
113 * - relativeUrlPrefix: prefix for all relative links when navigateTo()
115 function initScenarioAdapter(jstestdriver, initScenarioAndRun, config) {
117 // create and register ScenarioPlugin
118 jstestdriver.pluginRegistrar.register(plugin);
119 plugin.runScenario = initScenarioAndRun;
122 * HACK (angular.scenario.Application.navigateTo)
124 * We need to navigate to relative urls when running from browser (without JSTD),
125 * because we want to allow running scenario tests without creating its own virtual host.
126 * For example: http://angular.local/build/docs/docs-scenario.html
128 * On the other hand, when running with JSTD, we need to navigate to absolute urls,
129 * because of JSTD proxy. (proxy, because of same domain policy)
131 * So this hack is applied only if running with JSTD and change all relative urls to absolute.
133 var appProto = angular.scenario.Application.prototype,
134 navigateTo = appProto.navigateTo,
135 relativeUrlPrefix = config && config.relativeUrlPrefix || '/';
137 appProto.navigateTo = function(url, loadFn, errorFn) {
138 if (url.charAt(0) != '/' && url.charAt(0) != '#' &&
139 url != 'about:blank' && !url.match(/^https?/)) {
140 url = relativeUrlPrefix + url;
143 return navigateTo.call(this, url, loadFn, errorFn);
149 * Builds proper TestResult object from given model spec
151 * TODO(vojta) report error details
153 * @param {angular.scenario.ObjectModel.Spec} spec
154 * @returns {jstestdriver.TestResult}
156 function createTestResultFromSpec(spec) {
163 return new jstestdriver.TestResult(
164 spec.fullDefinitionName,
166 jstestdriver.TestResult.RESULT[map[spec.status]],
173 * Generates JSTD output (jstestdriver.TestResult)
175 angular.scenario.output('jstd', function(context, runner, model) {
176 model.on('SpecEnd', function(spec) {
177 plugin.reportResult(createTestResultFromSpec(spec));
180 model.on('RunnerEnd', function() {
184 initScenarioAdapter(window.jstestdriver, angular.scenario.setUpAndRun, window.jstdScenarioAdapter);