Skip to content

Commit

Permalink
[dotnet] Migrate remaining NUnit assertions to Assert.That and `Has…
Browse files Browse the repository at this point in the history
….Count` (SeleniumHQ#14870)
  • Loading branch information
RenderMichael authored and sandeepsuryaprasad committed Dec 7, 2024
1 parent f1f4768 commit a0b9225
Show file tree
Hide file tree
Showing 19 changed files with 75 additions and 111 deletions.
2 changes: 1 addition & 1 deletion dotnet/test/common/BiDi/Browser/BrowserTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public async Task CanGetUserContexts()
var userContexts = await bidi.Browser.GetUserContextsAsync();

Assert.That(userContexts, Is.Not.Null);
Assert.That(userContexts.Count, Is.GreaterThanOrEqualTo(2));
Assert.That(userContexts, Has.Count.GreaterThanOrEqualTo(2));
Assert.That(userContexts, Does.Contain(userContext1));
Assert.That(userContexts, Does.Contain(userContext2));
}
Expand Down
2 changes: 1 addition & 1 deletion dotnet/test/common/BiDi/Network/NetworkEventsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public async Task CanListenToBeforeRequestSentEventWithCookie()

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

Assert.That(req.Request.Cookies.Count, Is.EqualTo(1));
Assert.That(req.Request.Cookies, Has.Count.EqualTo(1));
Assert.That(req.Request.Cookies[0].Name, Is.EqualTo("foo"));
Assert.That((req.Request.Cookies[0].Value as BytesValue.String).Value, Is.EqualTo("bar"));
}
Expand Down
4 changes: 2 additions & 2 deletions dotnet/test/common/BiDi/Script/ScriptCommandsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public async Task CanGetAllRealms()
var realms = await bidi.Script.GetRealmsAsync();

Assert.That(realms, Is.Not.Null);
Assert.That(realms.Count, Is.EqualTo(2));
Assert.That(realms, Has.Count.EqualTo(2));

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

Assert.That(realms, Is.Not.Null);
Assert.That(realms.Count, Is.EqualTo(2));
Assert.That(realms, Has.Count.EqualTo(2));

Assert.That(realms[0], Is.AssignableFrom<RealmInfo.Window>());
Assert.That(realms[0].Realm, Is.Not.Null);
Expand Down
6 changes: 3 additions & 3 deletions dotnet/test/common/BiDi/Storage/StorageTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ await context.Storage.SetCookieAsync(new("fish", "cod", UrlBuilder.HostName)
var cookies = await context.Storage.GetCookiesAsync();

Assert.That(cookies, Is.Not.Null);
Assert.That(cookies.Count, Is.EqualTo(1));
Assert.That(cookies, Has.Count.EqualTo(1));

var cookie = cookies[0];

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

Assert.That(cookies, Is.Not.Null);
Assert.That(cookies.Count, Is.EqualTo(2));
Assert.That(cookies, Has.Count.EqualTo(2));
Assert.That(cookies[0].Name, Is.EqualTo("key1"));
Assert.That(cookies[1].Name, Is.EqualTo("key2"));
}
Expand Down Expand Up @@ -157,7 +157,7 @@ public async Task CanDeleteCookieWithName()
var cookies = await bidi.Storage.GetCookiesAsync();

Assert.That(cookies, Is.Not.Null);
Assert.That(cookies.Count, Is.EqualTo(1));
Assert.That(cookies, Has.Count.EqualTo(1));
Assert.That(cookies[0].Name, Is.EqualTo("key2"));
}

Expand Down
2 changes: 1 addition & 1 deletion dotnet/test/common/ClickScrollingTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public void ClickingOnAnchorScrollsPage()

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

//Focusing on to click, but not actually following,
//the link will scroll it in to view, which is a few pixels further than 0
Expand Down
14 changes: 7 additions & 7 deletions dotnet/test/common/DriverElementFindingTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,55 +93,55 @@ public void ShouldFindElementsById()
{
driver.Url = nestedPage;
ReadOnlyCollection<IWebElement> elements = driver.FindElements(By.Id("test_id"));
Assert.That(elements.Count, Is.EqualTo(2));
Assert.That(elements, Has.Count.EqualTo(2));
}

[Test]
public void ShouldFindElementsByLinkText()
{
driver.Url = nestedPage;
ReadOnlyCollection<IWebElement> elements = driver.FindElements(By.LinkText("hello world"));
Assert.That(elements.Count, Is.EqualTo(12));
Assert.That(elements, Has.Count.EqualTo(12));
}

[Test]
public void ShouldFindElementsByName()
{
driver.Url = nestedPage;
ReadOnlyCollection<IWebElement> elements = driver.FindElements(By.Name("form1"));
Assert.That(elements.Count, Is.EqualTo(4));
Assert.That(elements, Has.Count.EqualTo(4));
}

[Test]
public void ShouldFindElementsByXPath()
{
driver.Url = nestedPage;
ReadOnlyCollection<IWebElement> elements = driver.FindElements(By.XPath("//a"));
Assert.That(elements.Count, Is.EqualTo(12));
Assert.That(elements, Has.Count.EqualTo(12));
}

[Test]
public void ShouldFindElementsByClassName()
{
driver.Url = nestedPage;
ReadOnlyCollection<IWebElement> elements = driver.FindElements(By.ClassName("one"));
Assert.That(elements.Count, Is.EqualTo(3));
Assert.That(elements, Has.Count.EqualTo(3));
}

[Test]
public void ShouldFindElementsByPartialLinkText()
{
driver.Url = nestedPage;
ReadOnlyCollection<IWebElement> elements = driver.FindElements(By.PartialLinkText("world"));
Assert.That(elements.Count, Is.EqualTo(12));
Assert.That(elements, Has.Count.EqualTo(12));
}

[Test]
public void ShouldFindElementsByTagName()
{
driver.Url = nestedPage;
ReadOnlyCollection<IWebElement> elements = driver.FindElements(By.TagName("a"));
Assert.That(elements.Count, Is.EqualTo(12));
Assert.That(elements, Has.Count.EqualTo(12));
}
#endregion
}
Expand Down
14 changes: 7 additions & 7 deletions dotnet/test/common/ElementElementFindingTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public void ShouldFindElementsById()
driver.Url = nestedPage;
IWebElement parent = driver.FindElement(By.Name("form2"));
ReadOnlyCollection<IWebElement> children = parent.FindElements(By.Id("2"));
Assert.That(children.Count, Is.EqualTo(2));
Assert.That(children, Has.Count.EqualTo(2));
}

[Test]
Expand All @@ -110,7 +110,7 @@ public void ShouldFindElementsByLinkText()
driver.Url = nestedPage;
IWebElement parent = driver.FindElement(By.Name("div1"));
ReadOnlyCollection<IWebElement> children = parent.FindElements(By.PartialLinkText("hello world"));
Assert.That(children.Count, Is.EqualTo(2));
Assert.That(children, Has.Count.EqualTo(2));
Assert.That(children[0].Text, Is.EqualTo("hello world"));
Assert.That(children[1].Text, Is.EqualTo("hello world"));
}
Expand All @@ -121,7 +121,7 @@ public void ShouldFindElementsByName()
driver.Url = nestedPage;
IWebElement parent = driver.FindElement(By.Name("form2"));
ReadOnlyCollection<IWebElement> children = parent.FindElements(By.Name("selectomatic"));
Assert.That(children.Count, Is.EqualTo(2));
Assert.That(children, Has.Count.EqualTo(2));
}

[Test]
Expand All @@ -130,7 +130,7 @@ public void ShouldFindElementsByXPath()
driver.Url = nestedPage;
IWebElement parent = driver.FindElement(By.Name("classes"));
ReadOnlyCollection<IWebElement> children = parent.FindElements(By.XPath("span"));
Assert.That(children.Count, Is.EqualTo(3));
Assert.That(children, Has.Count.EqualTo(3));
Assert.That(children[0].Text, Is.EqualTo("Find me"));
Assert.That(children[1].Text, Is.EqualTo("Also me"));
Assert.That(children[2].Text, Is.EqualTo("But not me"));
Expand All @@ -142,7 +142,7 @@ public void ShouldFindElementsByClassName()
driver.Url = nestedPage;
IWebElement parent = driver.FindElement(By.Name("classes"));
ReadOnlyCollection<IWebElement> children = parent.FindElements(By.ClassName("one"));
Assert.That(children.Count, Is.EqualTo(2));
Assert.That(children, Has.Count.EqualTo(2));
Assert.That(children[0].Text, Is.EqualTo("Find me"));
Assert.That(children[1].Text, Is.EqualTo("Also me"));
}
Expand All @@ -153,7 +153,7 @@ public void ShouldFindElementsByPartialLinkText()
driver.Url = nestedPage;
IWebElement parent = driver.FindElement(By.Name("div1"));
ReadOnlyCollection<IWebElement> children = parent.FindElements(By.PartialLinkText("hello "));
Assert.That(children.Count, Is.EqualTo(2));
Assert.That(children, Has.Count.EqualTo(2));
Assert.That(children[0].Text, Is.EqualTo("hello world"));
Assert.That(children[1].Text, Is.EqualTo("hello world"));
}
Expand All @@ -164,7 +164,7 @@ public void ShouldFindElementsByTagName()
driver.Url = nestedPage;
IWebElement parent = driver.FindElement(By.Name("classes"));
ReadOnlyCollection<IWebElement> children = parent.FindElements(By.TagName("span"));
Assert.That(children.Count, Is.EqualTo(3));
Assert.That(children, Has.Count.EqualTo(3));
Assert.That(children[0].Text, Is.EqualTo("Find me"));
Assert.That(children[1].Text, Is.EqualTo("Also me"));
Assert.That(children[2].Text, Is.EqualTo("But not me"));
Expand Down
12 changes: 7 additions & 5 deletions dotnet/test/common/ExecutingAsyncJavascriptTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ public void ShouldBeAbleToReturnArraysOfPrimitivesFromAsyncScripts()
Assert.That(result, Is.Not.Null);
Assert.That(result, Is.InstanceOf<ReadOnlyCollection<object>>());
ReadOnlyCollection<object> resultList = result as ReadOnlyCollection<object>;
Assert.That(resultList.Count, Is.EqualTo(5));
Assert.That(resultList, Has.Count.EqualTo(5));
Assert.That(resultList[0], Is.Null);
Assert.That((long)resultList[1], Is.EqualTo(123));
Assert.That(resultList[2].ToString(), Is.EqualTo("abc"));
Expand Down Expand Up @@ -221,10 +221,12 @@ public void ShouldCatchErrorsWithMessageAndStacktraceWhenExecutingInitialScript(
string js = "function functionB() { throw Error('errormessage'); };"
+ "function functionA() { functionB(); };"
+ "functionA();";
Exception ex = Assert.Catch(() => executor.ExecuteAsyncScript(js));
Assert.That(ex, Is.InstanceOf<WebDriverException>());
Assert.That(ex.Message.Contains("errormessage"));
Assert.That(ex.StackTrace.Contains("functionB"));

Assert.That(
() => executor.ExecuteAsyncScript(js),
Throws.InstanceOf<WebDriverException>()
.With.Message.Contains("errormessage")
.And.Property(nameof(WebDriverException.StackTrace)).Contains("functionB"));
}

[Test]
Expand Down
12 changes: 7 additions & 5 deletions dotnet/test/common/ExecutingJavascriptTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -256,10 +256,12 @@ public void ShouldThrowAnExceptionWithMessageAndStacktraceWhenTheJavascriptIsBad
string js = "function functionB() { throw Error('errormessage'); };"
+ "function functionA() { functionB(); };"
+ "functionA();";
Exception ex = Assert.Catch(() => ExecuteScript(js));
Assert.That(ex, Is.InstanceOf<WebDriverException>());
Assert.That(ex.Message, Does.Contain("errormessage"), "Exception message does not contain 'errormessage'");
Assert.That(ex.StackTrace, Does.Contain("functionB"), "Exception message does not contain 'functionB'");

Assert.That(
() => ExecuteScript(js),
Throws.InstanceOf<WebDriverException>()
.With.Message.Contains("errormessage")
.And.Property(nameof(WebDriverException.StackTrace)).Contains("functionB"));
}

[Test]
Expand Down Expand Up @@ -467,7 +469,7 @@ public void ShouldBeAbleToExecuteABigChunkOfJavascriptCode()
if (fileList.Length > 0)
{
string jquery = System.IO.File.ReadAllText(fileList[0]);
Assert.That(jquery.Length, Is.GreaterThan(50000));
Assert.That(jquery, Has.Length.GreaterThan(50000));
ExecuteScript(jquery, null);
}
}
Expand Down
8 changes: 4 additions & 4 deletions dotnet/test/common/Interactions/ActionBuilderTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

using NUnit.Framework;
using System;
using System.Collections;
using System.Collections.Generic;

namespace OpenQA.Selenium.Interactions
Expand Down Expand Up @@ -55,8 +54,9 @@ public void OutputsPointerEventsToDictionary()
Assert.That(dictionary, Does.ContainKey("type").WithValue("pointer"));
Assert.That(dictionary["id"], Is.Not.Null);
Assert.That(dictionary["parameters"], Is.Not.Null);

var parameters = new Dictionary<string, object> { { "pointerType", "pen" } };
CollectionAssert.AreEquivalent(parameters, (IEnumerable)dictionary["parameters"]);
Assert.That(dictionary["parameters"], Is.EquivalentTo(parameters));

var events = new Dictionary<string, object>
{
Expand All @@ -72,8 +72,8 @@ public void OutputsPointerEventsToDictionary()
{"type", "pointerDown"},
{"button", 0}
};
var actions = (IList<Object>)dictionary["actions"];
CollectionAssert.AreEquivalent(events, (IEnumerable)actions[0]);
var actions = (IList<object>)dictionary["actions"];
Assert.That(actions[0], Is.EquivalentTo(events));
}
}
}
8 changes: 4 additions & 4 deletions dotnet/test/common/Internal/Logging/FileLogHandlerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public void ShouldCreateFileIfDoesNotExist()
fileLogHandler2.Handle(new LogEvent(typeof(FileLogHandlerTest), DateTimeOffset.Now, LogEventLevel.Info, "test message"));
}

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

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

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

Assert.That(Regex.Matches(File.ReadAllText(tempFilePath), "test message").Count, Is.EqualTo(1));
Assert.That(Regex.Matches(File.ReadAllText(tempFilePath), "test message"), Has.Count.EqualTo(1));
}
finally
{
Expand Down
7 changes: 3 additions & 4 deletions dotnet/test/common/NavigationTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,12 +112,11 @@ public void ShouldRefreshPage()

[Test]
[NeedsFreshDriver(IsCreatedBeforeTest = true)]
public Task ShouldNotHaveProblemNavigatingWithNoPagesBrowsedAsync()
public void ShouldNotHaveProblemNavigatingWithNoPagesBrowsedAsync()
{
var navigation = driver.Navigate();
Assert.DoesNotThrowAsync(async () => await navigation.BackAsync());
Assert.DoesNotThrowAsync(async () => await navigation.ForwardAsync());
return Task.CompletedTask;
Assert.That(async () => await navigation.BackAsync(), Throws.Nothing);
Assert.That(async () => await navigation.ForwardAsync(), Throws.Nothing);
}

[Test]
Expand Down
4 changes: 2 additions & 2 deletions dotnet/test/common/PositionAndSizeTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public void ShouldGetCoordinatesInViewPortOfAnElementInAFrame()
Assert.That(GetLocationOnPage(By.Id("box")), Is.EqualTo(new Point(10, 10)));
// GetLocationInViewPort only works within the context of a single frame
// for W3C-spec compliant remote ends.
// Assert.AreEqual(new Point(25, 25), GetLocationInViewPort(By.Id("box")));
// Assert.That(GetLocationInViewPort(By.Id("box")), Is.EqualTo(new Point(25, 25)));
}

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

[Test]
Expand Down
12 changes: 3 additions & 9 deletions dotnet/test/firefox/FirefoxDriverTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,9 @@ public void ShouldContinueToWorkIfUnableToFindElementById()
{
driver.Url = formsPage;

try
{
driver.FindElement(By.Id("notThere"));
Assert.Fail("Should not be able to select element by id here");
}
catch (NoSuchElementException)
{
// This is expected
}
Assert.That(
() => driver.FindElement(By.Id("notThere")),
Throws.InstanceOf<NoSuchElementException>());

// Is this works, then we're golden
driver.Url = xhtmlTestPage;
Expand Down
2 changes: 1 addition & 1 deletion dotnet/test/remote/RemoteWebDriverSpecificTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public void ShouldBeAbleToSendFileToRemoteServer()
IAllowsFileDetection fileDetectionDriver = driver as IAllowsFileDetection;
if (fileDetectionDriver == null)
{
Assert.Fail("driver does not support file detection. This should not be");
Assert.That(driver, Is.InstanceOf<IAllowsFileDetection>(), "driver does not support file detection. This should not be");
}

fileDetectionDriver.FileDetector = new LocalFileDetector();
Expand Down
12 changes: 3 additions & 9 deletions dotnet/test/support/Events/EventFiringWebDriverTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -184,15 +184,9 @@ public void ShouldCallListenerOnException()
EventFiringWebDriver firingDriver = new EventFiringWebDriver(mockDriver.Object);
firingDriver.ExceptionThrown += new EventHandler<WebDriverExceptionEventArgs>(firingDriver_ExceptionThrown);

try
{
firingDriver.FindElement(By.Id("foo"));
Assert.Fail("Expected exception to be propogated");
}
catch (NoSuchElementException)
{
// Fine
}
Assert.That(
() => firingDriver.FindElement(By.Id("foo")),
Throws.InstanceOf<NoSuchElementException>());

Assert.That(log.ToString(), Does.Contain(exception.Message));
}
Expand Down
Loading

0 comments on commit a0b9225

Please sign in to comment.