diff --git a/dotnet/src/webdriver/WebDriver.cs b/dotnet/src/webdriver/WebDriver.cs index e29ce29a12c79..6bd964136c928 100644 --- a/dotnet/src/webdriver/WebDriver.cs +++ b/dotnet/src/webdriver/WebDriver.cs @@ -935,7 +935,7 @@ private static object ConvertObjectToJavaScriptObject(object arg) } else { - throw new ArgumentException("Argument is of an illegal type" + arg.ToString(), nameof(arg)); + throw new ArgumentException("Argument is of an illegal type: " + arg.ToString(), nameof(arg)); } return converted; diff --git a/dotnet/test/common/AlertsTest.cs b/dotnet/test/common/AlertsTest.cs index a99706724838b..84a05f4a6a78c 100644 --- a/dotnet/test/common/AlertsTest.cs +++ b/dotnet/test/common/AlertsTest.cs @@ -60,7 +60,9 @@ public void ShouldThrowArgumentNullExceptionWhenKeysNull() IAlert alert = WaitFor(AlertToBePresent, "No alert found"); try { - Assert.That(() => alert.SendKeys(null), Throws.ArgumentNullException); + Assert.That( + () => alert.SendKeys(null), + Throws.ArgumentNullException); } finally { @@ -147,13 +149,12 @@ public void SettingTheValueOfAnAlertThrows() driver.FindElement(By.Id("alert")).Click(); IAlert alert = WaitFor(AlertToBePresent, "No alert found"); + try { - alert.SendKeys("cheese"); - Assert.Fail("Expected exception"); - } - catch (ElementNotInteractableException) - { + Assert.That( + () => alert.SendKeys("cheese"), + Throws.TypeOf()); } finally { @@ -198,8 +199,10 @@ public void AlertShouldNotAllowAdditionalCommandsIfDismissed() IAlert alert = WaitFor(AlertToBePresent, "No alert found"); alert.Dismiss(); - string text; - Assert.That(() => text = alert.Text, Throws.InstanceOf()); + + Assert.That( + () => alert.Text, + Throws.TypeOf()); } [Test] @@ -249,7 +252,9 @@ public void SwitchingToMissingAlertThrows() { driver.Url = CreateAlertPage("cheese"); - Assert.That(() => AlertToBePresent(), Throws.InstanceOf()); + Assert.That( + () => AlertToBePresent(), + Throws.TypeOf()); } [Test] @@ -270,15 +275,9 @@ public void SwitchingToMissingAlertInAClosedWindowThrows() driver.Close(); WaitFor(WindowHandleCountToBe(1), "Window count was not 1"); - try - { - AlertToBePresent().Accept(); - Assert.Fail("Expected exception"); - } - catch (NoSuchWindowException) - { - // Expected - } + Assert.That( + () => AlertToBePresent().Accept(), + Throws.TypeOf()); } finally @@ -321,17 +320,22 @@ public void HandlesTwoAlertsFromOneInteraction() { driver.Url = EnvironmentManager.Instance.UrlBuilder.CreateInlinePage(new InlinePage() .WithScripts( - "function setInnerText(id, value) {", - " document.getElementById(id).innerHTML = '

' + value + '

';", - "}", - "function displayTwoPrompts() {", - " setInnerText('text1', prompt('First'));", - " setInnerText('text2', prompt('Second'));", - "}") + """ + function setInnerText(id, value) { + document.getElementById(id).innerHTML = '

' + value + '

'; + } + + function displayTwoPrompts() { + setInnerText('text1', prompt('First')); + setInnerText('text2', prompt('Second')); + } + """) .WithBody( - "click me", - "
", - "
")); + """ + click me +
+
+ """)); driver.FindElement(By.Id("double-prompt")).Click(); @@ -355,7 +359,7 @@ public void HandlesTwoAlertsFromOneInteraction() public void ShouldHandleAlertOnPageLoad() { string pageWithOnLoad = EnvironmentManager.Instance.UrlBuilder.CreateInlinePage(new InlinePage() - .WithOnLoad("javascript:alert(\"onload\")") + .WithOnLoad("""javascript:alert("onload")""") .WithBody("

Page with onload event handler

")); driver.Url = EnvironmentManager.Instance.UrlBuilder.CreateInlinePage(new InlinePage() .WithBody(string.Format("open new page", pageWithOnLoad))); @@ -411,17 +415,12 @@ public void ShouldNotHandleAlertInAnotherWindow() Assert.AreEqual(1, allWindows.Count); onloadWindow = allWindows[0]; - try + Assert.That(() => { IWebElement el = driver.FindElement(By.Id("open-new-window")); WaitFor(AlertToBePresent, TimeSpan.FromSeconds(5), "No alert found"); - Assert.Fail("Expected exception"); - } - catch (WebDriverException) - { - // An operation timed out exception is expected, - // since we're using WaitFor. - } + }, + Throws.TypeOf()); } finally @@ -442,15 +441,10 @@ public void IncludesAlertTextInUnhandledAlertException() driver.FindElement(By.Id("alert")).Click(); WaitFor(AlertToBePresent, "No alert found"); - try - { - string title = driver.Title; - Assert.Fail("Expected UnhandledAlertException"); - } - catch (UnhandledAlertException e) - { - Assert.AreEqual("cheese", e.AlertText); - } + + Assert.That( + () => driver.Title, + Throws.TypeOf().With.Property(nameof(UnhandledAlertException.AlertText)).EqualTo("cheese")); } [Test] @@ -522,16 +516,14 @@ private Func ElementToBePresent(By locator) { return () => { - IWebElement foundElement = null; try { - foundElement = driver.FindElement(By.Id("open-page-with-onunload-alert")); + return driver.FindElement(By.Id("open-page-with-onunload-alert")); } catch (NoSuchElementException) { + return null; } - - return foundElement; }; } @@ -554,9 +546,8 @@ private Func WindowWithName(string name) } catch (NoSuchWindowException) { + return false; } - - return false; }; } diff --git a/dotnet/test/common/CorrectEventFiringTest.cs b/dotnet/test/common/CorrectEventFiringTest.cs index e358197100d08..d30e205b485fd 100644 --- a/dotnet/test/common/CorrectEventFiringTest.cs +++ b/dotnet/test/common/CorrectEventFiringTest.cs @@ -348,20 +348,12 @@ public void SendingKeysToAFocusedElementShouldNotBlurThatElement() focused = true; break; } - try - { - System.Threading.Thread.Sleep(200); - } - catch (Exception) - { - throw; - } - } - if (!focused) - { - Assert.Fail("Clicking on element didn't focus it in time - can't proceed so failing"); + + System.Threading.Thread.Sleep(200); } + Assert.That(focused, Is.True, "Clicking on element didn't focus it in time - can't proceed so failing"); + element.SendKeys("a"); AssertEventNotFired("blur"); } diff --git a/dotnet/test/common/ElementAttributeTest.cs b/dotnet/test/common/ElementAttributeTest.cs index 21ed7305a1801..43a1be4a063a9 100644 --- a/dotnet/test/common/ElementAttributeTest.cs +++ b/dotnet/test/common/ElementAttributeTest.cs @@ -127,28 +127,19 @@ public void ShouldThrowExceptionIfSendingKeysToElementDisabledUsingRandomDisable { driver.Url = formsPage; IWebElement disabledTextElement1 = driver.FindElement(By.Id("disabledTextElement1")); - try + + Assert.That(() => { disabledTextElement1.SendKeys("foo"); - Assert.Fail("Should have thrown exception"); - } - catch (InvalidElementStateException) - { - //Expected - } + }, Throws.TypeOf()); Assert.AreEqual(string.Empty, disabledTextElement1.Text); IWebElement disabledTextElement2 = driver.FindElement(By.Id("disabledTextElement2")); - try - { - disabledTextElement2.SendKeys("bar"); - Assert.Fail("Should have thrown exception"); - } - catch (InvalidElementStateException) - { - //Expected - } + + Assert.That( + () => disabledTextElement2.SendKeys("bar"), + Throws.TypeOf()); Assert.AreEqual(string.Empty, disabledTextElement2.Text); } diff --git a/dotnet/test/common/Environment/RemoteSeleniumServer.cs b/dotnet/test/common/Environment/RemoteSeleniumServer.cs index 388bc6b1e3083..8c12943c928f3 100644 --- a/dotnet/test/common/Environment/RemoteSeleniumServer.cs +++ b/dotnet/test/common/Environment/RemoteSeleniumServer.cs @@ -91,14 +91,15 @@ public async Task StopAsync() { if (autoStart && webserverProcess != null && !webserverProcess.HasExited) { - using var httpClient = new HttpClient(); - - try - { - using var response = await httpClient.GetAsync("http://localhost:6000/selenium-server/driver?cmd=shutDownSeleniumServer"); - } - catch (Exception ex) when (ex is HttpRequestException || ex is TimeoutException) + using (var httpClient = new HttpClient()) { + try + { + using var response = await httpClient.GetAsync("http://localhost:6000/selenium-server/driver?cmd=shutDownSeleniumServer"); + } + catch (Exception ex) when (ex is HttpRequestException || ex is TimeoutException) + { + } } webserverProcess.WaitForExit(10000); diff --git a/dotnet/test/common/ExecutingJavascriptTest.cs b/dotnet/test/common/ExecutingJavascriptTest.cs index 048b712d3178d..34a567a7a92c2 100644 --- a/dotnet/test/common/ExecutingJavascriptTest.cs +++ b/dotnet/test/common/ExecutingJavascriptTest.cs @@ -391,14 +391,17 @@ public void ShouldBeAbleToPassACollectionAsArgument() Assert.AreEqual(collection.Count, length); } - + [Test] public void ShouldThrowAnExceptionIfAnArgumentIsNotValid() { if (!(driver is IJavaScriptExecutor)) return; driver.Url = javascriptPage; - Assert.That(() => ExecuteScript("return arguments[0];", driver), Throws.InstanceOf()); + + Assert.That( + () => ExecuteScript("return arguments[0];", driver), + Throws.ArgumentException.With.Message.StartsWith("Argument is of an illegal type: ")); } [Test] @@ -490,10 +493,9 @@ public async Task ShouldBeAbleToPinJavascriptCodeAndExecuteRepeatedly() await jsEngine.UnpinScript(script); - Assert.That(() => - { - _ = ((IJavaScriptExecutor)driver).ExecuteScript(script); - }, Throws.TypeOf()); + Assert.That( + () => ((IJavaScriptExecutor)driver).ExecuteScript(script), + Throws.TypeOf()); } [Test] @@ -566,7 +568,9 @@ public void ShouldThrowAnExceptionWhenArgumentsWithStaleElementPassed() Dictionary args = new Dictionary(); args["key"] = new object[] { "a", new object[] { "zero", 1, true, 3.14159, false, el }, "c" }; - Assert.That(() => executor.ExecuteScript("return undefined;", args), Throws.InstanceOf()); + Assert.That( + () => executor.ExecuteScript("return undefined;", args), + Throws.TypeOf()); } [Test] @@ -611,7 +615,17 @@ public void ShouldHandleRecursiveStructures() { driver.Url = simpleTestPage; - Assert.That(() => ExecuteScript("var obj1 = {}; var obj2 = {}; obj1['obj2'] = obj2; obj2['obj1'] = obj1; return obj1"), Throws.InstanceOf()); + Assert.That( + () => ExecuteScript( + """ + var obj1 = {}; + var obj2 = {}; + obj1['obj2'] = obj2; + obj2['obj1'] = obj1; + return obj1 + """ + ), + Throws.TypeOf()); } //------------------------------------------------------------------ @@ -622,26 +636,68 @@ public void ShouldHandleRecursiveStructures() [Ignore("Reason for ignore: Failure indicates hang condition, which would break the test suite. Really needs a timeout set.")] public void ShouldThrowExceptionIfExecutingOnNoPage() { - bool exceptionCaught = false; - try - { - ((IJavaScriptExecutor)driver).ExecuteScript("return 1;"); - } - catch (WebDriverException) - { - exceptionCaught = true; - } - - if (!exceptionCaught) - { - Assert.Fail("Expected an exception to be caught"); - } + Assert.That( + () => ((IJavaScriptExecutor)driver).ExecuteScript("return 1;"), + Throws.InstanceOf()); } [Test] public void ExecutingLargeJavaScript() { - string script = "// stolen from injectableSelenium.js in WebDriver\nvar browserbot = {\n\n triggerEvent: function(element, eventType, canBubble, controlKeyDown, altKeyDown, shiftKeyDown, metaKeyDown) {\n canBubble = (typeof(canBubble) == undefined) ? true: canBubble;\n if (element.fireEvent && element.ownerDocument && element.ownerDocument.createEventObject) {\n // IE\n var evt = this.createEventObject(element, controlKeyDown, altKeyDown, shiftKeyDown, metaKeyDown);\n element.fireEvent('on' + eventType,evt);\n } else {\n var evt = document.createEvent('HTMLEvents');\n\n try {\n evt.shiftKey = shiftKeyDown;\n evt.metaKey = metaKeyDown;\n evt.altKey = altKeyDown;\n evt.ctrlKey = controlKeyDown;\n } catch(e) {\n // Nothing sane to do\n }\n\n evt.initEvent(eventType, canBubble, true);\n return element.dispatchEvent(evt);\n }\n },\n\n getVisibleText: function() {\n var selection = getSelection();\n var range = document.createRange();\n range.selectNodeContents(document.documentElement);\n selection.addRange(range);\nvar string = selection.toString();\n selection.removeAllRanges();\n\n return string;\n },\n\n getOuterHTML: function(element) {\n if(element.outerHTML) {\n return element.outerHTML;\n } else if(typeof(XMLSerializer) != undefined) {\n return new XMLSerializer().serializeToString(element);\n } else {\n throw \"can't get outerHTML in this browser\";\n }\n }\n\n\n};return browserbot.getOuterHTML.apply(browserbot, arguments);"; + string script = """ + // stolen from injectableSelenium.js in WebDriver + var browserbot = { + + triggerEvent: function(element, eventType, canBubble, controlKeyDown, altKeyDown, shiftKeyDown, metaKeyDown) { + canBubble = (typeof(canBubble) == undefined) ? true : canBubble; + + if (element.fireEvent && element.ownerDocument && element.ownerDocument.createEventObject) { + // IE + var evt = this.createEventObject(element, controlKeyDown, altKeyDown, shiftKeyDown, metaKeyDown); + element.fireEvent('on' + eventType,evt); + } else { + var evt = document.createEvent('HTMLEvents'); + + try { + evt.shiftKey = shiftKeyDown; + evt.metaKey = metaKeyDown; + evt.altKey = altKeyDown; + evt.ctrlKey = controlKeyDown; + } catch(e) { + // Nothing sane to do + } + + evt.initEvent(eventType, canBubble, true); + return element.dispatchEvent(evt); + } + }, + + getVisibleText: function() { + var selection = getSelection(); + var range = document.createRange(); + range.selectNodeContents(document.documentElement); + selection.addRange(range); + + var string = selection.toString(); + selection.removeAllRanges(); + + return string; + }, + + getOuterHTML: function(element) { + if(element.outerHTML) { + return element.outerHTML; + } else if(typeof(XMLSerializer) != undefined) { + return new XMLSerializer().serializeToString(element); + } else { + throw "can't get outerHTML in this browser"; + } + } + }; + + return browserbot.getOuterHTML.apply(browserbot, arguments); + """; + driver.Url = javascriptPage; IWebElement element = driver.FindElement(By.TagName("body")); object x = ExecuteScript(script, element); diff --git a/dotnet/test/common/FrameSwitchingTest.cs b/dotnet/test/common/FrameSwitchingTest.cs index c669ab8fca519..a5e9777e2f48a 100644 --- a/dotnet/test/common/FrameSwitchingTest.cs +++ b/dotnet/test/common/FrameSwitchingTest.cs @@ -175,28 +175,16 @@ public void FrameSearchesShouldBeRelativeToTheCurrentlySelectedFrame() driver.SwitchTo().Frame(frameElement); Assert.AreEqual("2", driver.FindElement(By.Id("pageNumber")).Text); - try - { - driver.SwitchTo().Frame("third"); - Assert.Fail(); - } - catch (NoSuchFrameException) - { - // Do nothing - } + Assert.That( + () => driver.SwitchTo().Frame("third"), + Throws.TypeOf()); driver.SwitchTo().DefaultContent(); driver.SwitchTo().Frame("third"); - try - { - driver.SwitchTo().Frame("second"); - Assert.Fail(); - } - catch (NoSuchFrameException) - { - // Do nothing - } + Assert.That( + () => driver.SwitchTo().Frame("second"), + Throws.TypeOf()); driver.SwitchTo().DefaultContent(); driver.SwitchTo().Frame("second"); @@ -216,7 +204,9 @@ public void ShouldThrowFrameNotFoundExceptionLookingUpSubFramesWithSuperFrameNam { driver.Url = framesetPage; driver.SwitchTo().Frame("fourth"); - Assert.That(() => driver.SwitchTo().Frame("second"), Throws.InstanceOf()); + Assert.That( + () => driver.SwitchTo().Frame("second"), + Throws.TypeOf()); } @@ -224,14 +214,20 @@ public void ShouldThrowFrameNotFoundExceptionLookingUpSubFramesWithSuperFrameNam public void ShouldThrowAnExceptionWhenAFrameCannotBeFound() { driver.Url = xhtmlTestPage; - Assert.That(() => driver.SwitchTo().Frame("Nothing here"), Throws.InstanceOf()); + + Assert.That( + () => driver.SwitchTo().Frame("Nothing here"), + Throws.TypeOf()); } [Test] public void ShouldThrowAnExceptionWhenAFrameCannotBeFoundByIndex() { driver.Url = xhtmlTestPage; - Assert.That(() => driver.SwitchTo().Frame(27), Throws.InstanceOf()); + + Assert.That( + () => driver.SwitchTo().Frame(27), + Throws.TypeOf()); } [Test] @@ -470,7 +466,9 @@ public void ShouldNotBeAbleToDoAnythingTheFrameIsDeletedFromUnderUs() IWebElement killIframe = driver.FindElement(By.Id("killIframe")); killIframe.Click(); - Assert.That(() => driver.FindElement(By.Id("killIframe")), Throws.InstanceOf()); + Assert.That( + () => driver.FindElement(By.Id("killIframe")), + Throws.TypeOf()); } [Test] @@ -615,9 +613,8 @@ private bool FrameExistsAndSwitchedTo(string locator) } catch (NoSuchFrameException) { + return false; } - - return false; } private bool FrameExistsAndSwitchedTo(int index) @@ -629,9 +626,8 @@ private bool FrameExistsAndSwitchedTo(int index) } catch (NoSuchFrameException) { + return false; } - - return false; } private bool FrameExistsAndSwitchedTo(IWebElement frameElement) @@ -643,9 +639,8 @@ private bool FrameExistsAndSwitchedTo(IWebElement frameElement) } catch (NoSuchFrameException) { + return false; } - - return false; } } } diff --git a/dotnet/test/common/TargetLocatorTest.cs b/dotnet/test/common/TargetLocatorTest.cs index 7985431a68a1c..19a23cb1dd086 100644 --- a/dotnet/test/common/TargetLocatorTest.cs +++ b/dotnet/test/common/TargetLocatorTest.cs @@ -30,14 +30,20 @@ public class TargetLocatorTest : DriverTestFixture public void ShouldThrowExceptionAfterSwitchingToNonExistingFrameIndex() { driver.Url = framesPage; - Assert.That(() => driver.SwitchTo().Frame(10), Throws.InstanceOf()); + + Assert.That( + () => driver.SwitchTo().Frame(10), + Throws.TypeOf()); } [Test] public void ShouldThrowExceptionAfterSwitchingToNonExistingFrameName() { driver.Url = framesPage; - Assert.That(() => driver.SwitchTo().Frame("æ©ñµøöíúüþ®éåä²doesnotexist"), Throws.InstanceOf()); + + Assert.That( + () => driver.SwitchTo().Frame("æ©ñµøöíúüþ®éåä²doesnotexist"), + Throws.TypeOf()); } [Test] @@ -45,7 +51,10 @@ public void ShouldThrowExceptionAfterSwitchingToNullFrameName() { string frameName = null; driver.Url = framesPage; - Assert.That(() => driver.SwitchTo().Frame(frameName), Throws.InstanceOf()); + + Assert.That( + () => driver.SwitchTo().Frame(frameName), + Throws.TypeOf()); } [Test] @@ -83,29 +92,21 @@ public void ShouldSwitchToFrameByNameAndBackToDefaultContent() Assert.AreEqual(driver.FindElement(By.Id("pageNumber")).Text, "1"); driver.SwitchTo().DefaultContent(); - try - { - // DefaultContent should not have the element in it. - Assert.AreEqual(driver.FindElement(By.Id("pageNumber")).Text, "1"); - Assert.Fail("Should not be able to get element in frame from DefaultContent"); - } - catch (NoSuchElementException) - { - } + + // DefaultContent should not have the element in it. + Assert.That( + () => driver.FindElement(By.Id("pageNumber")), + Throws.TypeOf()); driver.SwitchTo().Frame("second"); Assert.AreEqual(driver.FindElement(By.Id("pageNumber")).Text, "2"); driver.SwitchTo().DefaultContent(); - try - { - // DefaultContent should not have the element in it. - Assert.AreEqual(driver.FindElement(By.Id("pageNumber")).Text, "1"); - Assert.Fail("Should not be able to get element in frame from DefaultContent"); - } - catch (NoSuchElementException) - { - } + + // DefaultContent should not have the element in it. + Assert.That( + () => driver.FindElement(By.Id("pageNumber")), + Throws.TypeOf()); } [Test] @@ -117,29 +118,22 @@ public void ShouldSwitchToFrameByIndexAndBackToDefaultContent() Assert.AreEqual(driver.FindElement(By.Id("pageNumber")).Text, "1"); driver.SwitchTo().DefaultContent(); - try - { - // DefaultContent should not have the element in it. - Assert.AreEqual(driver.FindElement(By.Id("pageNumber")).Text, "1"); - Assert.Fail("Should not be able to get element in frame from DefaultContent"); - } - catch (NoSuchElementException) - { - } + + // DefaultContent should not have the element in it. + Assert.That( + () => driver.FindElement(By.Id("pageNumber")), + Throws.TypeOf()); + driver.SwitchTo().Frame(1); Assert.AreEqual(driver.FindElement(By.Id("pageNumber")).Text, "2"); driver.SwitchTo().DefaultContent(); - try - { - // DefaultContent should not have the element in it. - Assert.AreEqual(driver.FindElement(By.Id("pageNumber")).Text, "1"); - Assert.Fail("Should not be able to get element in frame from DefaultContent"); - } - catch (NoSuchElementException) - { - } + + // DefaultContent should not have the element in it. + Assert.That( + () => driver.FindElement(By.Id("pageNumber")).Text, + Throws.TypeOf()); } } diff --git a/dotnet/test/common/WindowSwitchingTest.cs b/dotnet/test/common/WindowSwitchingTest.cs index b268200710f92..ab153b8377a50 100644 --- a/dotnet/test/common/WindowSwitchingTest.cs +++ b/dotnet/test/common/WindowSwitchingTest.cs @@ -60,14 +60,10 @@ public void ShouldThrowNoSuchWindowException() { driver.Url = xhtmlTestPage; String current = driver.CurrentWindowHandle; - try - { - driver.SwitchTo().Window("invalid name"); - } - catch (NoSuchWindowException) - { - // This is expected. - } + + Assert.That( + () => driver.SwitchTo().Window("invalid name"), + Throws.TypeOf()); driver.SwitchTo().Window(current); } @@ -91,12 +87,9 @@ public void ShouldThrowNoSuchWindowExceptionOnAnAttemptToGetItsHandle() try { - string currentHandle = driver.CurrentWindowHandle; - Assert.Fail("NoSuchWindowException expected"); - } - catch (NoSuchWindowException) - { - // Expected. + Assert.That( + () => driver.CurrentWindowHandle, + Throws.TypeOf()); } finally { @@ -123,25 +116,13 @@ public void ShouldThrowNoSuchWindowExceptionOnAnyOperationIfAWindowIsClosed() try { - try - { - string title = driver.Title; - Assert.Fail("NoSuchWindowException expected"); - } - catch (NoSuchWindowException) - { - // Expected. - } + Assert.That( + () => driver.Title, + Throws.TypeOf()); - try - { - driver.FindElement(By.TagName("body")); - Assert.Fail("NoSuchWindowException expected"); - } - catch (NoSuchWindowException) - { - // Expected. - } + Assert.That( + () => driver.FindElement(By.TagName("body")), + Throws.TypeOf()); } finally { @@ -169,12 +150,9 @@ public void ShouldThrowNoSuchWindowExceptionOnAnyElementOperationIfAWindowIsClos try { - string bodyText = body.Text; - Assert.Fail("NoSuchWindowException expected"); - } - catch (NoSuchWindowException) - { - // Expected. + Assert.That( + () => body.Text, + Throws.TypeOf()); } finally { @@ -280,15 +258,10 @@ public void FailingToSwitchToAWindowLeavesTheCurrentWindowAsIs() driver.Url = xhtmlTestPage; String current = driver.CurrentWindowHandle; - try - { - driver.SwitchTo().Window("i will never exist"); - Assert.Fail("Should not be ablt to change to a non-existant window"); - } - catch (NoSuchWindowException) - { - // expected - } + Assert.That( + () => driver.SwitchTo().Window("i will never exist"), + Throws.TypeOf(), + "Should not be able to change to a non-existant window"); String newHandle = driver.CurrentWindowHandle; @@ -488,9 +461,8 @@ private Func WindowWithName(string name) } catch (NoSuchWindowException) { + return false; } - - return false; }; } @@ -498,16 +470,14 @@ private Func AlertToBePresent() { return () => { - IAlert alert = null; try { - alert = driver.SwitchTo().Alert(); + return driver.SwitchTo().Alert(); } catch (NoAlertPresentException) { + return null; } - - return alert; }; } } diff --git a/dotnet/test/support/Extensions/ExecuteJavaScriptTest.cs b/dotnet/test/support/Extensions/ExecuteJavaScriptTest.cs index 88f99381f539d..15ad94527c7aa 100644 --- a/dotnet/test/support/Extensions/ExecuteJavaScriptTest.cs +++ b/dotnet/test/support/Extensions/ExecuteJavaScriptTest.cs @@ -57,7 +57,9 @@ public void ShouldConvertToIEnumerable() driver.Setup(_ => _.ExecuteScript(It.IsAny(), It.IsAny())).Returns(expected); - Assert.That(() => driver.Object.ExecuteJavaScript(JavaScript, JavaScriptParameters), Throws.Nothing); + Assert.That( + () => driver.Object.ExecuteJavaScript(JavaScript, JavaScriptParameters), + Throws.Nothing); } [Test] @@ -67,7 +69,9 @@ public void ShouldConvertToIEnumerableOfObject() driver.Setup(_ => _.ExecuteScript(It.IsAny(), It.IsAny())).Returns(expected); - Assert.That(() => driver.Object.ExecuteJavaScript>(JavaScript, JavaScriptParameters), Throws.Nothing); + Assert.That( + () => driver.Object.ExecuteJavaScript>(JavaScript, JavaScriptParameters), + Throws.Nothing); } [Test] @@ -77,7 +81,9 @@ public void ShouldNotConvertToIEnumerableOfInteger() driver.Setup(_ => _.ExecuteScript(It.IsAny(), It.IsAny())).Returns(expected); - Assert.That(() => driver.Object.ExecuteJavaScript>(JavaScript, JavaScriptParameters), Throws.InstanceOf()); + Assert.That( + () => driver.Object.ExecuteJavaScript>(JavaScript, JavaScriptParameters), + Throws.TypeOf()); } [Test] @@ -87,7 +93,9 @@ public void ShouldConvertToReadOnlyCollectionOfObject() driver.Setup(_ => _.ExecuteScript(It.IsAny(), It.IsAny())).Returns(expected); - Assert.That(() => driver.Object.ExecuteJavaScript>(JavaScript, JavaScriptParameters), Throws.Nothing); + Assert.That( + () => driver.Object.ExecuteJavaScript>(JavaScript, JavaScriptParameters), + Throws.Nothing); } [Test] @@ -97,7 +105,9 @@ public void ShouldNotConvertToSubClassOfReadOnlyCollectionOfObject() driver.Setup(_ => _.ExecuteScript(It.IsAny(), It.IsAny())).Returns(expected); - Assert.That(() => driver.Object.ExecuteJavaScript(JavaScript, JavaScriptParameters), Throws.InstanceOf()); + Assert.That( + () => driver.Object.ExecuteJavaScript(JavaScript, JavaScriptParameters), + Throws.TypeOf()); } [Test] @@ -105,7 +115,9 @@ public void ShouldNotThrowWhenNullIsReturned() { driver.Setup(_ => _.ExecuteScript(It.IsAny(), It.IsAny())).Returns(null); - Assert.That(() => driver.Object.ExecuteJavaScript(JavaScript, JavaScriptParameters), Throws.Nothing); + Assert.That( + () => driver.Object.ExecuteJavaScript(JavaScript, JavaScriptParameters), + Throws.Nothing); } [Test] @@ -113,7 +125,9 @@ public void ShouldNotThrowWhenNullIsReturnedForNullableValueType() { driver.Setup(_ => _.ExecuteScript(It.IsAny(), It.IsAny())).Returns(null); - Assert.That(() => driver.Object.ExecuteJavaScript(JavaScript, JavaScriptParameters), Throws.Nothing); + Assert.That( + () => driver.Object.ExecuteJavaScript(JavaScript, JavaScriptParameters), + Throws.Nothing); } [Test] @@ -121,7 +135,9 @@ public void ShouldThrowWhenNullIsReturnedForValueType() { driver.Setup(_ => _.ExecuteScript(It.IsAny(), It.IsAny())).Returns(null); - Assert.That(() => driver.Object.ExecuteJavaScript(JavaScript, JavaScriptParameters), Throws.InstanceOf()); + Assert.That( + () => driver.Object.ExecuteJavaScript(JavaScript, JavaScriptParameters), + Throws.TypeOf()); } [Test] @@ -129,7 +145,9 @@ public void ShouldAllowExecutingJavaScriptWithoutReturningResult() { driver.Setup(_ => _.ExecuteScript(It.IsAny(), It.IsAny())).Returns(null); - Assert.That(() => driver.Object.ExecuteJavaScript(JavaScript, JavaScriptParameters), Throws.Nothing); + Assert.That( + () => driver.Object.ExecuteJavaScript(JavaScript, JavaScriptParameters), + Throws.Nothing); } } }