Skip to main content
Transform your testing process with: Real Device Features, Company-wide Licences, & Test Observability
No Result Found

Run tests behind proxy

Run Selenium tests on BrowserStack from behind a proxy server

  • This page is applicable if your test scripts will be run from a system that is behind a proxy.
  • If the website under test is privately hosted (for e.g. https://localhost:3000) and behind the same/different proxy then please follow the Local testing behind proxy documentation.

If your machine is behind a proxy, you won’t be able to connect to BrowserStack’s remote browsers directly. You need to set your proxy settings, so that the connections go through normally. Here is how you do it:

If you are using BrowserStack SDK, set your proxy details within the httpProxy capability in the browserstack.yml file as shown in the following example:

For HTTP

browserstack.yml
Copy icon Copy

For HTTPS

browserstack.yml
Copy icon Copy

You can use either Selenium 4 or Selenium Legacy JSON to organize your tests.

BrowserStack SDK is a plug-n-play solution that takes care of all the integration steps for you. Using the BrowserStack SDK is the recommended integration method for your project. To know more, visit the SDK core concepts page.

//For HTTP

System.getProperties().put("http.proxyHost", "<HOST>");
System.getProperties().put("http.proxyPort", "<PORT>");
System.getProperties().put("http.proxyUser", "<USER>");
System.getProperties().put("http.proxyPassword", "<PASSWORD>");

//For HTTPS

System.getProperties().put("https.proxyHost", "<HOST>");
System.getProperties().put("https.proxyPort", "<PORT>");
System.getProperties().put("https.proxyUser", "<USER>");
System.getProperties().put("https.proxyPassword", "<PASSWORD>");

Remember that System.getProperties().put will not change your system properties. It only modifies the Property object within the scope of the running program. Once the program is executed, these properties will no longer be accessible. Hence you need to set these every time your test suite is run.

var driver = new webdriver.Builder().
  usingServer('https://hub-cloud.browserstack.com/wd/hub').
  withCapabilities(capabilities).
  usingWebDriverProxy("http://proxy.org:8080").  // use your proxy host and port here
  build();

Note: Your proxy should be able to support https requests if the protocol of remote webdriver is https


In case of client bindings v3.10 and above, add the following in your test script:

HttpCommandExecutor commandExecutor = new HttpCommandExecutor(new Uri("https://YOUR_USERNAME:YOUR_ACCESS_KEY@hub-cloud.browserstack.com/wd/hub/"), TimeSpan.FromSeconds(60));
commandExecutor.Proxy = new WebProxy("http://<HOST>:<PORT>", false, null, new NetworkCredential("<USER>", "<PASSWORD>"));
// add caps, for e.g.
// ChromeOptions options = new ChromeOptions();
// options.AddAdditionalCapability("browser", "Chrome",true);
IWebDriver driver = new RemoteWebDriver(commandExecutor, options.ToCapabilities());


In case of client bindings up to v3.9.1, add the following proxy parameters in your project config file:

<configuration>  
  <system.net>  
    <defaultProxy enabled="true">  
      <proxy  
        proxyaddress="http://host:port"  
        bypassonlocal="false"  
      />  
    </defaultProxy>  
  </system.net>  
</configuration>


$web_driver = RemoteWebDriver::create(HUB_URL, $capabilities, null, null, PROXY_URL, PROXY_PORT);


Environment variables can’t be used in Python as it doesn’t interpret them, hence it is required to modify the client binding code:

1. Locate the client binding code on your system, that is, search for selenium folder in python/site-packages directory. Refer the link to search for python modules on your system.

2. Navigate to selenium/webdriver/remote/ directory and open remote_connection.py file in your editor.

3. Search for code line http = urllib3.PoolManager(timeout=self._timeout) and change it to http = urllib3.ProxyManager('host:port').

ENV['http_proxy'] = 'host:port'
use LWP::UserAgent;

my $ua = LWP::UserAgent->new;
$ua->proxy(['http'], 'http://localhost:8117');
my $driver = Selenium::Remote::Driver->new('ua'=>$ua);

The above code snippet should be included before you create the driver instance in your test script. The snippet will set relevant environment variables in your system such that your Selenium language binding knows how to reach BrowserStack cloud when the connection request is made.

We're sorry to hear that. Please share your feedback so we can do better

Contact our Support team for immediate help while we work on improving our docs.

We're continuously improving our docs. We'd love to know what you liked





Thank you for your valuable feedback

Is this page helping you?

Yes
No

We're sorry to hear that. Please share your feedback so we can do better

Contact our Support team for immediate help while we work on improving our docs.

We're continuously improving our docs. We'd love to know what you liked





Thank you for your valuable feedback!

Talk to an Expert
Download Copy Check Circle