@@ -60,7 +60,9 @@ public void ShouldThrowArgumentNullExceptionWhenKeysNull()
60
60
IAlert alert = WaitFor < IAlert > ( AlertToBePresent , "No alert found" ) ;
61
61
try
62
62
{
63
- Assert . That ( ( ) => alert . SendKeys ( null ) , Throws . ArgumentNullException ) ;
63
+ Assert . That (
64
+ ( ) => alert . SendKeys ( null ) ,
65
+ Throws . ArgumentNullException ) ;
64
66
}
65
67
finally
66
68
{
@@ -147,13 +149,12 @@ public void SettingTheValueOfAnAlertThrows()
147
149
driver . FindElement ( By . Id ( "alert" ) ) . Click ( ) ;
148
150
149
151
IAlert alert = WaitFor < IAlert > ( AlertToBePresent , "No alert found" ) ;
152
+
150
153
try
151
154
{
152
- alert . SendKeys ( "cheese" ) ;
153
- Assert . Fail ( "Expected exception" ) ;
154
- }
155
- catch ( ElementNotInteractableException )
156
- {
155
+ Assert . That (
156
+ ( ) => alert . SendKeys ( "cheese" ) ,
157
+ Throws . TypeOf < ElementNotInteractableException > ( ) ) ;
157
158
}
158
159
finally
159
160
{
@@ -198,8 +199,10 @@ public void AlertShouldNotAllowAdditionalCommandsIfDismissed()
198
199
199
200
IAlert alert = WaitFor < IAlert > ( AlertToBePresent , "No alert found" ) ;
200
201
alert . Dismiss ( ) ;
201
- string text ;
202
- Assert . That ( ( ) => text = alert . Text , Throws . InstanceOf < NoAlertPresentException > ( ) ) ;
202
+
203
+ Assert . That (
204
+ ( ) => alert . Text ,
205
+ Throws . TypeOf < NoAlertPresentException > ( ) ) ;
203
206
}
204
207
205
208
[ Test ]
@@ -249,7 +252,9 @@ public void SwitchingToMissingAlertThrows()
249
252
{
250
253
driver . Url = CreateAlertPage ( "cheese" ) ;
251
254
252
- Assert . That ( ( ) => AlertToBePresent ( ) , Throws . InstanceOf < NoAlertPresentException > ( ) ) ;
255
+ Assert . That (
256
+ ( ) => AlertToBePresent ( ) ,
257
+ Throws . TypeOf < NoAlertPresentException > ( ) ) ;
253
258
}
254
259
255
260
[ Test ]
@@ -270,15 +275,9 @@ public void SwitchingToMissingAlertInAClosedWindowThrows()
270
275
driver . Close ( ) ;
271
276
WaitFor ( WindowHandleCountToBe ( 1 ) , "Window count was not 1" ) ;
272
277
273
- try
274
- {
275
- AlertToBePresent ( ) . Accept ( ) ;
276
- Assert . Fail ( "Expected exception" ) ;
277
- }
278
- catch ( NoSuchWindowException )
279
- {
280
- // Expected
281
- }
278
+ Assert . That (
279
+ ( ) => AlertToBePresent ( ) . Accept ( ) ,
280
+ Throws . TypeOf < NoSuchWindowException > ( ) ) ;
282
281
283
282
}
284
283
finally
@@ -321,17 +320,22 @@ public void HandlesTwoAlertsFromOneInteraction()
321
320
{
322
321
driver . Url = EnvironmentManager . Instance . UrlBuilder . CreateInlinePage ( new InlinePage ( )
323
322
. WithScripts (
324
- "function setInnerText(id, value) {" ,
325
- " document.getElementById(id).innerHTML = '<p>' + value + '</p>';" ,
326
- "}" ,
327
- "function displayTwoPrompts() {" ,
328
- " setInnerText('text1', prompt('First'));" ,
329
- " setInnerText('text2', prompt('Second'));" ,
330
- "}" )
323
+ """
324
+ function setInnerText(id, value) {
325
+ document.getElementById(id).innerHTML = '<p>' + value + '</p>';
326
+ }
327
+
328
+ function displayTwoPrompts() {
329
+ setInnerText('text1', prompt('First'));
330
+ setInnerText('text2', prompt('Second'));
331
+ }
332
+ """ )
331
333
. WithBody (
332
- "<a href='#' id='double-prompt' onclick='displayTwoPrompts();'>click me</a>" ,
333
- "<div id='text1'></div>" ,
334
- "<div id='text2'></div>" ) ) ;
334
+ """
335
+ <a href='#' id='double-prompt' onclick='displayTwoPrompts();'>click me</a>
336
+ <div id='text1'></div>
337
+ <div id='text2'></div>
338
+ """ ) ) ;
335
339
336
340
driver . FindElement ( By . Id ( "double-prompt" ) ) . Click ( ) ;
337
341
@@ -355,7 +359,7 @@ public void HandlesTwoAlertsFromOneInteraction()
355
359
public void ShouldHandleAlertOnPageLoad ( )
356
360
{
357
361
string pageWithOnLoad = EnvironmentManager . Instance . UrlBuilder . CreateInlinePage ( new InlinePage ( )
358
- . WithOnLoad ( "javascript:alert(\ " onload\" ) " )
362
+ . WithOnLoad ( """ javascript:alert("onload")"" " )
359
363
. WithBody ( "<p>Page with onload event handler</p>" ) ) ;
360
364
driver . Url = EnvironmentManager . Instance . UrlBuilder . CreateInlinePage ( new InlinePage ( )
361
365
. WithBody ( string . Format ( "<a id='open-page-with-onload-alert' href='{0}'>open new page</a>" , pageWithOnLoad ) ) ) ;
@@ -411,17 +415,12 @@ public void ShouldNotHandleAlertInAnotherWindow()
411
415
Assert . AreEqual ( 1 , allWindows . Count ) ;
412
416
onloadWindow = allWindows [ 0 ] ;
413
417
414
- try
418
+ Assert . That ( ( ) =>
415
419
{
416
420
IWebElement el = driver . FindElement ( By . Id ( "open-new-window" ) ) ;
417
421
WaitFor < IAlert > ( AlertToBePresent , TimeSpan . FromSeconds ( 5 ) , "No alert found" ) ;
418
- Assert . Fail ( "Expected exception" ) ;
419
- }
420
- catch ( WebDriverException )
421
- {
422
- // An operation timed out exception is expected,
423
- // since we're using WaitFor<T>.
424
- }
422
+ } ,
423
+ Throws . TypeOf < WebDriverException > ( ) ) ;
425
424
426
425
}
427
426
finally
@@ -442,15 +441,10 @@ public void IncludesAlertTextInUnhandledAlertException()
442
441
443
442
driver . FindElement ( By . Id ( "alert" ) ) . Click ( ) ;
444
443
WaitFor < IAlert > ( AlertToBePresent , "No alert found" ) ;
445
- try
446
- {
447
- string title = driver . Title ;
448
- Assert . Fail ( "Expected UnhandledAlertException" ) ;
449
- }
450
- catch ( UnhandledAlertException e )
451
- {
452
- Assert . AreEqual ( "cheese" , e . AlertText ) ;
453
- }
444
+
445
+ Assert . That (
446
+ ( ) => driver . Title ,
447
+ Throws . TypeOf < UnhandledAlertException > ( ) . With . Property ( nameof ( UnhandledAlertException . AlertText ) ) . EqualTo ( "cheese" ) ) ;
454
448
}
455
449
456
450
[ Test ]
@@ -522,16 +516,14 @@ private Func<IWebElement> ElementToBePresent(By locator)
522
516
{
523
517
return ( ) =>
524
518
{
525
- IWebElement foundElement = null ;
526
519
try
527
520
{
528
- foundElement = driver . FindElement ( By . Id ( "open-page-with-onunload-alert" ) ) ;
521
+ return driver . FindElement ( By . Id ( "open-page-with-onunload-alert" ) ) ;
529
522
}
530
523
catch ( NoSuchElementException )
531
524
{
525
+ return null ;
532
526
}
533
-
534
- return foundElement ;
535
527
} ;
536
528
}
537
529
@@ -554,9 +546,8 @@ private Func<bool> WindowWithName(string name)
554
546
}
555
547
catch ( NoSuchWindowException )
556
548
{
549
+ return false ;
557
550
}
558
-
559
- return false ;
560
551
} ;
561
552
}
562
553
0 commit comments