local.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. /*
  4. * This is an implementation of the Local Driver Provider.
  5. * It is responsible for setting up the account object, tearing
  6. * it down, and setting up the driver correctly.
  7. *
  8. * TODO - it would be nice to do this in the launcher phase,
  9. * so that we only start the local selenium once per entire launch.
  10. */
  11. const fs = require("fs");
  12. const path = require("path");
  13. const q = require("q");
  14. const exitCodes_1 = require("../exitCodes");
  15. const logger_1 = require("../logger");
  16. const driverProvider_1 = require("./driverProvider");
  17. const SeleniumConfig = require('webdriver-manager/built/lib/config').Config;
  18. const remote = require('selenium-webdriver/remote');
  19. let logger = new logger_1.Logger('local');
  20. class Local extends driverProvider_1.DriverProvider {
  21. constructor(config) {
  22. super(config);
  23. this.server_ = null;
  24. }
  25. /**
  26. * Helper to locate the default jar path if none is provided by the user.
  27. * @private
  28. */
  29. addDefaultBinaryLocs_() {
  30. if (!this.config_.seleniumServerJar) {
  31. logger.debug('Attempting to find the SeleniumServerJar in the default ' +
  32. 'location used by webdriver-manager');
  33. try {
  34. let updateJson = path.resolve(SeleniumConfig.getSeleniumDir(), 'update-config.json');
  35. let updateConfig = JSON.parse(fs.readFileSync(updateJson).toString());
  36. this.config_.seleniumServerJar = updateConfig.standalone.last;
  37. }
  38. catch (err) {
  39. throw new exitCodes_1.BrowserError(logger, 'No update-config.json found.' +
  40. ' Run \'webdriver-manager update\' to download binaries.');
  41. }
  42. }
  43. if (!fs.existsSync(this.config_.seleniumServerJar)) {
  44. throw new exitCodes_1.BrowserError(logger, 'No selenium server jar found at ' + this.config_.seleniumServerJar +
  45. '. Run \'webdriver-manager update\' to download binaries.');
  46. }
  47. if (this.config_.capabilities.browserName === 'chrome') {
  48. if (!this.config_.chromeDriver) {
  49. logger.debug('Attempting to find the chromedriver binary in the default ' +
  50. 'location used by webdriver-manager');
  51. try {
  52. let updateJson = path.resolve(SeleniumConfig.getSeleniumDir(), 'update-config.json');
  53. let updateConfig = JSON.parse(fs.readFileSync(updateJson).toString());
  54. this.config_.chromeDriver = updateConfig.chrome.last;
  55. }
  56. catch (err) {
  57. throw new exitCodes_1.BrowserError(logger, 'No update-config.json found. ' +
  58. 'Run \'webdriver-manager update\' to download binaries.');
  59. }
  60. }
  61. // Check if file exists, if not try .exe or fail accordingly
  62. if (!fs.existsSync(this.config_.chromeDriver)) {
  63. if (fs.existsSync(this.config_.chromeDriver + '.exe')) {
  64. this.config_.chromeDriver += '.exe';
  65. }
  66. else {
  67. throw new exitCodes_1.BrowserError(logger, 'Could not find chromedriver at ' + this.config_.chromeDriver +
  68. '. Run \'webdriver-manager update\' to download binaries.');
  69. }
  70. }
  71. }
  72. if (this.config_.capabilities.browserName === 'firefox') {
  73. if (!this.config_.geckoDriver) {
  74. logger.debug('Attempting to find the gecko driver binary in the default ' +
  75. 'location used by webdriver-manager');
  76. try {
  77. let updateJson = path.resolve(SeleniumConfig.getSeleniumDir(), 'update-config.json');
  78. let updateConfig = JSON.parse(fs.readFileSync(updateJson).toString());
  79. this.config_.geckoDriver = updateConfig.gecko.last;
  80. }
  81. catch (err) {
  82. throw new exitCodes_1.BrowserError(logger, 'No update-config.json found. ' +
  83. 'Run \'webdriver-manager update\' to download binaries.');
  84. }
  85. }
  86. // Check if file exists, if not try .exe or fail accordingly
  87. if (!fs.existsSync(this.config_.geckoDriver)) {
  88. if (fs.existsSync(this.config_.geckoDriver + '.exe')) {
  89. this.config_.geckoDriver += '.exe';
  90. }
  91. else {
  92. throw new exitCodes_1.BrowserError(logger, 'Could not find gecko driver at ' + this.config_.geckoDriver +
  93. '. Run \'webdriver-manager update\' to download binaries.');
  94. }
  95. }
  96. }
  97. }
  98. /**
  99. * Configure and launch (if applicable) the object's environment.
  100. * @public
  101. * @return {q.promise} A promise which will resolve when the environment is
  102. * ready to test.
  103. */
  104. setupDriverEnv() {
  105. this.addDefaultBinaryLocs_();
  106. logger.info('Starting selenium standalone server...');
  107. let serverConf = this.config_.localSeleniumStandaloneOpts || {};
  108. // If args or port is not set use seleniumArgs and seleniumPort
  109. // for backward compatibility
  110. if (serverConf.args === undefined) {
  111. serverConf.args = this.config_.seleniumArgs || [];
  112. }
  113. if (serverConf.jvmArgs === undefined) {
  114. serverConf.jvmArgs = this.config_.jvmArgs || [];
  115. }
  116. else {
  117. if (!Array.isArray(serverConf.jvmArgs)) {
  118. throw new exitCodes_1.ConfigError(logger, 'jvmArgs should be an array.');
  119. }
  120. }
  121. if (serverConf.port === undefined) {
  122. serverConf.port = this.config_.seleniumPort;
  123. }
  124. // configure server
  125. if (this.config_.chromeDriver) {
  126. serverConf.jvmArgs.push('-Dwebdriver.chrome.driver=' + this.config_.chromeDriver);
  127. }
  128. if (this.config_.geckoDriver) {
  129. serverConf.jvmArgs.push('-Dwebdriver.gecko.driver=' + this.config_.geckoDriver);
  130. }
  131. this.server_ = new remote.SeleniumServer(this.config_.seleniumServerJar, serverConf);
  132. let deferred = q.defer();
  133. // start local server, grab hosted address, and resolve promise
  134. this.server_.start(this.config_.seleniumServerStartTimeout)
  135. .then((url) => {
  136. logger.info('Selenium standalone server started at ' + url);
  137. return this.server_.address();
  138. })
  139. .then((address) => {
  140. this.config_.seleniumAddress = address;
  141. deferred.resolve();
  142. })
  143. .catch((err) => {
  144. deferred.reject(err);
  145. });
  146. return deferred.promise;
  147. }
  148. /**
  149. * Teardown and destroy the environment and do any associated cleanup.
  150. * Shuts down the drivers and server.
  151. *
  152. * @public
  153. * @override
  154. * @return {q.promise} A promise which will resolve when the environment
  155. * is down.
  156. */
  157. teardownEnv() {
  158. return super.teardownEnv().then(() => {
  159. logger.info('Shutting down selenium standalone server.');
  160. return this.server_.stop();
  161. });
  162. }
  163. }
  164. exports.Local = Local;
  165. //# sourceMappingURL=local.js.map