extender.js 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. let Command = require('selenium-webdriver/lib/command').Command;
  4. class Extender {
  5. constructor(driver) {
  6. this.driver_ = driver;
  7. this.params_ = {};
  8. this.executor_ =
  9. driver.getExecutor ? driver.getExecutor() : driver.executor_;
  10. }
  11. /**
  12. * Defines a new command. When a command is sent, the {@code path} will be
  13. * preprocessed using the command's parameters; any path segments prefixed
  14. * with ":" will be replaced by the parameter of the same name. For example,
  15. * given "/person/:name" and the parameters "{name: 'Bob'}", the final command
  16. * path will be "/person/Bob".
  17. *
  18. * @param {string} name The command name.
  19. * @param {string} params The names of the parameters to the command
  20. * @param {string} method The HTTP method to use when sending this command.
  21. * @param {string} path The path to send the command to, relative to
  22. * the WebDriver server's command root and of the form
  23. * "/path/:variable/segment".
  24. */
  25. defineCommand(name, params, method, path) {
  26. this.executor_.defineCommand(name, method, path);
  27. this.params_[method + ':' + name] = params;
  28. }
  29. /**
  30. * Executes a command which was defined by defineCommand()
  31. *
  32. * @param {string} name The command name.
  33. * @param {*[]} params The parameters to the command
  34. * @return {webdriver.promise.Promise<*>} A promise that will be resolved with
  35. * the command result
  36. */
  37. execCommand(name, method, params) {
  38. var paramNames = this.params_[method + ':' + name];
  39. if (paramNames === undefined) {
  40. throw new RangeError('The command "' + name + '" has not yet been defined');
  41. }
  42. if (paramNames.length !== params.length) {
  43. throw new RangeError('The command "' + name + '" expected ' + paramNames.length + ' parameters, got ' +
  44. params.length);
  45. }
  46. var command = new Command(name);
  47. for (var i = 0; i < params.length; i++) {
  48. if (params[i] !== undefined) {
  49. command.setParameter(paramNames[i], params[i]);
  50. }
  51. }
  52. return this.driver_.schedule(command, 'Custom Command: ' + name + '(' +
  53. params
  54. .map((x) => {
  55. if ((typeof x == 'number') || (typeof x == 'boolean') ||
  56. (typeof x == 'function')) {
  57. return x.toString();
  58. }
  59. else if (x == null) {
  60. return '' + x;
  61. }
  62. else {
  63. return JSON.stringify(x);
  64. }
  65. })
  66. .join(', ') +
  67. ')');
  68. }
  69. }
  70. exports.Extender = Extender;
  71. //# sourceMappingURL=extender.js.map