PageRenderTime 43ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/node_modules/selenium-webdriver/lib/webdriver/testing/jsunit.js

https://gitlab.com/JuanCalcagnoDev/CertFront
JavaScript | 363 lines | 219 code | 50 blank | 94 comment | 46 complexity | de7a350653c41ff6cfdd4b3000316b60 MD5 | raw file
  1. // Licensed to the Software Freedom Conservancy (SFC) under one
  2. // or more contributor license agreements. See the NOTICE file
  3. // distributed with this work for additional information
  4. // regarding copyright ownership. The SFC licenses this file
  5. // to you under the Apache License, Version 2.0 (the
  6. // "License"); you may not use this file except in compliance
  7. // with the License. You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing,
  12. // software distributed under the License is distributed on an
  13. // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  14. // KIND, either express or implied. See the License for the
  15. // specific language governing permissions and limitations
  16. // under the License.
  17. /**
  18. * @fileoverview File to include for turning any HTML file page into a WebDriver
  19. * JSUnit test suite by configuring an onload listener to the body that will
  20. * instantiate and start the test runner.
  21. */
  22. goog.provide('webdriver.testing.jsunit');
  23. goog.provide('webdriver.testing.jsunit.TestRunner');
  24. goog.require('goog.testing.TestRunner');
  25. goog.require('webdriver.testing.Client');
  26. goog.require('webdriver.testing.TestCase');
  27. /**
  28. * Constructs a test runner.
  29. * @param {!webdriver.testing.Client} client .
  30. * @constructor
  31. * @extends {goog.testing.TestRunner}
  32. */
  33. webdriver.testing.jsunit.TestRunner = function(client) {
  34. goog.base(this);
  35. /** @private {!webdriver.testing.Client} */
  36. this.client_ = client;
  37. };
  38. goog.inherits(webdriver.testing.jsunit.TestRunner, goog.testing.TestRunner);
  39. /**
  40. * Element created in the document to add test results to.
  41. * @private {Element}
  42. */
  43. webdriver.testing.jsunit.TestRunner.prototype.logEl_ = null;
  44. /**
  45. * DOM element used to stored screenshots. Screenshots are stored in the DOM to
  46. * avoid exhausting JS stack-space.
  47. * @private {Element}
  48. */
  49. webdriver.testing.jsunit.TestRunner.prototype.screenshotCacheEl_ = null;
  50. /** @override */
  51. webdriver.testing.jsunit.TestRunner.prototype.initialize = function(testCase) {
  52. goog.base(this, 'initialize', testCase);
  53. this.screenshotCacheEl_ = document.createElement('div');
  54. document.body.appendChild(this.screenshotCacheEl_);
  55. this.screenshotCacheEl_.style.display = 'none';
  56. };
  57. /** @override */
  58. webdriver.testing.jsunit.TestRunner.prototype.execute = function() {
  59. if (!this.testCase) {
  60. throw Error('The test runner must be initialized with a test case before ' +
  61. 'execute can be called.');
  62. }
  63. this.screenshotCacheEl_.innerHTML = '';
  64. this.client_.sendInitEvent();
  65. this.testCase.setCompletedCallback(goog.bind(this.onComplete_, this));
  66. this.testCase.runTests();
  67. };
  68. /**
  69. * Writes a nicely formatted log out to the document. Overrides
  70. * {@link goog.testing.TestRunner#writeLog} to handle writing screenshots to the
  71. * log.
  72. * @param {string} log The string to write.
  73. * @override
  74. */
  75. webdriver.testing.jsunit.TestRunner.prototype.writeLog = function(log) {
  76. var lines = log.split('\n');
  77. for (var i = 0; i < lines.length; i++) {
  78. var line = lines[i];
  79. var color;
  80. var isFailOrError = /FAILED/.test(line) || /ERROR/.test(line);
  81. var isScreenshot = / \[SCREENSHOT\] /.test(line);
  82. if (/PASSED/.test(line)) {
  83. color = 'darkgreen';
  84. } else if (isFailOrError) {
  85. color = 'darkred';
  86. } else if (isScreenshot) {
  87. color = 'darkblue';
  88. } else {
  89. color = '#333';
  90. }
  91. var div = document.createElement('div');
  92. if (line.substr(0, 2) == '> ') {
  93. // The stack trace may contain links so it has to be interpreted as HTML.
  94. div.innerHTML = line;
  95. } else {
  96. div.appendChild(document.createTextNode(line));
  97. }
  98. if (isFailOrError) {
  99. var testNameMatch = /(\S+) (\[[^\]]*] )?: (FAILED|ERROR)/.exec(line);
  100. if (testNameMatch) {
  101. // Build a URL to run the test individually. If this test was already
  102. // part of another subset test, we need to overwrite the old runTests
  103. // query parameter. We also need to do this without bringing in any
  104. // extra dependencies, otherwise we could mask missing dependency bugs.
  105. var newSearch = 'runTests=' + testNameMatch[1];
  106. var search = window.location.search;
  107. if (search) {
  108. var oldTests = /runTests=([^&]*)/.exec(search);
  109. if (oldTests) {
  110. newSearch = search.substr(0, oldTests.index) +
  111. newSearch +
  112. search.substr(oldTests.index + oldTests[0].length);
  113. } else {
  114. newSearch = search + '&' + newSearch;
  115. }
  116. } else {
  117. newSearch = '?' + newSearch;
  118. }
  119. var href = window.location.href;
  120. var hash = window.location.hash;
  121. if (hash && hash.charAt(0) != '#') {
  122. hash = '#' + hash;
  123. }
  124. href = href.split('#')[0].split('?')[0] + newSearch + hash;
  125. // Add the link.
  126. var a = document.createElement('A');
  127. a.innerHTML = '(run individually)';
  128. a.style.fontSize = '0.8em';
  129. a.href = href;
  130. div.appendChild(document.createTextNode(' '));
  131. div.appendChild(a);
  132. }
  133. }
  134. if (isScreenshot && this.screenshotCacheEl_.childNodes.length) {
  135. var nextScreenshot = this.screenshotCacheEl_.childNodes[0];
  136. this.screenshotCacheEl_.removeChild(nextScreenshot);
  137. a = document.createElement('A');
  138. a.style.fontSize = '0.8em';
  139. a.href = 'javascript:void(0);';
  140. a.onclick = goog.partial(toggleVisibility, a, nextScreenshot);
  141. toggleVisibility(a, nextScreenshot);
  142. div.appendChild(document.createTextNode(' '));
  143. div.appendChild(a);
  144. }
  145. div.style.color = color;
  146. div.style.font = 'normal 100% monospace';
  147. try {
  148. div.style.whiteSpace = 'pre-wrap';
  149. } catch (e) {
  150. // NOTE(user): IE raises an exception when assigning to pre-wrap.
  151. // Thankfully, it doesn't collapse whitespace when using monospace fonts,
  152. // so it will display correctly if we ignore the exception.
  153. }
  154. if (i < 2) {
  155. div.style.fontWeight = 'bold';
  156. }
  157. this.logEl_.appendChild(div);
  158. if (nextScreenshot) {
  159. a = document.createElement('A');
  160. // Accessing the |src| property in IE sometimes results in an
  161. // "Invalid pointer" error, which indicates it has been garbage
  162. // collected. This does not occur when using getAttribute.
  163. a.href = nextScreenshot.getAttribute('src');
  164. a.target = '_blank';
  165. a.appendChild(nextScreenshot);
  166. this.logEl_.appendChild(a);
  167. nextScreenshot = null;
  168. }
  169. }
  170. function toggleVisibility(link, img) {
  171. if (img.style.display === 'none') {
  172. img.style.display = '';
  173. link.innerHTML = '(hide screenshot)';
  174. } else {
  175. img.style.display = 'none';
  176. link.innerHTML = '(view screenshot)';
  177. }
  178. }
  179. };
  180. /**
  181. * Copied from goog.testing.TestRunner.prototype.onComplete_, which has private
  182. * visibility.
  183. * @private
  184. */
  185. webdriver.testing.jsunit.TestRunner.prototype.onComplete_ = function() {
  186. var log = this.testCase.getReport(true);
  187. if (this.errors.length > 0) {
  188. log += '\n' + this.errors.join('\n');
  189. }
  190. if (!this.logEl_) {
  191. this.logEl_ = document.createElement('div');
  192. document.body.appendChild(this.logEl_);
  193. }
  194. // Remove all children from the log element.
  195. var logEl = this.logEl_;
  196. while (logEl.firstChild) {
  197. logEl.removeChild(logEl.firstChild);
  198. }
  199. this.writeLog(log);
  200. this.client_.sendResultsEvent(this.isSuccess(), this.getReport(true));
  201. };
  202. /**
  203. * Takes a screenshot. In addition to saving the screenshot for viewing in the
  204. * HTML logs, the screenshot will also be saved using
  205. * @param {!webdriver.WebDriver} driver The driver to take the screenshot with.
  206. * @param {string=} opt_label An optional debug label to identify the screenshot
  207. * with.
  208. * @return {!webdriver.promise.Promise} A promise that will be resolved to the
  209. * screenshot as a base-64 encoded PNG.
  210. */
  211. webdriver.testing.jsunit.TestRunner.prototype.takeScreenshot = function(
  212. driver, opt_label) {
  213. if (!this.isInitialized()) {
  214. throw Error(
  215. 'The test runner must be initialized before it may be used to' +
  216. ' take screenshots');
  217. }
  218. var client = this.client_;
  219. var testCase = this.testCase;
  220. var screenshotCache = this.screenshotCacheEl_;
  221. return driver.takeScreenshot().then(function(png) {
  222. client.sendScreenshotEvent(png, opt_label);
  223. var img = document.createElement('img');
  224. img.src = 'data:image/png;base64,' + png;
  225. img.style.border = '1px solid black';
  226. img.style.maxWidth = '500px';
  227. screenshotCache.appendChild(img);
  228. if (testCase) {
  229. testCase.saveMessage('[SCREENSHOT] ' + (opt_label || '<Not Labeled>'));
  230. }
  231. return png;
  232. });
  233. };
  234. /**
  235. * Sends a base64 encoded PNG image to the server to be saved in the test
  236. * outputs.
  237. * @param {string} data The base64 encoded PNG image to be sent to the server.
  238. * @param {string=} opt_label An optional debug label to identify the
  239. * screenshot with.
  240. */
  241. webdriver.testing.jsunit.TestRunner.prototype.saveImage = function(
  242. data, opt_label) {
  243. if (!this.isInitialized()) {
  244. throw Error(
  245. 'The test runner must be initialized before it may be used to' +
  246. ' save images');
  247. }
  248. this.client_.sendScreenshotEvent(data, opt_label);
  249. var img = document.createElement('img');
  250. img.src = 'data:image/png;base64,' + data;
  251. img.style.border = '1px solid black';
  252. img.style.maxWidth = '500px';
  253. this.screenshotCacheEl_.appendChild(img);
  254. if (this.testCase) {
  255. this.testCase.saveMessage('[SCREENSHOT] ' + (opt_label || '<Not Labeled>'));
  256. }
  257. };
  258. (function() {
  259. var client = new webdriver.testing.Client();
  260. var tr = new webdriver.testing.jsunit.TestRunner(client);
  261. // Export our test runner so it can be accessed by Selenium/WebDriver. This
  262. // will only work if webdriver.WebDriver is using a pure-JavaScript
  263. // webdriver.CommandExecutor. Otherwise, the JS-client could change the
  264. // driver's focus to another window or frame and the Java/Python-client
  265. // wouldn't be able to access this object.
  266. goog.exportSymbol('G_testRunner', tr);
  267. goog.exportSymbol('G_testRunner.initialize', tr.initialize);
  268. goog.exportSymbol('G_testRunner.isInitialized', tr.isInitialized);
  269. goog.exportSymbol('G_testRunner.isFinished', tr.isFinished);
  270. goog.exportSymbol('G_testRunner.isSuccess', tr.isSuccess);
  271. goog.exportSymbol('G_testRunner.getReport', tr.getReport);
  272. goog.exportSymbol('G_testRunner.getRunTime', tr.getRunTime);
  273. goog.exportSymbol('G_testRunner.getNumFilesLoaded', tr.getNumFilesLoaded);
  274. goog.exportSymbol('G_testRunner.setStrict', tr.setStrict);
  275. goog.exportSymbol('G_testRunner.logTestFailure', tr.logTestFailure);
  276. // Export debug as a global function for JSUnit compatibility. This just
  277. // calls log on the current test case.
  278. if (!goog.global['debug']) {
  279. goog.exportSymbol('debug', goog.bind(tr.log, tr));
  280. }
  281. // Add an error handler to report errors that may occur during
  282. // initialization of the page.
  283. var onerror = window.onerror;
  284. window.onerror = function(error, url, line) {
  285. // Call any existing onerror handlers.
  286. if (onerror) {
  287. onerror(error, url, line);
  288. }
  289. if (typeof error == 'object') {
  290. // Webkit started passing an event object as the only argument to
  291. // window.onerror. It doesn't contain an error message, url or line
  292. // number. We therefore log as much info as we can.
  293. if (error.target && error.target.tagName == 'SCRIPT') {
  294. tr.logError('UNKNOWN ERROR: Script ' + error.target.src);
  295. } else {
  296. tr.logError('UNKNOWN ERROR: No error information available.');
  297. }
  298. } else {
  299. tr.logError('JS ERROR: ' + error + '\nURL: ' + url + '\nLine: ' + line);
  300. }
  301. };
  302. var onload = window.onload;
  303. window.onload = function() {
  304. // Call any existing onload handlers.
  305. if (onload) {
  306. onload();
  307. }
  308. var testCase = new webdriver.testing.TestCase(client, document.title);
  309. testCase.autoDiscoverTests();
  310. tr.initialize(testCase);
  311. tr.execute();
  312. };
  313. })();