Skip to content

Commit ac4ecbf

Browse files
committed
Refactoring
1 parent 9b1ffd9 commit ac4ecbf

17 files changed

+288
-299
lines changed

WebDriverManager/BrowserManagers/AppiumDriverManager.cs

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,36 +9,37 @@
99

1010
public class AppiumDriverManager : Logging, IBaseBrowserManager
1111
{
12-
private readonly string installationCommand = "/SP- /silent /noicons /closeapplications /dir=expand:%1";
12+
private const string InstallationCommand = "/SP- /silent /noicons /closeapplications /dir=expand:%1";
1313

1414
/// <summary>
1515
/// Set target appium driver architecture to x32 by default because of only 32 architecture presented
1616
/// </summary>
17-
WebDriverManagerConfig config = new WebDriverManagerConfig
17+
private readonly WebDriverManagerConfig _config = new WebDriverManagerConfig
1818
{
19-
binary = "appium-installer.exe",
20-
url = "https://bitbucket.org/appium/appium.app/downloads/AppiumForWindows_<version>.zip",
21-
pathVariable = "appium.binary.path",
22-
architecture = Architecture.x32.ToString()
19+
Binary = "appium-installer.exe",
20+
Url = "https://bitbucket.org/appium/appium.app/downloads/AppiumForWindows_<version>.zip",
21+
PathVariable = "appium.binary.path",
22+
Architecture = Architecture.X32.ToString()
2323
};
2424

2525
public string GetLatestVersion()
2626
{
2727
try
2828
{
29-
using (WebClient client = new WebClient())
29+
using (var client = new WebClient())
3030
{
31-
string version = null;
3231
var doc = new HtmlDocument();
3332
var htmlCode = client.DownloadString("https://bitbucket.org/appium/appium.app/downloads");
3433
doc.LoadHtml(htmlCode);
35-
var itemList = doc.DocumentNode.SelectNodes("//tr[@class='iterable-item']/td[@class='name']/a[contains(.,'AppiumForWindows_')]").Select(p => p.InnerText).ToList();
34+
var itemList =
35+
doc.DocumentNode.SelectNodes(
36+
"//tr[@class='iterable-item']/td[@class='name']/a[contains(.,'AppiumForWindows_')]")
37+
.Select(p => p.InnerText)
38+
.ToList();
3639
var item = itemList.FirstOrDefault();
37-
version = item.Substring(item.IndexOf(item.Split('_')[1])).Split('.')[0];
38-
if (version != null || version != string.Empty)
39-
Log?.Info($"Latest appium driver version is '{version}'");
40-
else
41-
Log?.Warn($"Problem with getting latest appium driver version. Parsed version is '{version}'");
40+
var version = item?.Substring(item.IndexOf(item.Split('_')[1], StringComparison.Ordinal))
41+
.Split('.')[0];
42+
Log?.Info($"Latest appium driver version is '{version}'");
4243
return version;
4344
}
4445
}
@@ -50,37 +51,35 @@ public string GetLatestVersion()
5051
}
5152

5253
public AppiumDriverManager()
53-
: base()
5454
{
55-
config.version = GetLatestVersion();
55+
_config.Version = GetLatestVersion();
5656
}
5757

5858
public AppiumDriverManager(string version)
59-
: base()
6059
{
61-
config.version = version;
60+
_config.Version = version;
6261
Log?.Info($"Set appium driver version to: '{version}'");
6362
}
6463

6564
public void Init()
6665
{
67-
config.destication = Path.Combine(Directory.GetCurrentDirectory(), config.DefaultDestinationFolder);
66+
_config.Destication = Path.Combine(Directory.GetCurrentDirectory(), _config.DefaultDestinationFolder);
6867
Base();
6968
}
7069

7170
public void Init(string destination)
7271
{
73-
config.destication = destination;
72+
_config.Destication = destination;
7473
Log?.Info($"Set custom appium driver destination path to: '{destination}'");
7574
Base();
7675
}
7776

7877
public void Base()
7978
{
80-
WebDriverManager.Download(config);
81-
WebDriverManager.Unzip(config);
79+
WebDriverManager.Download(_config);
80+
WebDriverManager.Unzip(_config);
8281
WebDriverManager.Clean();
83-
WebDriverManager.Install(installationCommand);
82+
WebDriverManager.Install(InstallationCommand);
8483
}
8584
}
86-
}
85+
}

WebDriverManager/BrowserManagers/ChromeDriverManager.cs

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -10,34 +10,35 @@ public class ChromeDriverManager : Logging, IBaseBrowserManager
1010
/// <summary>
1111
/// Set target chrome driver architecture to x32 by default because of only 32 architecture presented
1212
/// </summary>
13-
WebDriverManagerConfig config = new WebDriverManagerConfig
13+
private readonly WebDriverManagerConfig _config = new WebDriverManagerConfig
1414
{
15-
binary = "chromedriver.exe",
16-
url = "https://chromedriver.storage.googleapis.com/<version>/chromedriver_win<architecture>.zip",
17-
pathVariable = "webdriver.chrome.driver",
18-
architecture = Architecture.x32.ToString().Replace("x", "")
15+
Binary = "chromedriver.exe",
16+
Url = "https://chromedriver.storage.googleapis.com/<version>/chromedriver_win<architecture>.zip",
17+
PathVariable = "webdriver.chrome.driver",
18+
Architecture = Architecture.X32.ToString().Replace("x", "")
1919
};
2020

2121
public string GetLatestVersion()
2222
{
2323
try
2424
{
25-
string version = null;
2625
var webRequest = WebRequest.Create(@"https://chromedriver.storage.googleapis.com/LATEST_RELEASE");
27-
2826
using (var response = webRequest.GetResponse())
2927
{
3028
using (var content = response.GetResponseStream())
3129
{
32-
using (var reader = new StreamReader(content))
30+
if (content != null)
3331
{
34-
version = reader.ReadToEnd().Trim();
35-
if (version != null || version != string.Empty)
32+
using (var reader = new StreamReader(content))
33+
{
34+
var version = reader.ReadToEnd().Trim();
3635
Log?.Info($"Latest chrome driver version is '{version}'");
37-
else
38-
Log?.Warn($"Problem with getting latest chrome driver version. Parsed version is '{version}'");
39-
return version;
36+
return version;
37+
}
4038
}
39+
Log?.Error("Can't get content from URL");
40+
throw new WebDriverManagerException(
41+
"Can't get content from URL", new Exception());
4142
}
4243
}
4344
}
@@ -49,39 +50,37 @@ public string GetLatestVersion()
4950
}
5051

5152
public ChromeDriverManager()
52-
: base()
5353
{
54-
config.version = GetLatestVersion();
54+
_config.Version = GetLatestVersion();
5555
}
5656

5757
public ChromeDriverManager(string version)
58-
: base()
5958
{
60-
config.version = version;
59+
_config.Version = version;
6160
Log?.Info($"Set chrome driver version to: '{version}'");
6261
}
6362

6463
public void Init()
6564
{
66-
config.destication = Path.Combine(Directory.GetCurrentDirectory(), config.DefaultDestinationFolder);
67-
Log?.Debug($"Use default chrome driver destination path: '{config.destication}'");
65+
_config.Destication = Path.Combine(Directory.GetCurrentDirectory(), _config.DefaultDestinationFolder);
66+
Log?.Debug($"Use default chrome driver destination path: '{_config.Destication}'");
6867
Base();
6968
}
7069

7170
public void Init(string destination)
7271
{
73-
config.destication = destination;
72+
_config.Destication = destination;
7473
Log?.Info($"Set custom chrome driver destination path to: '{destination}'");
7574
Base();
7675
}
7776

7877
public void Base()
7978
{
80-
WebDriverManager.Download(config);
81-
WebDriverManager.Unzip(config);
79+
WebDriverManager.Download(_config);
80+
WebDriverManager.Unzip(_config);
8281
WebDriverManager.Clean();
83-
WebDriverManager.AddEnvironmentVariable(config.pathVariable);
84-
WebDriverManager.UpdatePath(config.pathVariable);
82+
WebDriverManager.AddEnvironmentVariable(_config.PathVariable);
83+
WebDriverManager.UpdatePath(_config.PathVariable);
8584
}
8685
}
87-
}
86+
}

WebDriverManager/BrowserManagers/EdgeDriverManager.cs

Lines changed: 27 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -9,30 +9,28 @@
99

1010
public class EdgeDriverManager : Logging, IBaseBrowserManager
1111
{
12-
WebDriverManagerConfig config = new WebDriverManagerConfig
12+
private readonly WebDriverManagerConfig _config = new WebDriverManagerConfig
1313
{
14-
binary = "MicrosoftWebDriver.exe",
15-
url = string.Empty,
16-
pathVariable = "webdriver.edge.driver",
17-
architecture = Architecture.x32.ToString()
14+
Binary = "MicrosoftWebDriver.exe",
15+
Url = string.Empty,
16+
PathVariable = "webdriver.edge.driver",
17+
Architecture = Architecture.X32.ToString()
1818
};
1919

2020
public string GetLatestVersion()
2121
{
2222
try
2323
{
24-
using (WebClient client = new WebClient())
24+
using (var client = new WebClient())
2525
{
26-
string version = null;
2726
var doc = new HtmlDocument();
28-
var htmlCode = client.DownloadString("https://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver");
27+
var htmlCode =
28+
client.DownloadString("https://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver");
2929
doc.LoadHtml(htmlCode);
30-
var itemList = doc.DocumentNode.SelectNodes("//ul[contains(@class, 'subsection__body')]//p[2]").Select(p => p.InnerText).ToList();
31-
version = itemList.FirstOrDefault().Split(' ')[1].Split(' ')[0];
32-
if (version != null || version != string.Empty)
33-
Log?.Info($"Latest edge driver version is '{version}'");
34-
else
35-
Log?.Warn($"Problem with getting latest edge driver version. Parsed version is '{version}'");
30+
var itemList = doc.DocumentNode.SelectNodes("//ul[contains(@class, 'subsection__body')]//p[2]")
31+
.Select(p => p.InnerText).ToList();
32+
var version = itemList.FirstOrDefault()?.Split(' ')[1].Split(' ')[0];
33+
Log?.Info($"Latest edge driver version is '{version}'");
3634
return version;
3735
}
3836
}
@@ -47,18 +45,16 @@ public string GetDownloadUrl()
4745
{
4846
try
4947
{
50-
using (WebClient client = new WebClient())
48+
using (var client = new WebClient())
5149
{
52-
string url = null;
5350
var doc = new HtmlDocument();
54-
var htmlCode = client.DownloadString("https://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver");
51+
var htmlCode =
52+
client.DownloadString("https://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver");
5553
doc.LoadHtml(htmlCode);
56-
var itemList = doc.DocumentNode.SelectNodes("//ul[contains(@class, 'subsection__body')]//p[1]/a").Select(p => p.GetAttributeValue("href", null)).ToList();
57-
url = itemList.FirstOrDefault();
58-
if (url != null || url != string.Empty)
59-
Log?.Info($"Edge driver download url is '{url}'");
60-
else
61-
Log?.Warn($"Problem with getting edge driver download url. Parsed url is '{url}'");
54+
var itemList = doc.DocumentNode.SelectNodes("//ul[contains(@class, 'subsection__body')]//p[1]/a")
55+
.Select(p => p.GetAttributeValue("href", null)).ToList();
56+
var url = itemList.FirstOrDefault();
57+
Log?.Info($"Edge driver download url is '{url}'");
6258
return url;
6359
}
6460
}
@@ -70,30 +66,29 @@ public string GetDownloadUrl()
7066
}
7167

7268
public EdgeDriverManager()
73-
: base()
7469
{
75-
config.version = GetLatestVersion();
76-
config.url = GetDownloadUrl();
70+
_config.Version = GetLatestVersion();
71+
_config.Url = GetDownloadUrl();
7772
}
7873

7974
public void Init()
8075
{
81-
config.destication = Path.Combine(Directory.GetCurrentDirectory(), config.DefaultDestinationFolder);
76+
_config.Destication = Path.Combine(Directory.GetCurrentDirectory(), _config.DefaultDestinationFolder);
8277
Base();
8378
}
8479

8580
public void Init(string destination)
8681
{
87-
config.destication = destination;
82+
_config.Destication = destination;
8883
Log?.Debug($"Set custom edge driver destination path to: '{destination}'");
8984
Base();
9085
}
9186

9287
public void Base()
9388
{
94-
WebDriverManager.Download(config);
95-
WebDriverManager.AddEnvironmentVariable(config.pathVariable);
96-
WebDriverManager.UpdatePath(config.pathVariable);
89+
WebDriverManager.Download(_config);
90+
WebDriverManager.AddEnvironmentVariable(_config.PathVariable);
91+
WebDriverManager.UpdatePath(_config.PathVariable);
9792
}
9893
}
99-
}
94+
}

WebDriverManager/BrowserManagers/IBaseBrowserManager.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,4 @@ public interface IBaseBrowserManager
2424
/// </summary>
2525
void Base();
2626
}
27-
}
27+
}

0 commit comments

Comments
 (0)