1+ using System ;
12using System . Collections . Generic ;
3+ using System . IO ;
24using System . Threading . Tasks ;
35using Microsoft . VisualStudio . TestTools . UnitTesting ;
46using OpenQA . Selenium ;
57using OpenQA . Selenium . DevTools ;
68using System . Linq ;
9+ using System . Threading ;
10+ using OpenQA . Selenium . DevTools . V127 . Browser ;
711using OpenQA . Selenium . DevTools . V127 . Network ;
812using OpenQA . Selenium . DevTools . V127 . Performance ;
913
@@ -37,14 +41,14 @@ public async Task BasicAuthentication()
3741 Assert . AreEqual ( "Congratulations! You must have the proper credentials." ,
3842 driver . FindElement ( By . TagName ( "p" ) ) . Text ) ;
3943 }
40-
44+
4145 [ TestMethod ]
4246 public async Task RecordNetworkResponse ( )
4347 {
4448 var contentType = new List < string > ( ) ;
4549
4650 INetwork networkInterceptor = driver . Manage ( ) . Network ;
47- networkInterceptor . NetworkResponseReceived += ( _ , e ) =>
51+ networkInterceptor . NetworkResponseReceived += ( _ , e ) =>
4852 {
4953 contentType . Add ( e . ResponseHeaders [ "content-type" ] ) ;
5054 } ;
@@ -102,7 +106,7 @@ public async Task TransformNetworkRequest()
102106
103107 Assert . AreEqual ( "two" , driver . FindElement ( By . Id ( "result" ) ) . Text ) ;
104108 }
105-
109+
106110 [ TestMethod ]
107111 public async Task PerformanceMetrics ( )
108112 {
@@ -147,5 +151,45 @@ public async Task SetCookie()
147151 Assert . AreEqual ( "gouda" , cheese . Value ) ;
148152 }
149153
154+ [ TestMethod ]
155+ public async Task WaitForDownload ( )
156+ {
157+ driver . Url = "https://www.selenium.dev/selenium/web/downloads/download.html" ;
158+ var session = ( ( IDevTools ) driver ) . GetDevToolsSession ( ) ;
159+
160+ var downloadPath = Path . GetTempPath ( ) ;
161+ var downloadBehaviorCommandSettings = new SetDownloadBehaviorCommandSettings
162+ {
163+ Behavior = "allowAndName" ,
164+ BrowserContextId = null ,
165+ DownloadPath = downloadPath ,
166+ EventsEnabled = true
167+ } ;
168+ await session . SendCommand ( downloadBehaviorCommandSettings ) ;
169+
170+ var downloadCompleted = new ManualResetEvent ( false ) ;
171+ string ? downloadId = null ;
172+ bool downloaded = false ;
173+ session . DevToolsEventReceived += ( sender , args ) =>
174+ {
175+ var downloadState = args . EventData [ "state" ] ? . ToString ( ) ;
176+ if ( args . EventName == "downloadProgress" &&
177+ ( string . Equals ( downloadState , "completed" ) ||
178+ string . Equals ( downloadState , "canceled" ) ) )
179+ {
180+ downloadId = args . EventData [ "guid" ] . ToString ( ) ;
181+ downloaded = downloadState . Equals ( "completed" ) ;
182+ downloadCompleted . Set ( ) ;
183+ }
184+ } ;
185+
186+ driver . FindElement ( By . Id ( "file-1" ) ) . Click ( ) ;
187+
188+ Assert . IsTrue ( downloadCompleted . WaitOne ( TimeSpan . FromSeconds ( 10 ) ) ) ;
189+ Assert . IsTrue ( downloaded ) ;
190+ var downloadedFilePath = Path . Combine ( downloadPath , downloadId ) ;
191+ Assert . IsTrue ( File . Exists ( downloadedFilePath ) ) ;
192+ File . Delete ( downloadedFilePath ) ;
193+ }
150194 }
151195}
0 commit comments