Skip to content

Commit 40ea8a4

Browse files
authoredDec 6, 2024··
[dotnet] Migrate remaining NUnit assertions to Assert.That and Has.Count (#14870)
1 parent 22a6f0b commit 40ea8a4

19 files changed

+75
-111
lines changed
 

‎dotnet/test/common/BiDi/Browser/BrowserTest.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public async Task CanGetUserContexts()
4141
var userContexts = await bidi.Browser.GetUserContextsAsync();
4242

4343
Assert.That(userContexts, Is.Not.Null);
44-
Assert.That(userContexts.Count, Is.GreaterThanOrEqualTo(2));
44+
Assert.That(userContexts, Has.Count.GreaterThanOrEqualTo(2));
4545
Assert.That(userContexts, Does.Contain(userContext1));
4646
Assert.That(userContexts, Does.Contain(userContext2));
4747
}

‎dotnet/test/common/BiDi/Network/NetworkEventsTest.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ public async Task CanListenToBeforeRequestSentEventWithCookie()
9999

100100
var req = await tcs.Task.WaitAsync(TimeSpan.FromSeconds(5));
101101

102-
Assert.That(req.Request.Cookies.Count, Is.EqualTo(1));
102+
Assert.That(req.Request.Cookies, Has.Count.EqualTo(1));
103103
Assert.That(req.Request.Cookies[0].Name, Is.EqualTo("foo"));
104104
Assert.That((req.Request.Cookies[0].Value as BytesValue.String).Value, Is.EqualTo("bar"));
105105
}

‎dotnet/test/common/BiDi/Script/ScriptCommandsTest.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public async Task CanGetAllRealms()
3434
var realms = await bidi.Script.GetRealmsAsync();
3535

3636
Assert.That(realms, Is.Not.Null);
37-
Assert.That(realms.Count, Is.EqualTo(2));
37+
Assert.That(realms, Has.Count.EqualTo(2));
3838

3939
Assert.That(realms[0], Is.AssignableFrom<RealmInfo.Window>());
4040
Assert.That(realms[0].Realm, Is.Not.Null);
@@ -51,7 +51,7 @@ public async Task CanGetAllRealmsByType()
5151
var realms = await bidi.Script.GetRealmsAsync(new() { Type = RealmType.Window });
5252

5353
Assert.That(realms, Is.Not.Null);
54-
Assert.That(realms.Count, Is.EqualTo(2));
54+
Assert.That(realms, Has.Count.EqualTo(2));
5555

5656
Assert.That(realms[0], Is.AssignableFrom<RealmInfo.Window>());
5757
Assert.That(realms[0].Realm, Is.Not.Null);

‎dotnet/test/common/BiDi/Storage/StorageTest.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ await context.Storage.SetCookieAsync(new("fish", "cod", UrlBuilder.HostName)
9494
var cookies = await context.Storage.GetCookiesAsync();
9595

9696
Assert.That(cookies, Is.Not.Null);
97-
Assert.That(cookies.Count, Is.EqualTo(1));
97+
Assert.That(cookies, Has.Count.EqualTo(1));
9898

9999
var cookie = cookies[0];
100100

@@ -119,7 +119,7 @@ public async Task CanGetAllCookies()
119119
var cookies = await bidi.Storage.GetCookiesAsync();
120120

121121
Assert.That(cookies, Is.Not.Null);
122-
Assert.That(cookies.Count, Is.EqualTo(2));
122+
Assert.That(cookies, Has.Count.EqualTo(2));
123123
Assert.That(cookies[0].Name, Is.EqualTo("key1"));
124124
Assert.That(cookies[1].Name, Is.EqualTo("key2"));
125125
}
@@ -157,7 +157,7 @@ public async Task CanDeleteCookieWithName()
157157
var cookies = await bidi.Storage.GetCookiesAsync();
158158

159159
Assert.That(cookies, Is.Not.Null);
160-
Assert.That(cookies.Count, Is.EqualTo(1));
160+
Assert.That(cookies, Has.Count.EqualTo(1));
161161
Assert.That(cookies[0].Name, Is.EqualTo("key2"));
162162
}
163163

‎dotnet/test/common/ClickScrollingTest.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public void ClickingOnAnchorScrollsPage()
4444

4545
// Sometimes JS is returning a double
4646
object result = ((IJavaScriptExecutor)driver).ExecuteScript(scrollScript);
47-
var yOffset = Convert.ChangeType(result, typeof(long));
47+
var yOffset = Convert.ToInt64(result);
4848

4949
//Focusing on to click, but not actually following,
5050
//the link will scroll it in to view, which is a few pixels further than 0

‎dotnet/test/common/DriverElementFindingTest.cs

+7-7
Original file line numberDiff line numberDiff line change
@@ -93,55 +93,55 @@ public void ShouldFindElementsById()
9393
{
9494
driver.Url = nestedPage;
9595
ReadOnlyCollection<IWebElement> elements = driver.FindElements(By.Id("test_id"));
96-
Assert.That(elements.Count, Is.EqualTo(2));
96+
Assert.That(elements, Has.Count.EqualTo(2));
9797
}
9898

9999
[Test]
100100
public void ShouldFindElementsByLinkText()
101101
{
102102
driver.Url = nestedPage;
103103
ReadOnlyCollection<IWebElement> elements = driver.FindElements(By.LinkText("hello world"));
104-
Assert.That(elements.Count, Is.EqualTo(12));
104+
Assert.That(elements, Has.Count.EqualTo(12));
105105
}
106106

107107
[Test]
108108
public void ShouldFindElementsByName()
109109
{
110110
driver.Url = nestedPage;
111111
ReadOnlyCollection<IWebElement> elements = driver.FindElements(By.Name("form1"));
112-
Assert.That(elements.Count, Is.EqualTo(4));
112+
Assert.That(elements, Has.Count.EqualTo(4));
113113
}
114114

115115
[Test]
116116
public void ShouldFindElementsByXPath()
117117
{
118118
driver.Url = nestedPage;
119119
ReadOnlyCollection<IWebElement> elements = driver.FindElements(By.XPath("//a"));
120-
Assert.That(elements.Count, Is.EqualTo(12));
120+
Assert.That(elements, Has.Count.EqualTo(12));
121121
}
122122

123123
[Test]
124124
public void ShouldFindElementsByClassName()
125125
{
126126
driver.Url = nestedPage;
127127
ReadOnlyCollection<IWebElement> elements = driver.FindElements(By.ClassName("one"));
128-
Assert.That(elements.Count, Is.EqualTo(3));
128+
Assert.That(elements, Has.Count.EqualTo(3));
129129
}
130130

131131
[Test]
132132
public void ShouldFindElementsByPartialLinkText()
133133
{
134134
driver.Url = nestedPage;
135135
ReadOnlyCollection<IWebElement> elements = driver.FindElements(By.PartialLinkText("world"));
136-
Assert.That(elements.Count, Is.EqualTo(12));
136+
Assert.That(elements, Has.Count.EqualTo(12));
137137
}
138138

139139
[Test]
140140
public void ShouldFindElementsByTagName()
141141
{
142142
driver.Url = nestedPage;
143143
ReadOnlyCollection<IWebElement> elements = driver.FindElements(By.TagName("a"));
144-
Assert.That(elements.Count, Is.EqualTo(12));
144+
Assert.That(elements, Has.Count.EqualTo(12));
145145
}
146146
#endregion
147147
}

‎dotnet/test/common/ElementElementFindingTest.cs

+7-7
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ public void ShouldFindElementsById()
101101
driver.Url = nestedPage;
102102
IWebElement parent = driver.FindElement(By.Name("form2"));
103103
ReadOnlyCollection<IWebElement> children = parent.FindElements(By.Id("2"));
104-
Assert.That(children.Count, Is.EqualTo(2));
104+
Assert.That(children, Has.Count.EqualTo(2));
105105
}
106106

107107
[Test]
@@ -110,7 +110,7 @@ public void ShouldFindElementsByLinkText()
110110
driver.Url = nestedPage;
111111
IWebElement parent = driver.FindElement(By.Name("div1"));
112112
ReadOnlyCollection<IWebElement> children = parent.FindElements(By.PartialLinkText("hello world"));
113-
Assert.That(children.Count, Is.EqualTo(2));
113+
Assert.That(children, Has.Count.EqualTo(2));
114114
Assert.That(children[0].Text, Is.EqualTo("hello world"));
115115
Assert.That(children[1].Text, Is.EqualTo("hello world"));
116116
}
@@ -121,7 +121,7 @@ public void ShouldFindElementsByName()
121121
driver.Url = nestedPage;
122122
IWebElement parent = driver.FindElement(By.Name("form2"));
123123
ReadOnlyCollection<IWebElement> children = parent.FindElements(By.Name("selectomatic"));
124-
Assert.That(children.Count, Is.EqualTo(2));
124+
Assert.That(children, Has.Count.EqualTo(2));
125125
}
126126

127127
[Test]
@@ -130,7 +130,7 @@ public void ShouldFindElementsByXPath()
130130
driver.Url = nestedPage;
131131
IWebElement parent = driver.FindElement(By.Name("classes"));
132132
ReadOnlyCollection<IWebElement> children = parent.FindElements(By.XPath("span"));
133-
Assert.That(children.Count, Is.EqualTo(3));
133+
Assert.That(children, Has.Count.EqualTo(3));
134134
Assert.That(children[0].Text, Is.EqualTo("Find me"));
135135
Assert.That(children[1].Text, Is.EqualTo("Also me"));
136136
Assert.That(children[2].Text, Is.EqualTo("But not me"));
@@ -142,7 +142,7 @@ public void ShouldFindElementsByClassName()
142142
driver.Url = nestedPage;
143143
IWebElement parent = driver.FindElement(By.Name("classes"));
144144
ReadOnlyCollection<IWebElement> children = parent.FindElements(By.ClassName("one"));
145-
Assert.That(children.Count, Is.EqualTo(2));
145+
Assert.That(children, Has.Count.EqualTo(2));
146146
Assert.That(children[0].Text, Is.EqualTo("Find me"));
147147
Assert.That(children[1].Text, Is.EqualTo("Also me"));
148148
}
@@ -153,7 +153,7 @@ public void ShouldFindElementsByPartialLinkText()
153153
driver.Url = nestedPage;
154154
IWebElement parent = driver.FindElement(By.Name("div1"));
155155
ReadOnlyCollection<IWebElement> children = parent.FindElements(By.PartialLinkText("hello "));
156-
Assert.That(children.Count, Is.EqualTo(2));
156+
Assert.That(children, Has.Count.EqualTo(2));
157157
Assert.That(children[0].Text, Is.EqualTo("hello world"));
158158
Assert.That(children[1].Text, Is.EqualTo("hello world"));
159159
}
@@ -164,7 +164,7 @@ public void ShouldFindElementsByTagName()
164164
driver.Url = nestedPage;
165165
IWebElement parent = driver.FindElement(By.Name("classes"));
166166
ReadOnlyCollection<IWebElement> children = parent.FindElements(By.TagName("span"));
167-
Assert.That(children.Count, Is.EqualTo(3));
167+
Assert.That(children, Has.Count.EqualTo(3));
168168
Assert.That(children[0].Text, Is.EqualTo("Find me"));
169169
Assert.That(children[1].Text, Is.EqualTo("Also me"));
170170
Assert.That(children[2].Text, Is.EqualTo("But not me"));

‎dotnet/test/common/ExecutingAsyncJavascriptTest.cs

+7-5
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ public void ShouldBeAbleToReturnArraysOfPrimitivesFromAsyncScripts()
118118
Assert.That(result, Is.Not.Null);
119119
Assert.That(result, Is.InstanceOf<ReadOnlyCollection<object>>());
120120
ReadOnlyCollection<object> resultList = result as ReadOnlyCollection<object>;
121-
Assert.That(resultList.Count, Is.EqualTo(5));
121+
Assert.That(resultList, Has.Count.EqualTo(5));
122122
Assert.That(resultList[0], Is.Null);
123123
Assert.That((long)resultList[1], Is.EqualTo(123));
124124
Assert.That(resultList[2].ToString(), Is.EqualTo("abc"));
@@ -221,10 +221,12 @@ public void ShouldCatchErrorsWithMessageAndStacktraceWhenExecutingInitialScript(
221221
string js = "function functionB() { throw Error('errormessage'); };"
222222
+ "function functionA() { functionB(); };"
223223
+ "functionA();";
224-
Exception ex = Assert.Catch(() => executor.ExecuteAsyncScript(js));
225-
Assert.That(ex, Is.InstanceOf<WebDriverException>());
226-
Assert.That(ex.Message.Contains("errormessage"));
227-
Assert.That(ex.StackTrace.Contains("functionB"));
224+
225+
Assert.That(
226+
() => executor.ExecuteAsyncScript(js),
227+
Throws.InstanceOf<WebDriverException>()
228+
.With.Message.Contains("errormessage")
229+
.And.Property(nameof(WebDriverException.StackTrace)).Contains("functionB"));
228230
}
229231

230232
[Test]

‎dotnet/test/common/ExecutingJavascriptTest.cs

+7-5
Original file line numberDiff line numberDiff line change
@@ -256,10 +256,12 @@ public void ShouldThrowAnExceptionWithMessageAndStacktraceWhenTheJavascriptIsBad
256256
string js = "function functionB() { throw Error('errormessage'); };"
257257
+ "function functionA() { functionB(); };"
258258
+ "functionA();";
259-
Exception ex = Assert.Catch(() => ExecuteScript(js));
260-
Assert.That(ex, Is.InstanceOf<WebDriverException>());
261-
Assert.That(ex.Message, Does.Contain("errormessage"), "Exception message does not contain 'errormessage'");
262-
Assert.That(ex.StackTrace, Does.Contain("functionB"), "Exception message does not contain 'functionB'");
259+
260+
Assert.That(
261+
() => ExecuteScript(js),
262+
Throws.InstanceOf<WebDriverException>()
263+
.With.Message.Contains("errormessage")
264+
.And.Property(nameof(WebDriverException.StackTrace)).Contains("functionB"));
263265
}
264266

265267
[Test]
@@ -467,7 +469,7 @@ public void ShouldBeAbleToExecuteABigChunkOfJavascriptCode()
467469
if (fileList.Length > 0)
468470
{
469471
string jquery = System.IO.File.ReadAllText(fileList[0]);
470-
Assert.That(jquery.Length, Is.GreaterThan(50000));
472+
Assert.That(jquery, Has.Length.GreaterThan(50000));
471473
ExecuteScript(jquery, null);
472474
}
473475
}

‎dotnet/test/common/Interactions/ActionBuilderTest.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919

2020
using NUnit.Framework;
2121
using System;
22-
using System.Collections;
2322
using System.Collections.Generic;
2423

2524
namespace OpenQA.Selenium.Interactions
@@ -55,8 +54,9 @@ public void OutputsPointerEventsToDictionary()
5554
Assert.That(dictionary, Does.ContainKey("type").WithValue("pointer"));
5655
Assert.That(dictionary["id"], Is.Not.Null);
5756
Assert.That(dictionary["parameters"], Is.Not.Null);
57+
5858
var parameters = new Dictionary<string, object> { { "pointerType", "pen" } };
59-
CollectionAssert.AreEquivalent(parameters, (IEnumerable)dictionary["parameters"]);
59+
Assert.That(dictionary["parameters"], Is.EquivalentTo(parameters));
6060

6161
var events = new Dictionary<string, object>
6262
{
@@ -72,8 +72,8 @@ public void OutputsPointerEventsToDictionary()
7272
{"type", "pointerDown"},
7373
{"button", 0}
7474
};
75-
var actions = (IList<Object>)dictionary["actions"];
76-
CollectionAssert.AreEquivalent(events, (IEnumerable)actions[0]);
75+
var actions = (IList<object>)dictionary["actions"];
76+
Assert.That(actions[0], Is.EquivalentTo(events));
7777
}
7878
}
7979
}

‎dotnet/test/common/Internal/Logging/FileLogHandlerTest.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public void ShouldCreateFileIfDoesNotExist()
7272
fileLogHandler2.Handle(new LogEvent(typeof(FileLogHandlerTest), DateTimeOffset.Now, LogEventLevel.Info, "test message"));
7373
}
7474

75-
Assert.That(Regex.Matches(File.ReadAllText(tempFile), "test message").Count, Is.EqualTo(1));
75+
Assert.That(Regex.Matches(File.ReadAllText(tempFile), "test message"), Has.Count.EqualTo(1));
7676
}
7777
finally
7878
{
@@ -97,7 +97,7 @@ public void ShouldAppendFileIfExists()
9797
fileLogHandler2.Handle(new LogEvent(typeof(FileLogHandlerTest), DateTimeOffset.Now, LogEventLevel.Info, "test message"));
9898
}
9999

100-
Assert.That(Regex.Matches(File.ReadAllText(tempFilePath), "test message").Count, Is.EqualTo(2));
100+
Assert.That(Regex.Matches(File.ReadAllText(tempFilePath), "test message"), Has.Count.EqualTo(2));
101101
}
102102
finally
103103
{
@@ -117,7 +117,7 @@ public void ShouldOverwriteFileIfExists()
117117
fileLogHandler.Handle(new LogEvent(typeof(FileLogHandlerTest), DateTimeOffset.Now, LogEventLevel.Info, "test message"));
118118
}
119119

120-
Assert.That(Regex.Matches(File.ReadAllText(tempFile), "test message").Count, Is.EqualTo(1));
120+
Assert.That(Regex.Matches(File.ReadAllText(tempFile), "test message"), Has.Count.EqualTo(1));
121121
}
122122
finally
123123
{
@@ -137,7 +137,7 @@ public void ShouldAppendFileIfDoesNotExist()
137137
fileLogHandler.Handle(new LogEvent(typeof(FileLogHandlerTest), DateTimeOffset.Now, LogEventLevel.Info, "test message"));
138138
}
139139

140-
Assert.That(Regex.Matches(File.ReadAllText(tempFilePath), "test message").Count, Is.EqualTo(1));
140+
Assert.That(Regex.Matches(File.ReadAllText(tempFilePath), "test message"), Has.Count.EqualTo(1));
141141
}
142142
finally
143143
{

‎dotnet/test/common/NavigationTest.cs

+3-4
Original file line numberDiff line numberDiff line change
@@ -112,12 +112,11 @@ public void ShouldRefreshPage()
112112

113113
[Test]
114114
[NeedsFreshDriver(IsCreatedBeforeTest = true)]
115-
public Task ShouldNotHaveProblemNavigatingWithNoPagesBrowsedAsync()
115+
public void ShouldNotHaveProblemNavigatingWithNoPagesBrowsedAsync()
116116
{
117117
var navigation = driver.Navigate();
118-
Assert.DoesNotThrowAsync(async () => await navigation.BackAsync());
119-
Assert.DoesNotThrowAsync(async () => await navigation.ForwardAsync());
120-
return Task.CompletedTask;
118+
Assert.That(async () => await navigation.BackAsync(), Throws.Nothing);
119+
Assert.That(async () => await navigation.ForwardAsync(), Throws.Nothing);
121120
}
122121

123122
[Test]

‎dotnet/test/common/PositionAndSizeTest.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ public void ShouldGetCoordinatesInViewPortOfAnElementInAFrame()
111111
Assert.That(GetLocationOnPage(By.Id("box")), Is.EqualTo(new Point(10, 10)));
112112
// GetLocationInViewPort only works within the context of a single frame
113113
// for W3C-spec compliant remote ends.
114-
// Assert.AreEqual(new Point(25, 25), GetLocationInViewPort(By.Id("box")));
114+
// Assert.That(GetLocationInViewPort(By.Id("box")), Is.EqualTo(new Point(25, 25)));
115115
}
116116

117117
[Test]
@@ -123,7 +123,7 @@ public void ShouldGetCoordinatesInViewPortOfAnElementInANestedFrame()
123123
Assert.That(GetLocationOnPage(By.Id("box")), Is.EqualTo(new Point(10, 10)));
124124
// GetLocationInViewPort only works within the context of a single frame
125125
// for W3C-spec compliant remote ends.
126-
// Assert.AreEqual(new Point(40, 40), GetLocationInViewPort(By.Id("box")));
126+
// Assert.That(GetLocationInViewPort(By.Id("box")), Is.EqualTo(new Point(40, 40)));
127127
}
128128

129129
[Test]

‎dotnet/test/firefox/FirefoxDriverTest.cs

+3-9
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,9 @@ public void ShouldContinueToWorkIfUnableToFindElementById()
3434
{
3535
driver.Url = formsPage;
3636

37-
try
38-
{
39-
driver.FindElement(By.Id("notThere"));
40-
Assert.Fail("Should not be able to select element by id here");
41-
}
42-
catch (NoSuchElementException)
43-
{
44-
// This is expected
45-
}
37+
Assert.That(
38+
() => driver.FindElement(By.Id("notThere")),
39+
Throws.InstanceOf<NoSuchElementException>());
4640

4741
// Is this works, then we're golden
4842
driver.Url = xhtmlTestPage;

‎dotnet/test/remote/RemoteWebDriverSpecificTests.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public void ShouldBeAbleToSendFileToRemoteServer()
6262
IAllowsFileDetection fileDetectionDriver = driver as IAllowsFileDetection;
6363
if (fileDetectionDriver == null)
6464
{
65-
Assert.Fail("driver does not support file detection. This should not be");
65+
Assert.That(driver, Is.InstanceOf<IAllowsFileDetection>(), "driver does not support file detection. This should not be");
6666
}
6767

6868
fileDetectionDriver.FileDetector = new LocalFileDetector();

‎dotnet/test/support/Events/EventFiringWebDriverTest.cs

+3-9
Original file line numberDiff line numberDiff line change
@@ -184,15 +184,9 @@ public void ShouldCallListenerOnException()
184184
EventFiringWebDriver firingDriver = new EventFiringWebDriver(mockDriver.Object);
185185
firingDriver.ExceptionThrown += new EventHandler<WebDriverExceptionEventArgs>(firingDriver_ExceptionThrown);
186186

187-
try
188-
{
189-
firingDriver.FindElement(By.Id("foo"));
190-
Assert.Fail("Expected exception to be propogated");
191-
}
192-
catch (NoSuchElementException)
193-
{
194-
// Fine
195-
}
187+
Assert.That(
188+
() => firingDriver.FindElement(By.Id("foo")),
189+
Throws.InstanceOf<NoSuchElementException>());
196190

197191
Assert.That(log.ToString(), Does.Contain(exception.Message));
198192
}

‎dotnet/test/support/UI/LoadableComponentTests.cs

+9-19
Original file line numberDiff line numberDiff line change
@@ -53,31 +53,21 @@ public void ShouldThrowAnErrorIfCallingLoadDoesNotCauseTheComponentToLoad()
5353
{
5454
LoadsOk ok = new LoadsOk(false);
5555

56-
try
57-
{
58-
ok.Load();
59-
Assert.Fail();
60-
}
61-
catch (LoadableComponentException e)
62-
{
63-
Assert.That(e.Message, Is.EqualTo("Expected failure"));
64-
}
56+
Assert.That(
57+
() => ok.Load(),
58+
Throws.InstanceOf<LoadableComponentException>().With.Message.EqualTo("Expected failure"));
6559
}
6660

6761
[Test]
6862
public void ShouldCallHandleLoadErrorWhenWebDriverExceptionOccursDuringExecuteLoad()
6963
{
7064
ExecuteLoadThrows loadThrows = new ExecuteLoadThrows();
71-
try
72-
{
73-
loadThrows.Load();
74-
Assert.Fail();
75-
}
76-
catch (Exception e)
77-
{
78-
Assert.That(e.Message, Is.EqualTo("HandleLoadError called"));
79-
Assert.That(e.InnerException.Message, Is.EqualTo("Excpected failure in ExecuteLoad"));
80-
}
65+
66+
Assert.That(
67+
() => loadThrows.Load(),
68+
Throws.Exception
69+
.With.Message.EqualTo("HandleLoadError called")
70+
.And.InnerException.Message.EqualTo("Excpected failure in ExecuteLoad"));
8171

8272
}
8373

‎dotnet/test/support/UI/SlowLoadableComponentTest.cs

+7-18
Original file line numberDiff line numberDiff line change
@@ -65,31 +65,20 @@ public void TestTheLoadMethodShouldOnlyBeCalledOnceIfTheComponentTakesALongTimeT
6565
public void TestShouldThrowAnErrorIfCallingLoadDoesNotCauseTheComponentToLoadBeforeTimeout()
6666
{
6767
FakeClock clock = new FakeClock();
68-
try
69-
{
70-
new BasicSlowLoader(TimeSpan.FromSeconds(2), clock).Load();
71-
Assert.Fail();
72-
}
73-
catch (WebDriverTimeoutException)
74-
{
75-
// We expect to time out
76-
}
68+
69+
Assert.That(
70+
() => new BasicSlowLoader(TimeSpan.FromSeconds(2), clock).Load(),
71+
Throws.InstanceOf<WebDriverTimeoutException>());
7772
}
7873

7974
[Test]
8075
public void TestShouldCancelLoadingIfAnErrorIsDetected()
8176
{
8277
HasError error = new HasError();
8378

84-
try
85-
{
86-
error.Load();
87-
Assert.Fail();
88-
}
89-
catch (CustomException)
90-
{
91-
// This is expected
92-
}
79+
Assert.That(
80+
() => error.Load(),
81+
Throws.InstanceOf<CustomException>());
9382
}
9483

9584

‎dotnet/test/support/UI/WebDriverWaitTest.cs

+3-9
Original file line numberDiff line numberDiff line change
@@ -129,15 +129,9 @@ public void ChainsNoSuchElementExceptionWhenTimingOut()
129129

130130
var wait = new WebDriverWait(GetClock(), mockDriver.Object, ONE_SECONDS, ZERO_SECONDS);
131131

132-
try
133-
{
134-
wait.Until(condition);
135-
Assert.Fail("Expected WebDriverTimeoutException to be thrown");
136-
}
137-
catch (WebDriverTimeoutException e)
138-
{
139-
Assert.That(e.InnerException, Is.InstanceOf<NoSuchElementException>());
140-
}
132+
Assert.That(
133+
() => wait.Until(condition),
134+
Throws.InstanceOf<WebDriverTimeoutException>().With.InnerException.InstanceOf<NoSuchElementException>());
141135
}
142136

143137
private Func<IWebDriver, T> GetCondition<T>(Func<T> first, Func<T> second)

0 commit comments

Comments
 (0)
Please sign in to comment.