Skip to content

Commit 40ea8a4

Browse files
[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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 3 additions & 3 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 7 additions & 7 deletions
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

Lines changed: 7 additions & 7 deletions
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

Lines changed: 7 additions & 5 deletions
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

Lines changed: 7 additions & 5 deletions
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

Lines changed: 4 additions & 4 deletions
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
}

0 commit comments

Comments
 (0)