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