Skip to content

Commit ef6e1ab

Browse files
committed
Update tests style per PR feedback part 1
1 parent 775dd72 commit ef6e1ab

File tree

5 files changed

+85
-79
lines changed

5 files changed

+85
-79
lines changed

dotnet/src/webdriver/WebDriver.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -935,7 +935,7 @@ private static object ConvertObjectToJavaScriptObject(object arg)
935935
}
936936
else
937937
{
938-
throw new ArgumentException("Argument is of an illegal type" + arg.ToString(), nameof(arg));
938+
throw new ArgumentException("Argument is of an illegal type: " + arg.ToString(), nameof(arg));
939939
}
940940

941941
return converted;

dotnet/test/common/AlertsTest.cs

Lines changed: 36 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,9 @@ public void ShouldThrowArgumentNullExceptionWhenKeysNull()
6060
IAlert alert = WaitFor<IAlert>(AlertToBePresent, "No alert found");
6161
try
6262
{
63-
Assert.That(() => alert.SendKeys(null), Throws.ArgumentNullException);
63+
Assert.That(
64+
() => alert.SendKeys(null),
65+
Throws.ArgumentNullException);
6466
}
6567
finally
6668
{
@@ -147,13 +149,12 @@ public void SettingTheValueOfAnAlertThrows()
147149
driver.FindElement(By.Id("alert")).Click();
148150

149151
IAlert alert = WaitFor<IAlert>(AlertToBePresent, "No alert found");
152+
150153
try
151154
{
152-
alert.SendKeys("cheese");
153-
Assert.Fail("Expected exception");
154-
}
155-
catch (ElementNotInteractableException)
156-
{
155+
Assert.That(
156+
() => alert.SendKeys("cheese"),
157+
Throws.InstanceOf<ElementNotInteractableException>());
157158
}
158159
finally
159160
{
@@ -198,8 +199,10 @@ public void AlertShouldNotAllowAdditionalCommandsIfDismissed()
198199

199200
IAlert alert = WaitFor<IAlert>(AlertToBePresent, "No alert found");
200201
alert.Dismiss();
201-
string text;
202-
Assert.That(() => text = alert.Text, Throws.InstanceOf<NoAlertPresentException>());
202+
203+
Assert.That(
204+
() => alert.Text,
205+
Throws.InstanceOf<NoAlertPresentException>());
203206
}
204207

205208
[Test]
@@ -249,7 +252,9 @@ public void SwitchingToMissingAlertThrows()
249252
{
250253
driver.Url = CreateAlertPage("cheese");
251254

252-
Assert.That(() => AlertToBePresent(), Throws.InstanceOf<NoAlertPresentException>());
255+
Assert.That(
256+
AlertToBePresent,
257+
Throws.InstanceOf<NoAlertPresentException>());
253258
}
254259

255260
[Test]
@@ -270,15 +275,9 @@ public void SwitchingToMissingAlertInAClosedWindowThrows()
270275
driver.Close();
271276
WaitFor(WindowHandleCountToBe(1), "Window count was not 1");
272277

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.InstanceOf<NoSuchWindowException>());
282281

283282
}
284283
finally
@@ -321,17 +320,22 @@ public void HandlesTwoAlertsFromOneInteraction()
321320
{
322321
driver.Url = EnvironmentManager.Instance.UrlBuilder.CreateInlinePage(new InlinePage()
323322
.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+
""")
331333
.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+
"""));
335339

336340
driver.FindElement(By.Id("double-prompt")).Click();
337341

@@ -355,7 +359,7 @@ public void HandlesTwoAlertsFromOneInteraction()
355359
public void ShouldHandleAlertOnPageLoad()
356360
{
357361
string pageWithOnLoad = EnvironmentManager.Instance.UrlBuilder.CreateInlinePage(new InlinePage()
358-
.WithOnLoad("javascript:alert(\"onload\")")
362+
.WithOnLoad("""javascript:alert("onload")""")
359363
.WithBody("<p>Page with onload event handler</p>"));
360364
driver.Url = EnvironmentManager.Instance.UrlBuilder.CreateInlinePage(new InlinePage()
361365
.WithBody(string.Format("<a id='open-page-with-onload-alert' href='{0}'>open new page</a>", pageWithOnLoad)));
@@ -411,17 +415,12 @@ public void ShouldNotHandleAlertInAnotherWindow()
411415
Assert.AreEqual(1, allWindows.Count);
412416
onloadWindow = allWindows[0];
413417

414-
try
418+
Assert.That(() =>
415419
{
416420
IWebElement el = driver.FindElement(By.Id("open-new-window"));
417421
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.InstanceOf<WebDriverException>());
425424

426425
}
427426
finally

dotnet/test/common/ElementAttributeTest.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -131,16 +131,15 @@ public void ShouldThrowExceptionIfSendingKeysToElementDisabledUsingRandomDisable
131131
Assert.That(() =>
132132
{
133133
disabledTextElement1.SendKeys("foo");
134-
}, Throws.InstanceOf<InvalidElementStateException>());
134+
}, Throws.TypeOf<ElementNotInteractableException>());
135135

136136
Assert.AreEqual(string.Empty, disabledTextElement1.Text);
137137

138138
IWebElement disabledTextElement2 = driver.FindElement(By.Id("disabledTextElement2"));
139139

140-
Assert.That(() =>
141-
{
142-
disabledTextElement2.SendKeys("bar");
143-
}, Throws.InstanceOf<InvalidElementStateException>());
140+
Assert.That(
141+
() => disabledTextElement2.SendKeys("bar"),
142+
Throws.TypeOf<ElementNotInteractableException>());
144143

145144
Assert.AreEqual(string.Empty, disabledTextElement2.Text);
146145
}

dotnet/test/common/ExecutingJavascriptTest.cs

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -391,14 +391,17 @@ public void ShouldBeAbleToPassACollectionAsArgument()
391391
Assert.AreEqual(collection.Count, length);
392392
}
393393

394-
394+
[Test]
395395
public void ShouldThrowAnExceptionIfAnArgumentIsNotValid()
396396
{
397397
if (!(driver is IJavaScriptExecutor))
398398
return;
399399

400400
driver.Url = javascriptPage;
401-
Assert.That(() => ExecuteScript("return arguments[0];", driver), Throws.InstanceOf<ArgumentException>());
401+
402+
Assert.That(
403+
() => ExecuteScript("return arguments[0];", driver),
404+
Throws.TypeOf<ArgumentException>().With.Message.StartsWith("Argument is of an illegal type: "));
402405
}
403406

404407
[Test]
@@ -490,10 +493,9 @@ public async Task ShouldBeAbleToPinJavascriptCodeAndExecuteRepeatedly()
490493

491494
await jsEngine.UnpinScript(script);
492495

493-
Assert.That(() =>
494-
{
495-
_ = ((IJavaScriptExecutor)driver).ExecuteScript(script);
496-
}, Throws.TypeOf<JavaScriptException>());
496+
Assert.That(
497+
() => ((IJavaScriptExecutor)driver).ExecuteScript(script),
498+
Throws.TypeOf<JavaScriptException>());
497499
}
498500

499501
[Test]
@@ -566,7 +568,9 @@ public void ShouldThrowAnExceptionWhenArgumentsWithStaleElementPassed()
566568

567569
Dictionary<string, object> args = new Dictionary<string, object>();
568570
args["key"] = new object[] { "a", new object[] { "zero", 1, true, 3.14159, false, el }, "c" };
569-
Assert.That(() => executor.ExecuteScript("return undefined;", args), Throws.InstanceOf<StaleElementReferenceException>());
571+
Assert.That(
572+
() => executor.ExecuteScript("return undefined;", args),
573+
Throws.TypeOf<StaleElementReferenceException>());
570574
}
571575

572576
[Test]
@@ -611,7 +615,17 @@ public void ShouldHandleRecursiveStructures()
611615
{
612616
driver.Url = simpleTestPage;
613617

614-
Assert.That(() => ExecuteScript("var obj1 = {}; var obj2 = {}; obj1['obj2'] = obj2; obj2['obj1'] = obj1; return obj1"), Throws.InstanceOf<WebDriverException>());
618+
Assert.That(
619+
() => ExecuteScript(
620+
"""
621+
var obj1 = {};
622+
var obj2 = {};
623+
obj1['obj2'] = obj2;
624+
obj2['obj1'] = obj1;
625+
return obj1
626+
"""
627+
),
628+
Throws.TypeOf<JavaScriptException>());
615629
}
616630

617631
//------------------------------------------------------------------
@@ -622,10 +636,9 @@ public void ShouldHandleRecursiveStructures()
622636
[Ignore("Reason for ignore: Failure indicates hang condition, which would break the test suite. Really needs a timeout set.")]
623637
public void ShouldThrowExceptionIfExecutingOnNoPage()
624638
{
625-
Assert.That(() =>
626-
{
627-
((IJavaScriptExecutor)driver).ExecuteScript("return 1;");
628-
}, Throws.InstanceOf<WebDriverException>());
639+
Assert.That(
640+
() => ((IJavaScriptExecutor)driver).ExecuteScript("return 1;"),
641+
Throws.InstanceOf<WebDriverException>());
629642
}
630643

631644
[Test]

dotnet/test/common/WindowSwitchingTest.cs

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,9 @@ public void ShouldThrowNoSuchWindowException()
6161
driver.Url = xhtmlTestPage;
6262
String current = driver.CurrentWindowHandle;
6363

64-
Assert.That(() =>
65-
{
66-
driver.SwitchTo().Window("invalid name");
67-
}, Throws.InstanceOf<NoSuchWindowException>());
64+
Assert.That(
65+
() => driver.SwitchTo().Window("invalid name"),
66+
Throws.InstanceOf<NoSuchWindowException>());
6867

6968
driver.SwitchTo().Window(current);
7069
}
@@ -88,10 +87,9 @@ public void ShouldThrowNoSuchWindowExceptionOnAnAttemptToGetItsHandle()
8887

8988
try
9089
{
91-
Assert.That(() =>
92-
{
93-
_ = driver.CurrentWindowHandle;
94-
}, Throws.InstanceOf<NoSuchWindowException>());
90+
Assert.That(
91+
() => driver.CurrentWindowHandle,
92+
Throws.InstanceOf<NoSuchWindowException>());
9593
}
9694
finally
9795
{
@@ -118,15 +116,13 @@ public void ShouldThrowNoSuchWindowExceptionOnAnyOperationIfAWindowIsClosed()
118116

119117
try
120118
{
121-
Assert.That(() =>
122-
{
123-
_ = driver.Title;
124-
}, Throws.InstanceOf<NoSuchWindowException>());
119+
Assert.That(
120+
() => driver.Title,
121+
Throws.InstanceOf<NoSuchWindowException>());
125122

126-
Assert.That(() =>
127-
{
128-
driver.FindElement(By.TagName("body"));
129-
}, Throws.InstanceOf<NoSuchWindowException>());
123+
Assert.That(
124+
() => driver.FindElement(By.TagName("body")),
125+
Throws.InstanceOf<NoSuchWindowException>());
130126
}
131127
finally
132128
{
@@ -154,10 +150,9 @@ public void ShouldThrowNoSuchWindowExceptionOnAnyElementOperationIfAWindowIsClos
154150

155151
try
156152
{
157-
Assert.That(() =>
158-
{
159-
_ = body.Text;
160-
}, Throws.InstanceOf<NoSuchWindowException>());
153+
Assert.That(
154+
() => body.Text,
155+
Throws.InstanceOf<NoSuchWindowException>());
161156
}
162157
finally
163158
{
@@ -263,10 +258,10 @@ public void FailingToSwitchToAWindowLeavesTheCurrentWindowAsIs()
263258
driver.Url = xhtmlTestPage;
264259
String current = driver.CurrentWindowHandle;
265260

266-
Assert.That(() =>
267-
{
268-
driver.SwitchTo().Window("i will never exist");
269-
}, Throws.InstanceOf<NoSuchWindowException>());
261+
Assert.That(
262+
() => driver.SwitchTo().Window("i will never exist"),
263+
Throws.InstanceOf<NoSuchWindowException>(),
264+
"Should not be able to change to a non-existant window");
270265

271266
String newHandle = driver.CurrentWindowHandle;
272267

0 commit comments

Comments
 (0)