@@ -21,9 +21,19 @@ public ScreenshotController(ILogger<ScreenshotController> logger, ISessionReposi
2121 public async Task < ActionResult > TakeScreenshot ( [ FromRoute ] string sessionId )
2222 {
2323 var session = GetActiveSession ( sessionId ) ;
24- var currentWindow = session . CurrentWindow ;
25- _logger . LogInformation ( "Taking screenshot of window with title {WindowTitle} (session {SessionId})" , currentWindow . Title , session . SessionId ) ;
26- using var bitmap = currentWindow . Capture ( ) ;
24+ AutomationElement screenshotElement ;
25+ if ( session . App == null )
26+ {
27+ _logger . LogInformation ( "Taking screenshot of desktop (session {SessionId})" , session . SessionId ) ;
28+ screenshotElement = session . Automation . GetDesktop ( ) ;
29+ }
30+ else
31+ {
32+ var currentWindow = session . CurrentWindow ;
33+ _logger . LogInformation ( "Taking screenshot of window with title {WindowTitle} (session {SessionId})" , currentWindow . Title , session . SessionId ) ;
34+ screenshotElement = currentWindow ;
35+ }
36+ using var bitmap = screenshotElement . Capture ( ) ;
2737 return await Task . FromResult ( WebDriverResult . Success ( GetBase64Data ( bitmap ) ) ) ;
2838 }
2939
@@ -57,7 +67,7 @@ private AutomationElement GetElement(Session session, string elementId)
5767 private Session GetActiveSession ( string sessionId )
5868 {
5969 var session = GetSession ( sessionId ) ;
60- if ( session . App == null || session . App . HasExited )
70+ if ( session . App != null && session . App . HasExited )
6171 {
6272 throw WebDriverResponseException . NoWindowsOpenForSession ( ) ;
6373 }
0 commit comments