-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathBaseTest.cs
114 lines (104 loc) · 3.74 KB
/
BaseTest.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
using System;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Net.Sockets;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
namespace SeleniumDocs
{
public class BaseTest
{
protected IWebDriver driver;
protected Uri GridUrl;
private Process _webserverProcess;
private const string ServerJarName = "selenium-server-4.31.0.jar";
private static readonly string BaseDirectory = AppContext.BaseDirectory;
private const string RelativePathToGrid = "../../../../../";
private readonly string _examplesDirectory = Path.GetFullPath(Path.Combine(BaseDirectory, RelativePathToGrid));
[TestCleanup]
public void Cleanup()
{
driver?.Quit();
if (_webserverProcess != null)
{
StopServer();
}
}
protected void StartDriver(string browserVersion = "stable")
{
ChromeOptions options = new ChromeOptions
{
BrowserVersion = browserVersion
};
driver = new ChromeDriver(options);
}
protected async Task StartServer()
{
if (_webserverProcess == null || _webserverProcess.HasExited)
{
_webserverProcess = new Process();
_webserverProcess.StartInfo.FileName =
RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "java.exe" : "java";
string port = GetFreeTcpPort().ToString();
GridUrl = new Uri("http://localhost:" + port + "/wd/hub");
_webserverProcess.StartInfo.Arguments = " -jar " + ServerJarName +
" standalone --port " + port +
" --selenium-manager true --enable-managed-downloads true";
_webserverProcess.StartInfo.WorkingDirectory = _examplesDirectory;
_webserverProcess.Start();
await EnsureGridIsRunningAsync();
}
}
private void StopServer()
{
if (_webserverProcess != null && !_webserverProcess.HasExited)
{
_webserverProcess.Kill();
_webserverProcess.Dispose();
_webserverProcess = null;
}
}
private static int GetFreeTcpPort()
{
TcpListener l = new TcpListener(IPAddress.Loopback, 0);
l.Start();
int port = ((IPEndPoint)l.LocalEndpoint).Port;
l.Stop();
return port;
}
private async Task EnsureGridIsRunningAsync()
{
DateTime timeout = DateTime.Now.Add(TimeSpan.FromSeconds(30));
bool isRunning = false;
HttpClient client = new HttpClient();
while (!isRunning && DateTime.Now < timeout)
{
try
{
HttpResponseMessage response = await client.GetAsync(GridUrl + "/status");
if (response.IsSuccessStatusCode)
{
isRunning = true;
}
else
{
await Task.Delay(500);
}
}
catch (HttpRequestException)
{
await Task.Delay(500);
}
}
if (!isRunning)
{
throw new TimeoutException("Could not confirm the remote selenium server is running within 30 seconds");
}
}
}
}