Skip to content

Commit 73af46e

Browse files
drobannxAndreyBelym
authored andcommitted
Added ability to pass proxy options via environment variables (#7) (closes #6)
1 parent eacf14d commit 73af46e

2 files changed

Lines changed: 55 additions & 2 deletions

File tree

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,13 @@ testCafe
3636

3737
Tip: you can skip version (`@53.0`) or/and OS name (`:Windows 10`).
3838

39+
## Browserstack Proxy Options
40+
Proxy options can be passed via envrionment variables.
41+
42+
BROWSERSTACK_PROXY - This string needs to be in the form of `user:pass@proxyHostName:port`
43+
BROWERSTACK_LOCAL_PROXY - This string needs to be in the form of `user:pass@proxyHostName:port`
44+
BROWSERSTACK_FORCE_LOCAL - This can be true or omitted for false
45+
BROWSERSTACK_FORCE_PROXY - This can be true or omitted for false
46+
3947
## Author
4048
Developer Express Inc. (https://devexpress.com)

src/index.js

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import parseCapabilities from 'desired-capabilities';
55
import { Local as BrowserstackConnector } from 'browserstack-local';
66
import jimp from 'jimp';
77
import OS from 'os-family';
8-
8+
import nodeUrl from 'url';
99

1010
const TESTS_TIMEOUT = process.env['BROWSERSTACK_TEST_TIMEOUT'] || 1800;
1111
const BROWSERSTACK_CONNECTOR_DELAY = 10000;
@@ -42,16 +42,61 @@ function delay (ms) {
4242
return new Promise(resolve => setTimeout(resolve, ms));
4343
}
4444

45+
function getProxyOptions (environmentVariable) {
46+
var browserstackProxy = process.env[environmentVariable];
47+
var proxyOptions;
48+
49+
if (browserstackProxy) {
50+
var proxyURL = nodeUrl.parse('http://' + browserstackProxy);
51+
52+
if (proxyURL) {
53+
proxyOptions = {};
54+
55+
if (proxyURL.auth) {
56+
var auth = proxyURL.auth.split(':');
57+
58+
proxyOptions.user = auth[0];
59+
proxyOptions.password = auth.length === 2 ? auth[1] : '';
60+
}
61+
62+
if (proxyURL.host) {
63+
proxyOptions.host = proxyURL.host.hostname;
64+
proxyOptions.port = proxyURL.host.port;
65+
}
66+
}
67+
}
68+
69+
return proxyOptions;
70+
}
71+
4572
function createBrowserStackConnector (accessKey) {
4673
return new Promise((resolve, reject) => {
4774
var connector = new BrowserstackConnector();
75+
var proxyOptions = getProxyOptions('BROWSERSTACK_PROXY');
76+
var localProxyOptions = getProxyOptions('BROWSERSTACK_LOCAL_PROXY');
4877

4978
var opts = {
5079
'key': accessKey,
5180
'logfile': OS.win ? 'NUL' : '/dev/null',
52-
'enable-logging-for-api': true
81+
'enable-logging-for-api': true,
82+
'forceLocal': process.env['BROWSERSTACK_FORCE_LOCAL'] || false,
83+
'forceProxy': process.env['BROWSERSTACK_FORCE_PROXY'] || false
5384
};
5485

86+
if (proxyOptions) {
87+
opts.proxyUser = proxyOptions.user;
88+
opts.proxyPass = proxyOptions.password;
89+
opts.proxyHost = proxyOptions.host;
90+
opts.proxyPort = proxyOptions.port;
91+
}
92+
93+
if (localProxyOptions) {
94+
opts['local-proxy-user'] = localProxyOptions.user;
95+
opts['local-proxy-pass'] = localProxyOptions.password;
96+
opts['local-proxy-host'] = localProxyOptions.host;
97+
opts['local-proxy-port'] = localProxyOptions.port;
98+
}
99+
55100
connector.start(opts, err => {
56101
if (err) {
57102
reject(err);

0 commit comments

Comments
 (0)