24
24
using System . Collections ;
25
25
using System . Collections . Generic ;
26
26
using System . Collections . ObjectModel ;
27
+ using System . Diagnostics . CodeAnalysis ;
27
28
using System . Globalization ;
28
29
using System . Threading . Tasks ;
29
30
@@ -44,7 +45,7 @@ public class WebDriver : IWebDriver, ISearchContext, IJavaScriptExecutor, IFinds
44
45
private IFileDetector fileDetector = new DefaultFileDetector ( ) ;
45
46
private NetworkManager network ;
46
47
private WebElementFactory elementFactory ;
47
- private SessionId sessionId ;
48
+
48
49
private List < string > registeredCommands = new List < string > ( ) ;
49
50
50
51
/// <summary>
@@ -191,10 +192,7 @@ public bool IsActionExecutor
191
192
/// <summary>
192
193
/// Gets the <see cref="SessionId"/> for the current session of this driver.
193
194
/// </summary>
194
- public SessionId SessionId
195
- {
196
- get { return this . sessionId ; }
197
- }
195
+ public SessionId SessionId { get ; private set ; }
198
196
199
197
/// <summary>
200
198
/// Gets or sets the <see cref="IFileDetector"/> responsible for detecting
@@ -612,7 +610,7 @@ protected virtual Response Execute(string driverCommandToExecute,
612
610
/// <returns>A <see cref="Response"/> containing information about the success or failure of the command and any data returned by the command.</returns>
613
611
protected virtual async Task < Response > ExecuteAsync ( string driverCommandToExecute , Dictionary < string , object > parameters )
614
612
{
615
- Command commandToExecute = new Command ( this . sessionId , driverCommandToExecute , parameters ) ;
613
+ Command commandToExecute = new Command ( SessionId , driverCommandToExecute , parameters ) ;
616
614
617
615
Response commandResponse ;
618
616
@@ -641,6 +639,7 @@ protected virtual async Task<Response> ExecuteAsync(string driverCommandToExecut
641
639
/// Starts a session with the driver
642
640
/// </summary>
643
641
/// <param name="capabilities">Capabilities of the browser</param>
642
+ [ MemberNotNull ( nameof ( SessionId ) ) ]
644
643
protected void StartSession ( ICapabilities capabilities )
645
644
{
646
645
Dictionary < string , object > parameters = new Dictionary < string , object > ( ) ;
@@ -679,7 +678,9 @@ protected void StartSession(ICapabilities capabilities)
679
678
680
679
ReturnedCapabilities returnedCapabilities = new ReturnedCapabilities ( rawCapabilities ) ;
681
680
this . capabilities = returnedCapabilities ;
682
- this . sessionId = new SessionId ( response . SessionId ) ;
681
+
682
+ string sessionId = response . SessionId ?? throw new WebDriverException ( $ "The remote end did not respond with ID of a session when it was required. { response . Value } ") ;
683
+ this . SessionId = new SessionId ( sessionId ) ;
683
684
}
684
685
685
686
/// <summary>
@@ -723,7 +724,7 @@ protected virtual void Dispose(bool disposing)
723
724
{
724
725
try
725
726
{
726
- if ( this . sessionId is not null )
727
+ if ( this . SessionId is not null )
727
728
{
728
729
this . Execute ( DriverCommand . Quit , null ) ;
729
730
}
@@ -739,7 +740,7 @@ protected virtual void Dispose(bool disposing)
739
740
}
740
741
finally
741
742
{
742
- this . sessionId = null ;
743
+ this . SessionId = null ;
743
744
}
744
745
this . executor . Dispose ( ) ;
745
746
}
0 commit comments