Code coverage report for lib/main.js

Statements: 97.75% (87 / 89)      Branches: 92.68% (38 / 41)      Functions: 92.86% (13 / 14)      Lines: 97.75% (87 / 89)     

All files » lib/ » main.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 2521 1 1 1 1 1 1 1 1   1       1   80     80   3 3       80 1 1       80 1 1       80 1 1       80 5 5       80 4 4 4     80               80 80 1   80       1 96 96 20 76 16   60                 96   16 16       96                 1 37 37   37       1   1 1 1         1 28 34 34 34 34   28     1 8 8 8   8 8     1 26 26 26     1 60 60 60       1 8 8 8       1 51 51 51       1   1                                                               26                                                                                                    
var __slice = Array.prototype.slice;
var url = require('url');
var SPECIAL_KEYS = require('./special-keys');
var Webdriver = require('./webdriver');
var utils = require('./utils');
var deprecator = utils.deprecator;
var config = require('./config');
var _ = require("lodash");
var Q = require('q');
 
var factory = exports.factory = {
  WebDriver: Webdriver
};
 
function buildConfigUrl(remoteWdConfig)
{
  var configUrl = _(remoteWdConfig).clone();
 
  // for backward compatibility
  if( configUrl.host && (configUrl.host.indexOf(':') < 0) && configUrl.port )
  {
    configUrl.hostname = configUrl.host;
    delete configUrl.host;
  }
 
  // for backward compatibility
  if(configUrl.username){
    configUrl.user = configUrl.username;
    delete configUrl.username;
  }
 
  // for backward compatibility
  if(configUrl.accessKey){
    configUrl.pwd = configUrl.accessKey;
    delete configUrl.accessKey;
  }
 
  // for backward compatibility
  if(configUrl.https){
    configUrl.protocol = 'https:';
    delete configUrl.https;
  }
 
  // for backward compatibility
  if(configUrl.path){
    configUrl.pathname = configUrl.path;
    delete configUrl.path;
  }
 
  // setting auth from user/password
  if(configUrl.user && configUrl.pwd){
    configUrl.auth = configUrl.user + ':' + configUrl.pwd;
    delete configUrl.user;
    delete configUrl.pwd;
  }
 
  _.defaults(configUrl, {
    protocol: 'http:',
    hostname: '127.0.0.1',
    port: '4444',
    pathname: '/wd/hub'
  });
 
  // strip any trailing slashes from pathname
  var parsed = url.parse(url.format(configUrl), true);
  if (parsed.pathname[parsed.pathname.length - 1] === '/') {
    parsed.pathname = parsed.pathname.slice(0, parsed.pathname.length - 1);
  }
  return parsed;
}
 
// parses server parameters
var parseRemoteWdConfig = function(args) {
  var config;
  if ((typeof args[0]) === 'object') {
    config = buildConfigUrl( args[0] );
  } else if ((typeof args[0]) === 'string' && (args[0].match(/^https?:\/\//)))  {
    config = url.parse(args[0]);
  } else {
    config = buildConfigUrl( {
      hostname: args[0],
      port: args[1],
      user: args[2],
      pwd: args[3]
    } );
  }
 
  // saucelabs automatic config
  if( /saucelabs\.com/.exec(config.hostname) )
  {
    Eif(process.env.SAUCE_USERNAME && process.env.SAUCE_ACCESS_KEY){
      config.auth = process.env.SAUCE_USERNAME + ':' + process.env.SAUCE_ACCESS_KEY;
    }
  }
 
  return config;
};
 
// creates the webdriver object
// server parameters can be passed in 4 ways
// - as a url string
// - as a url object, constructed via url.parse
// - as a list of arguments host,port, user, pwd
// - as an option object containing the fields above
function remote() {
  var args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
  var rwc = parseRemoteWdConfig(args);
 
  return new factory.WebDriver(rwc);
}
 
 
var PromiseWebdriver, PromiseChainWebdriver;
 
function wrap() {
  PromiseWebdriver = require('./promise-webdriver')(factory.WebDriver, false);
  PromiseChainWebdriver = require('./promise-webdriver')(factory.WebDriver, true);
}
 
// todo: allow adding element methods
 
function addPromiseChainMethod(name, method) {
  var wrappedMethod = function() {
    var args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
    var promise = new Q(method.apply(this, args));
    this._enrich(promise);
    return promise;
  };
  PromiseChainWebdriver.prototype[name] = wrappedMethod;
}
 
function addPromiseMethod(name, method) {
  var wrappedMethod = function() {
    var args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
    return new Q(method.apply(this, args));
  };
  PromiseWebdriver.prototype[name] = wrappedMethod;
  addPromiseChainMethod(name, method);
}
 
function addAsyncMethod(name, method) {
  Webdriver.prototype[name] = method;  
  PromiseWebdriver.prototype[name] = PromiseWebdriver._wrapAsync(method);
  PromiseChainWebdriver.prototype[name] = PromiseChainWebdriver._wrapAsync(method);
}
 
function removeMethod(name) {
  delete Webdriver.prototype[name];
  delete PromiseWebdriver.prototype[name];
  delete PromiseChainWebdriver.prototype[name];  
}
 
// creates a webdriver object using the Q promise wrap not chained
function promiseRemote() {
  var args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
  var rwc = parseRemoteWdConfig(args);
  return new PromiseWebdriver(rwc);
}
 
// creates a webdriver object using the Q promise wrap chained
function promiseChainRemote() {
  var args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
  var rwc = parseRemoteWdConfig(args);
  return new PromiseChainWebdriver(rwc);
}
 
// initial wrapping
wrap();
 
module.exports = {
  // Retrieves browser
  remote: remote,
 
  // Retrieves wrap browser
  promiseRemote: promiseRemote,
  promiseChainRemote: promiseChainRemote,
 
  // Webdriver and Wrapper base classes
  Webdriver: factory.WebDriver,
  webdriver: factory.WebDriver, // for backward compatibility
  PromiseChainWebdriver: PromiseChainWebdriver,
  PromiseWebdriver: PromiseWebdriver,
 
  // Actualizes promise wrappers
  rewrap: function() {
    deprecator.warn('rewrap', 
    'rewrap has been deprecated, use addAsyncMethod instead.');  
    wrap();
  },
 
  // config
  /**
   * wd.configureHttp(opts)
   *
   * opts example:
   * {timeout:60000, retries: 3, 'retryDelay': 15, baseUrl='http://example.com/'}
   * more info in README.
   *
   * @wd
   */
  configureHttp: config.configureHttp,
  getHttpConfig: function() { return _(config.httpConfig).clone(); },
  
  // deprecation
  /**
   * wd.showHideDeprecation(boolean)
   *
   * @wd
   */ 
  showHideDeprecation: deprecator.showHideDeprecation.bind(deprecator), 
 
  // add/remove methods
  /**
   * wd.addAsyncMethod(name, func)
   *
   * @wd
   */ 
  addAsyncMethod: addAsyncMethod,
  /**
   * wd.addPromiseMethod(name, func)
   *
   * @wd
   */ 
  addPromiseMethod: addPromiseMethod,
  /**
   * wd.addPromiseChainMethod(name, func)
   *
   * @wd
   */ 
  addPromiseChainMethod: addPromiseChainMethod,
  /**
   * wd.removeMethod(name, func)
   *
   * @wd
   */ 
  removeMethod: removeMethod,
 
  // Useful stuff
  Asserter: require('./asserters').Asserter,
  asserters: require('./asserters'),
  SPECIAL_KEYS: SPECIAL_KEYS,
  Q: Q,
  findCallback: utils.findCallback,
  varargs: utils.varargs,
  transferPromiseness: utils.transferPromiseness,
 
  // This is for people who write wrapper
  // todo: That should not be needed.
  utils: utils
 
};