Skip to content

Commit 9dfac3e

Browse files
committed
Add support for appium driver
1 parent 032c78c commit 9dfac3e

File tree

3 files changed

+128
-3
lines changed

3 files changed

+128
-3
lines changed
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
namespace WebDriverManager.BrowserManagers
2+
{
3+
using HtmlAgilityPack;
4+
using System;
5+
using System.IO;
6+
using System.Linq;
7+
using System.Net;
8+
using Helpers;
9+
10+
public class AppiumDriverManager : Logging, IBaseBrowserManager
11+
{
12+
private readonly string installationCommand = "/SP- /silent /noicons /closeapplications /dir=expand:%1";
13+
14+
/// <summary>
15+
/// Set target appium driver architecture to x32 by default because of only 32 architecture presented
16+
/// </summary>
17+
WebDriverManagerConfig config = new WebDriverManagerConfig
18+
{
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()
23+
};
24+
25+
public string GetLatestVersion()
26+
{
27+
try
28+
{
29+
using (WebClient client = new WebClient())
30+
{
31+
string version = null;
32+
var doc = new HtmlDocument();
33+
var htmlCode = client.DownloadString("https://bitbucket.org/appium/appium.app/downloads");
34+
doc.LoadHtml(htmlCode);
35+
var itemList = doc.DocumentNode.SelectNodes("//tr[@class='iterable-item']/td[@class='name']/a[contains(.,'AppiumForWindows_')]").Select(p => p.InnerText).ToList();
36+
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}'");
42+
return version;
43+
}
44+
}
45+
catch (Exception ex)
46+
{
47+
Log?.Error(ex, "Error occurred during getting last appium driver version");
48+
throw new WebDriverManagerException("Error occurred during getting last appium driver version", ex);
49+
}
50+
}
51+
52+
public AppiumDriverManager()
53+
: base()
54+
{
55+
config.version = GetLatestVersion();
56+
}
57+
58+
public AppiumDriverManager(string version)
59+
: base()
60+
{
61+
config.version = version;
62+
Log?.Info($"Set appium driver version to: '{version}'");
63+
}
64+
65+
public void Init()
66+
{
67+
config.destication = Path.Combine(Directory.GetCurrentDirectory(), config.DefaultDestinationFolder);
68+
Base();
69+
}
70+
71+
public void Init(string destination)
72+
{
73+
config.destication = destination;
74+
Log?.Info($"Set custom appium driver destination path to: '{destination}'");
75+
Base();
76+
}
77+
78+
public void Base()
79+
{
80+
WebDriverManager.Download(config);
81+
WebDriverManager.Unzip(config);
82+
WebDriverManager.Clean();
83+
WebDriverManager.Install(installationCommand);
84+
}
85+
}
86+
}

WebDriverManager/Helpers/WebDriverManager.cs

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
{
33
using NLog;
44
using System;
5+
using System.Diagnostics;
56
using System.IO;
67
using System.IO.Compression;
78
using System.Net;
@@ -18,6 +19,8 @@ public static class WebDriverManager
1819

1920
private static string zip { get; set; } = null;
2021

22+
private static bool isNew { get; set; } = false;
23+
2124
/// <summary>
2225
/// Build browser driver download URL from mock using config parameters
2326
/// </summary>
@@ -35,7 +38,7 @@ private static string BuildUrl(string baseUrl, string release, string version, s
3538
.Replace("<version>", version)
3639
.Replace("<architecture>", architecture);
3740
}
38-
catch(Exception ex)
41+
catch (Exception ex)
3942
{
4043
hLog.Error(ex, "Error occurred during building browser driver archive download URL");
4144
throw new WebDriverManagerException("Error occurred during building browser driver archive download URL", ex);
@@ -53,7 +56,7 @@ private static void PrepareCatalogs(string path)
5356
if (!Directory.Exists(path))
5457
Directory.CreateDirectory(path);
5558
}
56-
catch(Exception ex)
59+
catch (Exception ex)
5760
{
5861
hLog.Error(ex, "Error occurred during browser driver catalog preparation");
5962
throw new WebDriverManagerException("Error occurred during browser driver catalog preparation", ex);
@@ -71,7 +74,7 @@ private static string ZipFileName(string hreflink)
7174
{
7275
return Path.GetFileName(hreflink);
7376
}
74-
catch(Exception ex)
77+
catch (Exception ex)
7578
{
7679
hLog.Error(ex, "Error occurred during getting browser driver archive name");
7780
throw new WebDriverManagerException("Error occurred during getting browser driver archive name", ex);
@@ -129,7 +132,10 @@ public static void Unzip(WebDriverManagerConfig config)
129132
}
130133
}
131134
}
135+
isNew = true;
132136
}
137+
else
138+
isNew = false;
133139
}
134140
catch (Exception ex)
135141
{
@@ -193,5 +199,37 @@ public static void UpdatePath(string variable)
193199
throw new WebDriverManagerException("Error occurred during updating PATH environment variable", ex);
194200
}
195201
}
202+
203+
/// <summary>
204+
/// Install application from file
205+
/// </summary>
206+
/// <param name="command">Installation command</param>
207+
public static void Install(string command)
208+
{
209+
try
210+
{
211+
if (File.Exists(desticationFile) && isNew)
212+
{
213+
ProcessStartInfo startInfo = new ProcessStartInfo
214+
{
215+
UseShellExecute = false,
216+
WindowStyle = ProcessWindowStyle.Hidden,
217+
FileName = desticationFile,
218+
Arguments = command
219+
};
220+
Process process = new Process
221+
{
222+
StartInfo = startInfo
223+
};
224+
process.Start();
225+
process.WaitForExit();
226+
}
227+
}
228+
catch (Exception ex)
229+
{
230+
hLog.Error(ex, $"Error occurred during application installation from file '{desticationFile}' using command '{command}'");
231+
throw new WebDriverManagerException($"Error occurred during application installation from file '{desticationFile}' using command '{command}'", ex);
232+
}
233+
}
196234
}
197235
}

WebDriverManager/WebDriverManager.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
<Compile Include="BrowserManagers\InternetExplorerDriverManager.cs" />
5454
<Compile Include="BrowserManagers\MarionetteDriverManager.cs" />
5555
<Compile Include="BrowserManagers\OperaDriverManager.cs" />
56+
<Compile Include="BrowserManagers\AppiumDriverManager.cs" />
5657
<Compile Include="BrowserManagers\PhantomJsDriverManager.cs" />
5758
<Compile Include="Helpers\Architecture.cs" />
5859
<Compile Include="BrowserManagers\ChromeDriverManager.cs" />

0 commit comments

Comments
 (0)