Selenium-webdriver and HTTP CONNECT tunneling

If you are running selenium-webdriver within an environment that requires an HTTPS proxy to connect to the outside world in order to drive your tests, such as when using the Browserstack webdriver hub, then you may need to configure a webdriver proxy with an HTTP CONNECT tunneling httpAgent, like so:

import { Builder } from 'selenium-webdriver';
import { httpsOverHttp } from 'tunnel-agent';
import url from 'url';

# other stuff ...
const proxyUrl = url.parse(process.env.HTTP_PROXY);

driver = new Builder()
  .usingServer('https://hub.browserstack.com/wd/hub')
  .withCapabilities({ ... })
  .usingHttpAgent(httpsOverHttp({
    proxy: {
      host: proxyUrl.hostname,
      port: proxyUrl.port
    }
  }))
  .build();