sauce.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. "use strict";
  2. /*
  3. * This is an implementation of the SauceLabs Driver Provider.
  4. * It is responsible for setting up the account object, tearing
  5. * it down, and setting up the driver correctly.
  6. */
  7. Object.defineProperty(exports, "__esModule", { value: true });
  8. const q = require("q");
  9. const util = require("util");
  10. const logger_1 = require("../logger");
  11. const driverProvider_1 = require("./driverProvider");
  12. const SauceLabs = require('saucelabs');
  13. const SAUCE_REGIONS = {
  14. 'us': '',
  15. 'eu': 'eu-central-1.'
  16. };
  17. let logger = new logger_1.Logger('sauce');
  18. class Sauce extends driverProvider_1.DriverProvider {
  19. constructor(config) {
  20. super(config);
  21. }
  22. /**
  23. * Hook to update the sauce job.
  24. * @public
  25. * @param {Object} update
  26. * @return {q.promise} A promise that will resolve when the update is complete.
  27. */
  28. updateJob(update) {
  29. let deferredArray = this.drivers_.map((driver) => {
  30. let deferred = q.defer();
  31. driver.getSession().then((session) => {
  32. logger.info('SauceLabs results available at http://saucelabs.com/jobs/' + session.getId());
  33. this.sauceServer_.updateJob(session.getId(), update, (err) => {
  34. if (err) {
  35. throw new Error('Error updating Sauce pass/fail status: ' + util.inspect(err));
  36. }
  37. deferred.resolve();
  38. });
  39. });
  40. return deferred.promise;
  41. });
  42. return q.all(deferredArray);
  43. }
  44. /**
  45. * Configure and launch (if applicable) the object's environment.
  46. * @public
  47. * @return {q.promise} A promise which will resolve when the environment is
  48. * ready to test.
  49. */
  50. setupDriverEnv() {
  51. let deferred = q.defer();
  52. this.sauceServer_ = new SauceLabs({
  53. hostname: this.getSauceEndpoint(this.config_.sauceRegion),
  54. username: this.config_.sauceUser,
  55. password: this.config_.sauceKey,
  56. agent: this.config_.sauceAgent,
  57. proxy: this.config_.sauceProxy
  58. });
  59. this.config_.capabilities['username'] = this.config_.sauceUser;
  60. this.config_.capabilities['accessKey'] = this.config_.sauceKey;
  61. this.config_.capabilities['build'] = this.config_.sauceBuild;
  62. let protocol = this.config_.sauceSeleniumUseHttp ? 'http://' : 'https://';
  63. let auth = protocol + this.config_.sauceUser + ':' + this.config_.sauceKey + '@';
  64. this.config_.seleniumAddress = auth +
  65. (this.config_.sauceSeleniumAddress ?
  66. this.config_.sauceSeleniumAddress :
  67. `ondemand.${this.getSauceEndpoint(this.config_.sauceRegion)}:443/wd/hub`);
  68. // Append filename to capabilities.name so that it's easier to identify
  69. // tests.
  70. if (this.config_.capabilities.name && this.config_.capabilities.shardTestFiles) {
  71. this.config_.capabilities.name +=
  72. (':' + this.config_.specs.toString().replace(/^.*[\\\/]/, ''));
  73. }
  74. logger.info('Using SauceLabs selenium server at ' +
  75. this.config_.seleniumAddress.replace(/\/\/.+@/, '//'));
  76. deferred.resolve();
  77. return deferred.promise;
  78. }
  79. /**
  80. * Get the Sauce Labs endpoint
  81. * @private
  82. * @param {string} region
  83. * @return {string} The endpoint that needs to be used
  84. */
  85. getSauceEndpoint(region) {
  86. const dc = region ?
  87. typeof SAUCE_REGIONS[region] !== 'undefined' ? SAUCE_REGIONS[region] : (region + '.') :
  88. '';
  89. return `${dc}saucelabs.com`;
  90. }
  91. }
  92. exports.Sauce = Sauce;
  93. //# sourceMappingURL=sauce.js.map