Skip to content

Commit e88d918

Browse files
jgelonJurn van Mieghem
andauthored
Create proxy based on environment variables (#197)
Co-authored-by: Jurn van Mieghem <jurn.vanmieghem@ad.dfnld.nl>
1 parent a88cd6c commit e88d918

File tree

3 files changed

+57
-0
lines changed

3 files changed

+57
-0
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,9 @@ new DriverManager().SetUpDriver(new TaobaoPhantomConfig());
220220
new DriverManager().WithProxy(previouslyInitializedProxy).SetUpDriver(new ChromeConfig());
221221
```
222222

223+
You can also set the environment variables `HTTP_PROXY` and/or `HTTPS_PROXY` to use a proxy when retrieving the drivers.
224+
This does not require any changes in the setup of DriverManager in C#.
225+
223226
## Thanks
224227
Thanks to the following companies for generously providing their services/products to help improve this project:
225228

WebDriverManager.Tests/BinaryServiceTests.cs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System;
12
using System.IO;
23
using WebDriverManager.Helpers;
34
using WebDriverManager.Services.Impl;
@@ -7,6 +8,42 @@ namespace WebDriverManager.Tests
78
{
89
public class BinaryServiceTests : BinaryService
910
{
11+
[Fact]
12+
public void ProxySetBySystemVariableHttp()
13+
{
14+
const string url = "https://chromedriver.storage.googleapis.com/2.27/chromedriver_win32.zip";
15+
const string httpName = "HTTP_PROXY";
16+
const string proxyUrl = "http://myproxy:8080/";
17+
Environment.SetEnvironmentVariable(httpName, proxyUrl);
18+
CheckProxySystemVariables();
19+
// Remove to make sure it is not saved in later runs
20+
Environment.SetEnvironmentVariable(httpName, null);
21+
Assert.NotNull(Proxy);
22+
Assert.Equal(proxyUrl, Proxy.GetProxy(new Uri(url)).AbsoluteUri);
23+
}
24+
25+
[Fact]
26+
public void ProxySetBySystemVariableHttps()
27+
{
28+
const string url = "https://chromedriver.storage.googleapis.com/2.27/chromedriver_win32.zip";
29+
const string httpName = "HTTPS_PROXY";
30+
const string proxyUrl = "https://myproxy:8080/";
31+
Environment.SetEnvironmentVariable(httpName, proxyUrl);
32+
CheckProxySystemVariables();
33+
// Remove to make sure it is not saved in later runs
34+
Environment.SetEnvironmentVariable(httpName, null);
35+
Assert.NotNull(Proxy);
36+
Assert.Equal(proxyUrl, Proxy.GetProxy(new Uri(url)).AbsoluteUri);
37+
}
38+
39+
40+
[Fact]
41+
public void NoProxyBySystemVariable()
42+
{
43+
CheckProxySystemVariables();
44+
Assert.Null(Proxy);
45+
}
46+
1047
[Fact]
1148
public void DownloadZipResultNotEmpty()
1249
{

WebDriverManager/Services/Impl/BinaryService.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,8 @@ public string SetupBinary(string url, string zipPath, string binaryPath)
138138
public string DownloadZip(string url, string destination)
139139
{
140140
if (File.Exists(destination)) return destination;
141+
if (Proxy == null) CheckProxySystemVariables();
142+
141143
if (Proxy != null)
142144
{
143145
using (var webClient = new WebClient() {Proxy = Proxy})
@@ -155,6 +157,21 @@ public string DownloadZip(string url, string destination)
155157

156158
return destination;
157159
}
160+
protected void CheckProxySystemVariables()
161+
{
162+
const string nameHttp = "HTTP_PROXY";
163+
const string nameHttps = "HTTPS_PROXY";
164+
var httpProxyVariable = Environment.GetEnvironmentVariable(nameHttp, EnvironmentVariableTarget.Process);
165+
var httpsProxyVariable = Environment.GetEnvironmentVariable(nameHttps, EnvironmentVariableTarget.Process);
166+
if (!string.IsNullOrEmpty(httpProxyVariable))
167+
{
168+
Proxy = new WebProxy(httpProxyVariable);
169+
}
170+
else if (!string.IsNullOrEmpty(httpsProxyVariable))
171+
{
172+
Proxy = new WebProxy(httpsProxyVariable);
173+
}
174+
}
158175

159176
protected string UnZip(string path, string destination, string name)
160177
{

0 commit comments

Comments
 (0)