|
| 1 | +/** |
| 2 | + * JSTestDriver adapter for angular scenario tests |
| 3 | + * |
| 4 | + * Example of jsTestDriver.conf for running scenario tests with JSTD: |
| 5 | + <pre> |
| 6 | + server: http://localhost:9877 |
| 7 | +
|
| 8 | + load: |
| 9 | + - lib/angular-scenario.js |
| 10 | + - lib/jstd-scenario-adapter-config.js |
| 11 | + - lib/jstd-scenario-adapter.js |
| 12 | + # your test files go here # |
| 13 | +
|
| 14 | + proxy: |
| 15 | + - {matcher: "/your-prefix/*", server: "http://localhost:8000/"} |
| 16 | + </pre> |
| 17 | + * |
| 18 | + * For more information on how to configure jstd proxy, see {@link http://code.google.com/p/js-test-driver/wiki/Proxy} |
| 19 | + * Note the order of files - it's important ! |
| 20 | + * |
| 21 | + * Example of jstd-scenario-adapter-config.js |
| 22 | + <pre> |
| 23 | + var jstdScenarioAdapter = { |
| 24 | + relativeUrlPrefix: '/your-prefix/' |
| 25 | + }; |
| 26 | + </pre> |
| 27 | + * |
| 28 | + * Whenever you use <code>browser().navigateTo('relativeUrl')</code> in your scenario test, the relativeUrlPrefix will be prepended. |
| 29 | + * You have to configure this to work together with JSTD proxy. |
| 30 | + * |
| 31 | + * Let's assume you are using the above configuration (jsTestDriver.conf and jstd-scenario-adapter-config.js): |
| 32 | + * Now, when you call <code>browser().navigateTo('index.html')</code> in your scenario test, the browser will open /your-prefix/index.html. |
| 33 | + * That matches the proxy, so JSTD will proxy this request to http://localhost:8000/index.html. |
| 34 | + */ |
| 35 | + |
| 36 | +/** |
| 37 | + * Custom type of test case |
| 38 | + * |
| 39 | + * @const |
| 40 | + * @see jstestdriver.TestCaseInfo |
| 41 | + */ |
| 42 | +var SCENARIO_TYPE = 'scenario'; |
| 43 | + |
| 44 | +/** |
| 45 | + * Plugin for JSTestDriver |
| 46 | + * Connection point between scenario's jstd output and jstestdriver. |
| 47 | + * |
| 48 | + * @see jstestdriver.PluginRegistrar |
| 49 | + */ |
| 50 | +function JstdPlugin() { |
| 51 | + var nop = function() {}; |
| 52 | + |
| 53 | + this.reportResult = nop; |
| 54 | + this.reportEnd = nop; |
| 55 | + this.runScenario = nop; |
| 56 | + |
| 57 | + this.name = 'Angular Scenario Adapter'; |
| 58 | + |
| 59 | + /** |
| 60 | + * Called for each JSTD TestCase |
| 61 | + * |
| 62 | + * Handles only SCENARIO_TYPE test cases. There should be only one fake TestCase. |
| 63 | + * Runs all scenario tests (under one fake TestCase) and report all results to JSTD. |
| 64 | + * |
| 65 | + * @param {jstestdriver.TestRunConfiguration} configuration |
| 66 | + * @param {Function} onTestDone |
| 67 | + * @param {Function} onAllTestsComplete |
| 68 | + * @returns {boolean} True if this type of test is handled by this plugin, false otherwise |
| 69 | + */ |
| 70 | + this.runTestConfiguration = function(configuration, onTestDone, onAllTestsComplete) { |
| 71 | + if (configuration.getTestCaseInfo().getType() != SCENARIO_TYPE) return false; |
| 72 | + |
| 73 | + this.reportResult = onTestDone; |
| 74 | + this.reportEnd = onAllTestsComplete; |
| 75 | + this.runScenario(); |
| 76 | + |
| 77 | + return true; |
| 78 | + }; |
| 79 | + |
| 80 | + this.getTestRunsConfigurationFor = function(testCaseInfos, expressions, testRunsConfiguration) { |
| 81 | + testRunsConfiguration.push( |
| 82 | + new jstestdriver.TestRunConfiguration( |
| 83 | + new jstestdriver.TestCaseInfo( |
| 84 | + 'Angular Scenario Tests', function() {}, SCENARIO_TYPE), [])); |
| 85 | + |
| 86 | + return true; |
| 87 | + }; |
| 88 | +} |
| 89 | + |
| 90 | +/** |
| 91 | + * Singleton instance of the plugin |
| 92 | + * Accessed using closure by: |
| 93 | + * - jstd output (reports to this plugin) |
| 94 | + * - initScenarioAdapter (register the plugin to jstd) |
| 95 | + */ |
| 96 | +var plugin = new JstdPlugin(); |
| 97 | + |
| 98 | +/** |
| 99 | + * Initialise scenario jstd-adapter |
| 100 | + * (only if jstestdriver is defined) |
| 101 | + * |
| 102 | + * @param {Object} jstestdriver Undefined when run from browser (without jstd) |
| 103 | + * @param {Function} initScenarioAndRun Function that inits scenario and runs all the tests |
| 104 | + * @param {Object=} config Configuration object, supported properties: |
| 105 | + * - relativeUrlPrefix: prefix for all relative links when navigateTo() |
| 106 | + */ |
| 107 | +function initScenarioAdapter(jstestdriver, initScenarioAndRun, config) { |
| 108 | + if (jstestdriver) { |
| 109 | + // create and register ScenarioPlugin |
| 110 | + jstestdriver.pluginRegistrar.register(plugin); |
| 111 | + plugin.runScenario = initScenarioAndRun; |
| 112 | + |
| 113 | + /** |
| 114 | + * HACK (angular.scenario.Application.navigateTo) |
| 115 | + * |
| 116 | + * We need to navigate to relative urls when running from browser (without JSTD), |
| 117 | + * because we want to allow running scenario tests without creating its own virtual host. |
| 118 | + * For example: http://angular.local/build/docs/docs-scenario.html |
| 119 | + * |
| 120 | + * On the other hand, when running with JSTD, we need to navigate to absolute urls, |
| 121 | + * because of JSTD proxy. (proxy, because of same domain policy) |
| 122 | + * |
| 123 | + * So this hack is applied only if running with JSTD and change all relative urls to absolute. |
| 124 | + */ |
| 125 | + var appProto = angular.scenario.Application.prototype, |
| 126 | + navigateTo = appProto.navigateTo, |
| 127 | + relativeUrlPrefix = config && config.relativeUrlPrefix || '/'; |
| 128 | + |
| 129 | + appProto.navigateTo = function(url, loadFn, errorFn) { |
| 130 | + if (url.charAt(0) != '/' && url.charAt(0) != '#' && |
| 131 | + url != 'about:blank' && !url.match(/^https?/)) { |
| 132 | + url = relativeUrlPrefix + url; |
| 133 | + } |
| 134 | + |
| 135 | + return navigateTo.call(this, url, loadFn, errorFn); |
| 136 | + }; |
| 137 | + } |
| 138 | +} |
| 139 | + |
| 140 | +/** |
| 141 | + * Builds proper TestResult object from given model spec |
| 142 | + * |
| 143 | + * TODO(vojta) report error details |
| 144 | + * |
| 145 | + * @param {angular.scenario.ObjectModel.Spec} spec |
| 146 | + * @returns {jstestdriver.TestResult} |
| 147 | + */ |
| 148 | +function createTestResultFromSpec(spec) { |
| 149 | + var map = { |
| 150 | + success: 'PASSED', |
| 151 | + error: 'ERROR', |
| 152 | + failure: 'FAILED' |
| 153 | + }; |
| 154 | + |
| 155 | + return new jstestdriver.TestResult( |
| 156 | + spec.fullDefinitionName, |
| 157 | + spec.name, |
| 158 | + jstestdriver.TestResult.RESULT[map[spec.status]], |
| 159 | + spec.error || '', |
| 160 | + spec.line || '', |
| 161 | + spec.duration); |
| 162 | +} |
| 163 | + |
| 164 | +/** |
| 165 | + * Generates JSTD output (jstestdriver.TestResult) |
| 166 | + */ |
| 167 | +angular.scenario.output('jstd', function(context, runner, model) { |
| 168 | + model.on('SpecEnd', function(spec) { |
| 169 | + plugin.reportResult(createTestResultFromSpec(spec)); |
| 170 | + }); |
| 171 | + |
| 172 | + model.on('RunnerEnd', function() { |
| 173 | + plugin.reportEnd(); |
| 174 | + }); |
| 175 | +}); |
0 commit comments