/testing/selenium-core/scripts/selenium-commandhandlers.js

http://datanucleus-appengine.googlecode.com/ · JavaScript · 377 lines · 257 code · 40 blank · 80 comment · 38 complexity · 08074b10965a1ee2b2513041d7558e8d MD5 · raw file

  1. /*
  2. * Copyright 2004 ThoughtWorks, Inc
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. // A naming convention used in this file:
  17. //
  18. //
  19. // - a "seleniumApi" is an instance of the Selenium object, defined in selenium-api.js.
  20. //
  21. // - a "Method" is an unbound function whose target must be supplied when it's called, ie.
  22. // it should be invoked using Function.call() or Function.apply()
  23. //
  24. // - a "Block" is a function that has been bound to a target object, so can be called invoked directly
  25. // (or with a null target)
  26. //
  27. // - "CommandHandler" is effectively an abstract base for
  28. // various handlers including ActionHandler, AccessorHandler and AssertHandler.
  29. // Subclasses need to implement an execute(seleniumApi, command) function,
  30. // where seleniumApi is the Selenium object, and command a SeleniumCommand object.
  31. //
  32. // - Handlers will return a "result" object (ActionResult, AccessorResult, AssertResult).
  33. // ActionResults may contain a .terminationCondition function which is run by
  34. // -executionloop.js after the command is run; we'll run it over and over again
  35. // until it returns true or the .terminationCondition throws an exception.
  36. // AccessorResults will contain the results of running getter (e.g. getTitle returns
  37. // the title as a string).
  38. var CommandHandlerFactory = classCreate();
  39. objectExtend(CommandHandlerFactory.prototype, {
  40. initialize: function() {
  41. this.handlers = {};
  42. },
  43. registerAction: function(name, actionBlock, wait, dontCheckAlertsAndConfirms) {
  44. this.handlers[name] = new ActionHandler(actionBlock, wait, dontCheckAlertsAndConfirms);
  45. },
  46. registerAccessor: function(name, accessBlock) {
  47. this.handlers[name] = new AccessorHandler(accessBlock);
  48. },
  49. registerAssert: function(name, assertBlock, haltOnFailure) {
  50. this.handlers[name] = new AssertHandler(assertBlock, haltOnFailure);
  51. },
  52. getCommandHandler: function(name) {
  53. return this.handlers[name];
  54. },
  55. _registerAllAccessors: function(seleniumApi) {
  56. // Methods of the form getFoo(target) result in commands:
  57. // getFoo, assertFoo, verifyFoo, assertNotFoo, verifyNotFoo
  58. // storeFoo, waitForFoo, and waitForNotFoo.
  59. for (var functionName in seleniumApi) {
  60. var match = /^(get|is)([A-Z].+)$/.exec(functionName);
  61. if (match) {
  62. var accessMethod = seleniumApi[functionName];
  63. var accessBlock = fnBind(accessMethod, seleniumApi);
  64. var baseName = match[2];
  65. var isBoolean = (match[1] == "is");
  66. var requiresTarget = (accessMethod.length == 1);
  67. this.registerAccessor(functionName, accessBlock);
  68. this._registerStoreCommandForAccessor(baseName, accessBlock, requiresTarget);
  69. var predicateBlock = this._predicateForAccessor(accessBlock, requiresTarget, isBoolean);
  70. this._registerAssertionsForPredicate(baseName, predicateBlock);
  71. this._registerWaitForCommandsForPredicate(seleniumApi, baseName, predicateBlock);
  72. }
  73. }
  74. },
  75. _registerAllActions: function(seleniumApi) {
  76. for (var functionName in seleniumApi) {
  77. var match = /^do([A-Z].+)$/.exec(functionName);
  78. if (match) {
  79. var actionName = match[1].lcfirst();
  80. var actionMethod = seleniumApi[functionName];
  81. var dontCheckPopups = actionMethod.dontCheckAlertsAndConfirms;
  82. var actionBlock = fnBind(actionMethod, seleniumApi);
  83. this.registerAction(actionName, actionBlock, false, dontCheckPopups);
  84. this.registerAction(actionName + "AndWait", actionBlock, true, dontCheckPopups);
  85. }
  86. }
  87. },
  88. _registerAllAsserts: function(seleniumApi) {
  89. for (var functionName in seleniumApi) {
  90. var match = /^assert([A-Z].+)$/.exec(functionName);
  91. if (match) {
  92. var assertBlock = fnBind(seleniumApi[functionName], seleniumApi);
  93. // Register the assert with the "assert" prefix, and halt on failure.
  94. var assertName = functionName;
  95. this.registerAssert(assertName, assertBlock, true);
  96. // Register the assert with the "verify" prefix, and do not halt on failure.
  97. var verifyName = "verify" + match[1];
  98. this.registerAssert(verifyName, assertBlock, false);
  99. }
  100. }
  101. },
  102. registerAll: function(seleniumApi) {
  103. this._registerAllAccessors(seleniumApi);
  104. this._registerAllActions(seleniumApi);
  105. this._registerAllAsserts(seleniumApi);
  106. },
  107. _predicateForAccessor: function(accessBlock, requiresTarget, isBoolean) {
  108. if (isBoolean) {
  109. return this._predicateForBooleanAccessor(accessBlock);
  110. }
  111. if (requiresTarget) {
  112. return this._predicateForSingleArgAccessor(accessBlock);
  113. }
  114. return this._predicateForNoArgAccessor(accessBlock);
  115. },
  116. _predicateForSingleArgAccessor: function(accessBlock) {
  117. // Given an accessor function getBlah(target),
  118. // return a "predicate" equivalient to isBlah(target, value) that
  119. // is true when the value returned by the accessor matches the specified value.
  120. return function(target, value) {
  121. var accessorResult = accessBlock(target);
  122. accessorResult = selArrayToString(accessorResult);
  123. if (PatternMatcher.matches(value, accessorResult)) {
  124. return new PredicateResult(true, "Actual value '" + accessorResult + "' did match '" + value + "'");
  125. } else {
  126. return new PredicateResult(false, "Actual value '" + accessorResult + "' did not match '" + value + "'");
  127. }
  128. };
  129. },
  130. _predicateForNoArgAccessor: function(accessBlock) {
  131. // Given a (no-arg) accessor function getBlah(),
  132. // return a "predicate" equivalient to isBlah(value) that
  133. // is true when the value returned by the accessor matches the specified value.
  134. return function(value) {
  135. var accessorResult = accessBlock();
  136. accessorResult = selArrayToString(accessorResult);
  137. if (PatternMatcher.matches(value, accessorResult)) {
  138. return new PredicateResult(true, "Actual value '" + accessorResult + "' did match '" + value + "'");
  139. } else {
  140. return new PredicateResult(false, "Actual value '" + accessorResult + "' did not match '" + value + "'");
  141. }
  142. };
  143. },
  144. _predicateForBooleanAccessor: function(accessBlock) {
  145. // Given a boolean accessor function isBlah(),
  146. // return a "predicate" equivalient to isBlah() that
  147. // returns an appropriate PredicateResult value.
  148. return function() {
  149. var accessorResult;
  150. if (arguments.length > 2) throw new SeleniumError("Too many arguments! " + arguments.length);
  151. if (arguments.length == 2) {
  152. accessorResult = accessBlock(arguments[0], arguments[1]);
  153. } else if (arguments.length == 1) {
  154. accessorResult = accessBlock(arguments[0]);
  155. } else {
  156. accessorResult = accessBlock();
  157. }
  158. if (accessorResult) {
  159. return new PredicateResult(true, "true");
  160. } else {
  161. return new PredicateResult(false, "false");
  162. }
  163. };
  164. },
  165. _invertPredicate: function(predicateBlock) {
  166. // Given a predicate, return the negation of that predicate.
  167. // Leaves the message unchanged.
  168. // Used to create assertNot, verifyNot, and waitForNot commands.
  169. return function(target, value) {
  170. var result = predicateBlock(target, value);
  171. result.isTrue = !result.isTrue;
  172. return result;
  173. };
  174. },
  175. createAssertionFromPredicate: function(predicateBlock) {
  176. // Convert an isBlahBlah(target, value) function into an assertBlahBlah(target, value) function.
  177. return function(target, value) {
  178. var result = predicateBlock(target, value);
  179. if (!result.isTrue) {
  180. Assert.fail(result.message);
  181. }
  182. };
  183. },
  184. _invertPredicateName: function(baseName) {
  185. var matchResult = /^(.*)Present$/.exec(baseName);
  186. if (matchResult != null) {
  187. return matchResult[1] + "NotPresent";
  188. }
  189. return "Not" + baseName;
  190. },
  191. _registerAssertionsForPredicate: function(baseName, predicateBlock) {
  192. // Register an assertion, a verification, a negative assertion,
  193. // and a negative verification based on the specified accessor.
  194. var assertBlock = this.createAssertionFromPredicate(predicateBlock);
  195. this.registerAssert("assert" + baseName, assertBlock, true);
  196. this.registerAssert("verify" + baseName, assertBlock, false);
  197. var invertedPredicateBlock = this._invertPredicate(predicateBlock);
  198. var negativeassertBlock = this.createAssertionFromPredicate(invertedPredicateBlock);
  199. this.registerAssert("assert" + this._invertPredicateName(baseName), negativeassertBlock, true);
  200. this.registerAssert("verify" + this._invertPredicateName(baseName), negativeassertBlock, false);
  201. },
  202. _waitForActionForPredicate: function(predicateBlock) {
  203. // Convert an isBlahBlah(target, value) function into a waitForBlahBlah(target, value) function.
  204. return function(target, value) {
  205. var terminationCondition = function () {
  206. try {
  207. return predicateBlock(target, value).isTrue;
  208. } catch (e) {
  209. // Treat exceptions as meaning the condition is not yet met.
  210. // Useful, for example, for waitForValue when the element has
  211. // not even been created yet.
  212. // TODO: possibly should rethrow some types of exception.
  213. return false;
  214. }
  215. };
  216. return Selenium.decorateFunctionWithTimeout(terminationCondition, this.defaultTimeout);
  217. };
  218. },
  219. _registerWaitForCommandsForPredicate: function(seleniumApi, baseName, predicateBlock) {
  220. // Register a waitForBlahBlah and waitForNotBlahBlah based on the specified accessor.
  221. var waitForActionMethod = this._waitForActionForPredicate(predicateBlock);
  222. var waitForActionBlock = fnBind(waitForActionMethod, seleniumApi);
  223. var invertedPredicateBlock = this._invertPredicate(predicateBlock);
  224. var waitForNotActionMethod = this._waitForActionForPredicate(invertedPredicateBlock);
  225. var waitForNotActionBlock = fnBind(waitForNotActionMethod, seleniumApi);
  226. this.registerAction("waitFor" + baseName, waitForActionBlock, false, true);
  227. this.registerAction("waitFor" + this._invertPredicateName(baseName), waitForNotActionBlock, false, true);
  228. //TODO decide remove "waitForNot.*Present" action name or not
  229. //for the back compatiblity issues we still make waitForNot.*Present availble
  230. this.registerAction("waitForNot" + baseName, waitForNotActionBlock, false, true);
  231. },
  232. _registerStoreCommandForAccessor: function(baseName, accessBlock, requiresTarget) {
  233. var action;
  234. if (requiresTarget) {
  235. action = function(target, varName) {
  236. storedVars[varName] = accessBlock(target);
  237. };
  238. } else {
  239. action = function(varName) {
  240. storedVars[varName] = accessBlock();
  241. };
  242. }
  243. this.registerAction("store" + baseName, action, false, true);
  244. }
  245. });
  246. function PredicateResult(isTrue, message) {
  247. this.isTrue = isTrue;
  248. this.message = message;
  249. }
  250. // NOTE: The CommandHandler is effectively an abstract base for
  251. // various handlers including ActionHandler, AccessorHandler and AssertHandler.
  252. // Subclasses need to implement an execute(seleniumApi, command) function,
  253. // where seleniumApi is the Selenium object, and command a SeleniumCommand object.
  254. function CommandHandler(type, haltOnFailure) {
  255. this.type = type;
  256. this.haltOnFailure = haltOnFailure;
  257. }
  258. // An ActionHandler is a command handler that executes the sepcified action,
  259. // possibly checking for alerts and confirmations (if checkAlerts is set), and
  260. // possibly waiting for a page load if wait is set.
  261. function ActionHandler(actionBlock, wait, dontCheckAlerts) {
  262. this.actionBlock = actionBlock;
  263. CommandHandler.call(this, "action", true);
  264. if (wait) {
  265. this.wait = true;
  266. }
  267. // note that dontCheckAlerts could be undefined!!!
  268. this.checkAlerts = (dontCheckAlerts) ? false : true;
  269. }
  270. ActionHandler.prototype = new CommandHandler;
  271. ActionHandler.prototype.execute = function(seleniumApi, command) {
  272. if (this.checkAlerts && (null == /(Alert|Confirmation)(Not)?Present/.exec(command.command))) {
  273. // todo: this conditional logic is ugly
  274. seleniumApi.ensureNoUnhandledPopups();
  275. }
  276. var terminationCondition = this.actionBlock(command.target, command.value);
  277. // If the handler didn't return a wait flag, check to see if the
  278. // handler was registered with the wait flag.
  279. if (terminationCondition == undefined && this.wait) {
  280. terminationCondition = seleniumApi.makePageLoadCondition();
  281. }
  282. return new ActionResult(terminationCondition);
  283. };
  284. function ActionResult(terminationCondition) {
  285. this.terminationCondition = terminationCondition;
  286. }
  287. function AccessorHandler(accessBlock) {
  288. this.accessBlock = accessBlock;
  289. CommandHandler.call(this, "accessor", true);
  290. }
  291. AccessorHandler.prototype = new CommandHandler;
  292. AccessorHandler.prototype.execute = function(seleniumApi, command) {
  293. var returnValue = this.accessBlock(command.target, command.value);
  294. return new AccessorResult(returnValue);
  295. };
  296. function AccessorResult(result) {
  297. this.result = result;
  298. }
  299. /**
  300. * Handler for assertions and verifications.
  301. */
  302. function AssertHandler(assertBlock, haltOnFailure) {
  303. this.assertBlock = assertBlock;
  304. CommandHandler.call(this, "assert", haltOnFailure || false);
  305. }
  306. AssertHandler.prototype = new CommandHandler;
  307. AssertHandler.prototype.execute = function(seleniumApi, command) {
  308. var result = new AssertResult();
  309. try {
  310. this.assertBlock(command.target, command.value);
  311. } catch (e) {
  312. // If this is not a AssertionFailedError, or we should haltOnFailure, rethrow.
  313. if (!e.isAssertionFailedError) {
  314. throw e;
  315. }
  316. if (this.haltOnFailure) {
  317. var error = new SeleniumError(e.failureMessage);
  318. throw error;
  319. }
  320. result.setFailed(e.failureMessage);
  321. }
  322. return result;
  323. };
  324. function AssertResult() {
  325. this.passed = true;
  326. }
  327. AssertResult.prototype.setFailed = function(message) {
  328. this.passed = null;
  329. this.failed = true;
  330. this.failureMessage = message;
  331. }
  332. function SeleniumCommand(command, target, value, isBreakpoint) {
  333. this.command = command;
  334. this.target = target;
  335. this.value = value;
  336. this.isBreakpoint = isBreakpoint;
  337. }