title: Extending selenium webdriver js
body: <h2>Preface</h2>
  <p>I'm trying to write a few extensions for <a href="https://npmjs.org/package/selenium-webdriver" rel="noreferrer">selenium-webdriver</a>, like so:</p>
  <pre><code>var webdriver = require('selenium-webdriver');
  var fs = require('fs');
  var resumer = require('resumer');
  
  webdriver.WebDriver.prototype.saveScreenshot = function(filename) {
      return this.takeScreenshot().then(function(data) {
          fs.writeFile(filename, data.replace(/^data:image\/png;base64,/,''), 'base64', function(err) {
              if(err) throw err;
          });
      });
  };
  
  webdriver.WebDriver.prototype.streamScreenshot = function() {
      var stream = resumer();
      this.takeScreenshot().then(function(data) {
          stream.queue(new Buffer(data.replace(/^data:image\/png;base64,/,''), 'base64')).end();
      });
      return stream;
  };
  
  module.exports = webdriver;
  </code></pre>
  <p>And then I just include my extended webdriver, instead of the official one:</p>
  <pre><code>var webdriver = require('./webdriver.ext');
  </code></pre>
  <p>I think that's the proper way to extend things in Node JS.</p>
  <h2>Problem</h2>
  <p>The issue I'm having is with adding a custom Locator Strategy. The strategies look like this in the source:</p>
  <pre><code>/**
   * Factory methods for the supported locator strategies.
   * @type {Object.&lt;function(string):!webdriver.Locator&gt;}
   */
  webdriver.Locator.Strategy = {
    'className': webdriver.Locator.factory_('class name'),
    'class name': webdriver.Locator.factory_('class name'),
    'css': webdriver.Locator.factory_('css selector'),
    'id': webdriver.Locator.factory_('id'),
    'js': webdriver.Locator.factory_('js'),
    'linkText': webdriver.Locator.factory_('link text'),
    'link text': webdriver.Locator.factory_('link text'),
    'name': webdriver.Locator.factory_('name'),
    'partialLinkText': webdriver.Locator.factory_('partial link text'),
    'partial link text': webdriver.Locator.factory_('partial link text'),
    'tagName': webdriver.Locator.factory_('tag name'),
    'tag name': webdriver.Locator.factory_('tag name'),
    'xpath': webdriver.Locator.factory_('xpath')
  };
  goog.exportSymbol('By', webdriver.Locator.Strategy);
  </code></pre>
  <p>I'm trying to add a new one by injecting it into that object:</p>
  <pre><code>webdriver.By.sizzle = function(selector) {
      driver.executeScript(&quot;return typeof Sizzle==='undefined'&quot;).then(function(noSizzle) {
          if(noSizzle) driver.executeScript(fs.readFileSync('sizzle.min.js', {encoding: 'utf8'}));
      });
      return new webdriver.By.js(&quot;return Sizzle(&quot;+JSON.stringify(selector)+&quot;)[0]&quot;);
  };
  </code></pre>
  <p>This actually works fine for simple scripts where <code>driver</code> is defined (notice that I'm using a global variable).</p>
  <p>Is there a way to access the &quot;current driver&quot; inside my function? Unlike the methods at the top, this isn't a prototypical method, so I don't have access to <code>this</code>.</p>
  <p>I don't know how those <code>factory_</code>s work; I was just guessing that I could inject a function directly.</p>