From b1260d330baeb01153d90cc0207fc075c8683cc9 Mon Sep 17 00:00:00 2001 From: Jake Ferm Date: Sun, 1 Nov 2020 17:01:06 -0600 Subject: [PATCH 01/11] Implemented SoftAssert.Assert functions. TODO need to implement Unit Tests --- Framework/BaseAppiumTest/AppiumSoftAssert.cs | 26 +++++++++++++++++++ .../BaseSeleniumTest/SeleniumSoftAssert.cs | 25 ++++++++++++++++++ Framework/BaseTest/SoftAssert.cs | 2 +- 3 files changed, 52 insertions(+), 1 deletion(-) diff --git a/Framework/BaseAppiumTest/AppiumSoftAssert.cs b/Framework/BaseAppiumTest/AppiumSoftAssert.cs index 18fddc812..1db3324f6 100644 --- a/Framework/BaseAppiumTest/AppiumSoftAssert.cs +++ b/Framework/BaseAppiumTest/AppiumSoftAssert.cs @@ -65,6 +65,32 @@ public override bool AreEqual(string expectedText, string actualText, string sof return true; } + /// + /// Soft assert method to check if the Action is false + /// + /// Function to use + /// Boolean of the assert + public override bool Assert(Action assertFunction) + { + bool didPass = base.Assert(assertFunction); + + if (!didPass && this.appiumTestObject.GetDriverManager().IsDriverIntialized()) + { + if (AppiumConfig.GetSoftAssertScreenshot()) + { + AppiumUtilities.CaptureScreenshot(this.appiumTestObject.AppiumDriver, this.appiumTestObject); + } + + if (AppiumConfig.GetSavePagesourceOnFail()) + { + AppiumUtilities.SavePageSource(this.appiumTestObject.AppiumDriver, this.appiumTestObject, StringProcessor.SafeFormatter(" ({0})", this.NumberOfAsserts)); + } + + return false; + } + return didPass; + } + /// /// Method to determine the text to be appended to the screenshot file names /// diff --git a/Framework/BaseSeleniumTest/SeleniumSoftAssert.cs b/Framework/BaseSeleniumTest/SeleniumSoftAssert.cs index 648c525a8..ed2dccf9e 100644 --- a/Framework/BaseSeleniumTest/SeleniumSoftAssert.cs +++ b/Framework/BaseSeleniumTest/SeleniumSoftAssert.cs @@ -146,6 +146,31 @@ public override bool IsTrue(bool condition, string softAssertName, string failur return true; } + /// + /// Soft assert method to check if the Action is false + /// + /// Function to use + /// Boolean of the assert + public override bool Assert(Action assertFunction) + { + bool didPass = base.Assert(assertFunction); + if (!didPass && this.testObject.GetDriverManager().IsDriverIntialized()) + { + if (SeleniumConfig.GetSoftAssertScreenshot()) + { + SeleniumUtilities.CaptureScreenshot(this.testObject.WebDriver, this.testObject); + } + + if (SeleniumConfig.GetSavePagesourceOnFail()) + { + SeleniumUtilities.SavePageSource(this.testObject.WebDriver, this.testObject, StringProcessor.SafeFormatter(" ({0})", this.NumberOfAsserts)); + } + + return false; + } + return didPass; + } + /// /// Method to determine the text to be appended to the screenshot file names /// diff --git a/Framework/BaseTest/SoftAssert.cs b/Framework/BaseTest/SoftAssert.cs index b3a0fc8bb..a88f904e5 100644 --- a/Framework/BaseTest/SoftAssert.cs +++ b/Framework/BaseTest/SoftAssert.cs @@ -258,7 +258,7 @@ public void FailTestIfAssertFailed(string message) /// /// The assert function /// True if the asset passed - public bool Assert(Action assertFunction) + public virtual bool Assert(Action assertFunction) { // Resetting every time we invoke a test to verify the user checked for failures this.DidUserCheckForFailures = false; From 91a2a3ae4f1d07c2edb6e1aa9a4141710455fc2b Mon Sep 17 00:00:00 2001 From: Jake Ferm Date: Wed, 9 Dec 2020 22:58:03 -0600 Subject: [PATCH 02/11] Merging from master with changes from Expected Soft Asserts. Added unit tests --- Framework/BaseAppiumTest/AppiumSoftAssert.cs | 5 ++- .../BaseSeleniumTest/SeleniumSoftAssert.cs | 5 ++- Framework/BaseTest/SoftAssert.cs | 2 +- .../SeleniumUnitTests/SeleniumUnitTest.cs | 44 +++++++++++++++++++ 4 files changed, 51 insertions(+), 5 deletions(-) diff --git a/Framework/BaseAppiumTest/AppiumSoftAssert.cs b/Framework/BaseAppiumTest/AppiumSoftAssert.cs index 24ca889e8..62001460b 100644 --- a/Framework/BaseAppiumTest/AppiumSoftAssert.cs +++ b/Framework/BaseAppiumTest/AppiumSoftAssert.cs @@ -69,10 +69,11 @@ public override bool AreEqual(string expectedText, string actualText, string sof /// Soft assert method to check if the Action is false /// /// Function to use + /// Key of expected assert being called. /// Boolean of the assert - public override bool Assert(Action assertFunction) + public override bool Assert(Action assertFunction, string assertCalledKey = null) { - bool didPass = base.Assert(assertFunction); + bool didPass = base.Assert(assertFunction, assertCalledKey); if (!didPass && this.appiumTestObject.GetDriverManager().IsDriverIntialized()) { diff --git a/Framework/BaseSeleniumTest/SeleniumSoftAssert.cs b/Framework/BaseSeleniumTest/SeleniumSoftAssert.cs index ed2dccf9e..bdbaa1c5b 100644 --- a/Framework/BaseSeleniumTest/SeleniumSoftAssert.cs +++ b/Framework/BaseSeleniumTest/SeleniumSoftAssert.cs @@ -150,10 +150,11 @@ public override bool IsTrue(bool condition, string softAssertName, string failur /// Soft assert method to check if the Action is false /// /// Function to use + /// Key of expected assert being called. /// Boolean of the assert - public override bool Assert(Action assertFunction) + public override bool Assert(Action assertFunction, string assertCalledKey = null) { - bool didPass = base.Assert(assertFunction); + bool didPass = base.Assert(assertFunction, assertCalledKey); if (!didPass && this.testObject.GetDriverManager().IsDriverIntialized()) { if (SeleniumConfig.GetSoftAssertScreenshot()) diff --git a/Framework/BaseTest/SoftAssert.cs b/Framework/BaseTest/SoftAssert.cs index db366e949..0afa646cc 100644 --- a/Framework/BaseTest/SoftAssert.cs +++ b/Framework/BaseTest/SoftAssert.cs @@ -288,7 +288,7 @@ internal void CheckForExpectedAsserts() /// The assert function /// Key of expected assert being called. /// True if the asset passed - public bool Assert(Action assertFunction, string assertCalledKey = null) + public virtual bool Assert(Action assertFunction, string assertCalledKey = null) { if(!string.IsNullOrEmpty(assertCalledKey) && _expectedAsserts.Any()) { diff --git a/Framework/SeleniumUnitTests/SeleniumUnitTest.cs b/Framework/SeleniumUnitTests/SeleniumUnitTest.cs index d4d9cb44f..b2d6c28ed 100644 --- a/Framework/SeleniumUnitTests/SeleniumUnitTest.cs +++ b/Framework/SeleniumUnitTests/SeleniumUnitTest.cs @@ -707,6 +707,28 @@ public void SeleniumSoftAssertIsTrueFalseCondition() Assert.IsFalse(isFalse); } + /// + /// Verify that a screenshot is taken if the SeleniumSoftAssert.IsTrue gets a false condition and the logger is set to log screenshots + /// + [TestMethod] + [TestCategory(TestCategories.Selenium)] + public void SeleniumSoftAssertWithAssertIsTrueFalseCondition() + { + this.WebDriver.Navigate().GoToUrl(TestSiteAutomationUrl); + this.Log = new FileLogger(string.Empty, "SeleniumSoftAssertIsTrueFalseCondition.txt", MessageType.GENERIC, true); + SeleniumSoftAssert seleniumSoftAssert = new SeleniumSoftAssert(TestObject); + string logLocation = ((FileLogger)Log).FilePath; + string screenShotLocation = logLocation.Substring(0, logLocation.LastIndexOf('.')) + " testSoftAssert" + " (1).Png"; + + bool isFalse = seleniumSoftAssert.Assert(() => Assert.IsTrue(false, "testSoftAssert", "message")); + + Assert.IsTrue(File.Exists(screenShotLocation), "Fail to find screenshot"); + File.Delete(screenShotLocation); + File.Delete(logLocation); + + Assert.IsFalse(isFalse); + } + /// /// Verify that page source is saved if the SeleniumSoftAssert.IsTrue gets a false condition and the logger is set to save Page Source /// @@ -729,6 +751,28 @@ public void SeleniumSoftAssertIsTrueFalseConditionPageSource() Assert.IsFalse(isFalse); } + /// + /// Verify that page source is saved if the SeleniumSoftAssert.IsTrue gets a false condition and the logger is set to save Page Source + /// + [TestMethod] + [TestCategory(TestCategories.Selenium)] + public void SeleniumSoftAssertWithAssertIsTrueFalseConditionPageSource() + { + this.WebDriver.Navigate().GoToUrl(TestSiteAutomationUrl); + this.Log = new FileLogger(string.Empty, "SeleniumSoftAssertIsTrueFalseConditionPageSource.txt", MessageType.GENERIC, true); + SeleniumSoftAssert seleniumSoftAssert = new SeleniumSoftAssert(TestObject); + string logLocation = ((FileLogger)Log).FilePath; + string pageSourceLocation = logLocation.Substring(0, logLocation.LastIndexOf('.')) + "_PS (1).txt"; + + bool isFalse = seleniumSoftAssert.Assert(() => Assert.IsTrue(false, "testSoftAssert", "message")); + + Assert.IsTrue(File.Exists(pageSourceLocation), "Fail to find page source"); + File.Delete(pageSourceLocation); + File.Delete(logLocation); + + Assert.IsFalse(isFalse); + } + /// /// Verify that a screenshot is taken if the SeleniumSoftAssert.IsFalse gets a true condition and the logger is set to log screenshots /// From ecf6e1fee392c3e6fdc9c4064fe38064fce7acec Mon Sep 17 00:00:00 2001 From: Jake Ferm Date: Wed, 9 Dec 2020 23:06:58 -0600 Subject: [PATCH 03/11] Fixing failing unit test --- Framework/SeleniumUnitTests/SeleniumUnitTest.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Framework/SeleniumUnitTests/SeleniumUnitTest.cs b/Framework/SeleniumUnitTests/SeleniumUnitTest.cs index b2d6c28ed..477cbbfd4 100644 --- a/Framework/SeleniumUnitTests/SeleniumUnitTest.cs +++ b/Framework/SeleniumUnitTests/SeleniumUnitTest.cs @@ -715,7 +715,7 @@ public void SeleniumSoftAssertIsTrueFalseCondition() public void SeleniumSoftAssertWithAssertIsTrueFalseCondition() { this.WebDriver.Navigate().GoToUrl(TestSiteAutomationUrl); - this.Log = new FileLogger(string.Empty, "SeleniumSoftAssertIsTrueFalseCondition.txt", MessageType.GENERIC, true); + this.Log = new FileLogger(string.Empty, "SeleniumSoftAssertWithAssertIsTrueFalseCondition.txt", MessageType.GENERIC, true); SeleniumSoftAssert seleniumSoftAssert = new SeleniumSoftAssert(TestObject); string logLocation = ((FileLogger)Log).FilePath; string screenShotLocation = logLocation.Substring(0, logLocation.LastIndexOf('.')) + " testSoftAssert" + " (1).Png"; From 5d6819e5bcf4724f244e1aba3d0de048b440400c Mon Sep 17 00:00:00 2001 From: Jake Ferm Date: Sat, 2 Jan 2021 23:29:22 -0600 Subject: [PATCH 04/11] Adding Failure message to .Assert --- Framework/BaseAppiumTest/AppiumSoftAssert.cs | 5 +++-- Framework/BaseSeleniumTest/SeleniumSoftAssert.cs | 5 +++-- Framework/BaseTest/SoftAssert.cs | 12 ++++++++++-- 3 files changed, 16 insertions(+), 6 deletions(-) diff --git a/Framework/BaseAppiumTest/AppiumSoftAssert.cs b/Framework/BaseAppiumTest/AppiumSoftAssert.cs index 62001460b..72d300395 100644 --- a/Framework/BaseAppiumTest/AppiumSoftAssert.cs +++ b/Framework/BaseAppiumTest/AppiumSoftAssert.cs @@ -69,11 +69,12 @@ public override bool AreEqual(string expectedText, string actualText, string sof /// Soft assert method to check if the Action is false /// /// Function to use + /// Message to log /// Key of expected assert being called. /// Boolean of the assert - public override bool Assert(Action assertFunction, string assertCalledKey = null) + public override bool Assert(Action assertFunction, string failureMessage = "", string assertCalledKey = null) { - bool didPass = base.Assert(assertFunction, assertCalledKey); + bool didPass = base.Assert(assertFunction, failureMessage, assertCalledKey); if (!didPass && this.appiumTestObject.GetDriverManager().IsDriverIntialized()) { diff --git a/Framework/BaseSeleniumTest/SeleniumSoftAssert.cs b/Framework/BaseSeleniumTest/SeleniumSoftAssert.cs index bdbaa1c5b..5459335ab 100644 --- a/Framework/BaseSeleniumTest/SeleniumSoftAssert.cs +++ b/Framework/BaseSeleniumTest/SeleniumSoftAssert.cs @@ -150,11 +150,12 @@ public override bool IsTrue(bool condition, string softAssertName, string failur /// Soft assert method to check if the Action is false /// /// Function to use + /// Message to log /// Key of expected assert being called. /// Boolean of the assert - public override bool Assert(Action assertFunction, string assertCalledKey = null) + public override bool Assert(Action assertFunction,string failureMessage = "", string assertCalledKey = null) { - bool didPass = base.Assert(assertFunction, assertCalledKey); + bool didPass = base.Assert(assertFunction, failureMessage, assertCalledKey); if (!didPass && this.testObject.GetDriverManager().IsDriverIntialized()) { if (SeleniumConfig.GetSoftAssertScreenshot()) diff --git a/Framework/BaseTest/SoftAssert.cs b/Framework/BaseTest/SoftAssert.cs index 0afa646cc..4a03ec71a 100644 --- a/Framework/BaseTest/SoftAssert.cs +++ b/Framework/BaseTest/SoftAssert.cs @@ -286,9 +286,10 @@ internal void CheckForExpectedAsserts() /// Wrap an assert inside a soft assert /// /// The assert function + /// Message to log /// Key of expected assert being called. /// True if the asset passed - public virtual bool Assert(Action assertFunction, string assertCalledKey = null) + public virtual bool Assert(Action assertFunction, string failureMessage = "", string assertCalledKey = null) { if(!string.IsNullOrEmpty(assertCalledKey) && _expectedAsserts.Any()) { @@ -310,7 +311,14 @@ public virtual bool Assert(Action assertFunction, string assertCalledKey = null) { this.NumberOfFailedAsserts = ++this.NumberOfFailedAsserts; result = false; - this.Log.LogMessage(MessageType.WARNING, "SoftAssert failed for: {0}. {1}", assertFunction.Method.Name, ex.Message); + if (string.IsNullOrEmpty(failureMessage)) + { + this.Log.LogMessage(MessageType.WARNING, $"SoftAssert failed for: {assertFunction.Method.Name}. {ex.Message}"); + } + else + { + this.Log.LogMessage(MessageType.WARNING, $"SoftAssert failed for: {assertFunction.Method.Name}. {failureMessage}. {ex.Message}"); + } this.listOfExceptions.Add(ex.Message); } finally From 282a3ab4afcd443feb2ee88723a2294960689247 Mon Sep 17 00:00:00 2001 From: Jake Ferm Date: Sat, 9 Jan 2021 12:42:10 -0600 Subject: [PATCH 05/11] Implementing SoftAssert increment on FileNames to fix failed tests --- Framework/BaseAppiumTest/AppiumSoftAssert.cs | 2 +- Framework/BaseSeleniumTest/SeleniumSoftAssert.cs | 2 +- Framework/SeleniumUnitTests/SeleniumUnitTest.cs | 2 +- Framework/UtilitiesUnitTests/SoftAssertUnitTests.cs | 12 ++++++------ 4 files changed, 9 insertions(+), 9 deletions(-) diff --git a/Framework/BaseAppiumTest/AppiumSoftAssert.cs b/Framework/BaseAppiumTest/AppiumSoftAssert.cs index 72d300395..8a3e15fc2 100644 --- a/Framework/BaseAppiumTest/AppiumSoftAssert.cs +++ b/Framework/BaseAppiumTest/AppiumSoftAssert.cs @@ -80,7 +80,7 @@ public override bool Assert(Action assertFunction, string failureMessage = "", s { if (AppiumConfig.GetSoftAssertScreenshot()) { - AppiumUtilities.CaptureScreenshot(this.appiumTestObject.AppiumDriver, this.appiumTestObject); + AppiumUtilities.CaptureScreenshot(this.appiumTestObject.AppiumDriver, this.appiumTestObject, this.TextToAppend(assertCalledKey)); } if (AppiumConfig.GetSavePagesourceOnFail()) diff --git a/Framework/BaseSeleniumTest/SeleniumSoftAssert.cs b/Framework/BaseSeleniumTest/SeleniumSoftAssert.cs index 5459335ab..396431368 100644 --- a/Framework/BaseSeleniumTest/SeleniumSoftAssert.cs +++ b/Framework/BaseSeleniumTest/SeleniumSoftAssert.cs @@ -160,7 +160,7 @@ public override bool Assert(Action assertFunction,string failureMessage = "", st { if (SeleniumConfig.GetSoftAssertScreenshot()) { - SeleniumUtilities.CaptureScreenshot(this.testObject.WebDriver, this.testObject); + SeleniumUtilities.CaptureScreenshot(this.testObject.WebDriver, this.testObject, this.TextToAppend(assertCalledKey)); } if (SeleniumConfig.GetSavePagesourceOnFail()) diff --git a/Framework/SeleniumUnitTests/SeleniumUnitTest.cs b/Framework/SeleniumUnitTests/SeleniumUnitTest.cs index 477cbbfd4..c82efb233 100644 --- a/Framework/SeleniumUnitTests/SeleniumUnitTest.cs +++ b/Framework/SeleniumUnitTests/SeleniumUnitTest.cs @@ -718,7 +718,7 @@ public void SeleniumSoftAssertWithAssertIsTrueFalseCondition() this.Log = new FileLogger(string.Empty, "SeleniumSoftAssertWithAssertIsTrueFalseCondition.txt", MessageType.GENERIC, true); SeleniumSoftAssert seleniumSoftAssert = new SeleniumSoftAssert(TestObject); string logLocation = ((FileLogger)Log).FilePath; - string screenShotLocation = logLocation.Substring(0, logLocation.LastIndexOf('.')) + " testSoftAssert" + " (1).Png"; + string screenShotLocation = logLocation.Substring(0, logLocation.LastIndexOf('.')) + " (1).Png"; bool isFalse = seleniumSoftAssert.Assert(() => Assert.IsTrue(false, "testSoftAssert", "message")); diff --git a/Framework/UtilitiesUnitTests/SoftAssertUnitTests.cs b/Framework/UtilitiesUnitTests/SoftAssertUnitTests.cs index e4a704a93..d785473f5 100644 --- a/Framework/UtilitiesUnitTests/SoftAssertUnitTests.cs +++ b/Framework/UtilitiesUnitTests/SoftAssertUnitTests.cs @@ -32,13 +32,13 @@ public void SoftAssertFailsValidTest() int one = 1; softAssert.AssertFails( - () => + () => { one = 0; var result = 9 / one; Assert.Fail($"Result should have thrown an error but is {result} instead"); - }, - typeof(DivideByZeroException), + }, + typeof(DivideByZeroException), "Assert action throws divide by zero exception", "Failed to assert that we couldn't divide by zero"); @@ -64,8 +64,8 @@ public void SoftAssertFailsInvalidTest() var result = 9 / one; Assert.Fail($"Result should have thrown an error but is {result} instead"); }, - typeof(NullReferenceException), - "Assert dividing by zero throws a null reference", + typeof(NullReferenceException), + "Assert dividing by zero throws a null reference", "Failed to assert that we couldn't divide by zero"); softAssert.FailTestIfAssertFailed(); @@ -244,7 +244,7 @@ public void SoftAssertManuallySetExpectedAsserts() new FileLogger(LoggingConfig.GetLogDirectory(), "UnitTests.SoftAssertManuallySetExpectedAssert")); softAssert.AddExpectedAsserts("one"); - softAssert.Assert(() => { }, "one"); + softAssert.Assert(() => { }, "AssertionMethod", "one"); softAssert.FailTestIfAssertFailed(); } From a7c5c64d3ceaf9da248e7aab6b5153c18a616a34 Mon Sep 17 00:00:00 2001 From: Jake Ferm Date: Tue, 12 Jan 2021 01:29:23 -0600 Subject: [PATCH 06/11] Removing Unused using statement --- Framework/BaseTestUnitTests/BaseFrameworkTests.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/Framework/BaseTestUnitTests/BaseFrameworkTests.cs b/Framework/BaseTestUnitTests/BaseFrameworkTests.cs index 164699448..52e2277b8 100644 --- a/Framework/BaseTestUnitTests/BaseFrameworkTests.cs +++ b/Framework/BaseTestUnitTests/BaseFrameworkTests.cs @@ -12,7 +12,6 @@ using System; using System.Diagnostics.CodeAnalysis; using System.IO; -using System.Linq; using MicroAssert = Microsoft.VisualStudio.TestTools.UnitTesting.Assert; namespace BaseTestUnitTests From 008330eb8c78104f81295cb9fbc71633d047e88e Mon Sep 17 00:00:00 2001 From: Jake Ferm Date: Tue, 12 Jan 2021 01:36:15 -0600 Subject: [PATCH 07/11] Updating with Unit Test --- .../UtilitiesUnitTests/SoftAssertUnitTests.cs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/Framework/UtilitiesUnitTests/SoftAssertUnitTests.cs b/Framework/UtilitiesUnitTests/SoftAssertUnitTests.cs index d785473f5..46e785c2b 100644 --- a/Framework/UtilitiesUnitTests/SoftAssertUnitTests.cs +++ b/Framework/UtilitiesUnitTests/SoftAssertUnitTests.cs @@ -263,6 +263,20 @@ public void SoftAssertManuallySetExpectedAssertsFails() softAssert.FailTestIfAssertFailed(); } + /// + /// Test to cover the soft assert .Assert with failure message + /// + [TestMethod] + [TestCategory(TestCategories.Utilities)] + [ExpectedException(typeof(AggregateException))] + public void SoftAssertAssertMethodWithFailureMessage() + { + SoftAssert softAssert = new SoftAssert(new FileLogger(LoggingConfig.GetLogDirectory(), + "UnitTests.SoftAssertAssertMethodWithFailureMessage")); + softAssert.Assert(() => Assert.Fail(), "Failure Message"); + softAssert.FailTestIfAssertFailed(); + } + /// /// Throws a null reference exception /// From 1f4b4e00057a69664bef9d06939ce349548e29f6 Mon Sep 17 00:00:00 2001 From: Jake Ferm Date: Wed, 13 Jan 2021 20:22:31 -0600 Subject: [PATCH 08/11] Updating SoftAsserts based on PR comments. Will fix SonarCloud issues on next commit --- Framework/BaseAppiumTest/AppiumSoftAssert.cs | 8 ++-- .../BaseSeleniumTest/SeleniumSoftAssert.cs | 8 ++-- Framework/BaseTest/SoftAssert.cs | 48 ++++++++++++++----- .../DatabaseSQLiteUnitTestsWithDriver.cs | 2 +- 4 files changed, 44 insertions(+), 22 deletions(-) diff --git a/Framework/BaseAppiumTest/AppiumSoftAssert.cs b/Framework/BaseAppiumTest/AppiumSoftAssert.cs index 8a3e15fc2..eeaf56d8b 100644 --- a/Framework/BaseAppiumTest/AppiumSoftAssert.cs +++ b/Framework/BaseAppiumTest/AppiumSoftAssert.cs @@ -70,17 +70,17 @@ public override bool AreEqual(string expectedText, string actualText, string sof /// /// Function to use /// Message to log - /// Key of expected assert being called. + /// Soft assert name or name of expected assert being called. /// Boolean of the assert - public override bool Assert(Action assertFunction, string failureMessage = "", string assertCalledKey = null) + public override bool Assert(Action assertFunction, string failureMessage = "", string assertName = null) { - bool didPass = base.Assert(assertFunction, failureMessage, assertCalledKey); + bool didPass = base.Assert(assertFunction, failureMessage, assertName); if (!didPass && this.appiumTestObject.GetDriverManager().IsDriverIntialized()) { if (AppiumConfig.GetSoftAssertScreenshot()) { - AppiumUtilities.CaptureScreenshot(this.appiumTestObject.AppiumDriver, this.appiumTestObject, this.TextToAppend(assertCalledKey)); + AppiumUtilities.CaptureScreenshot(this.appiumTestObject.AppiumDriver, this.appiumTestObject, this.TextToAppend(assertName)); } if (AppiumConfig.GetSavePagesourceOnFail()) diff --git a/Framework/BaseSeleniumTest/SeleniumSoftAssert.cs b/Framework/BaseSeleniumTest/SeleniumSoftAssert.cs index 396431368..a63ebdc21 100644 --- a/Framework/BaseSeleniumTest/SeleniumSoftAssert.cs +++ b/Framework/BaseSeleniumTest/SeleniumSoftAssert.cs @@ -151,16 +151,16 @@ public override bool IsTrue(bool condition, string softAssertName, string failur /// /// Function to use /// Message to log - /// Key of expected assert being called. + /// Soft assert name or name of expected assert being called. /// Boolean of the assert - public override bool Assert(Action assertFunction,string failureMessage = "", string assertCalledKey = null) + public override bool Assert(Action assertFunction,string failureMessage = "", string assertName = null) { - bool didPass = base.Assert(assertFunction, failureMessage, assertCalledKey); + bool didPass = base.Assert(assertFunction, failureMessage, assertName); if (!didPass && this.testObject.GetDriverManager().IsDriverIntialized()) { if (SeleniumConfig.GetSoftAssertScreenshot()) { - SeleniumUtilities.CaptureScreenshot(this.testObject.WebDriver, this.testObject, this.TextToAppend(assertCalledKey)); + SeleniumUtilities.CaptureScreenshot(this.testObject.WebDriver, this.testObject, this.TextToAppend(assertName)); } if (SeleniumConfig.GetSavePagesourceOnFail()) diff --git a/Framework/BaseTest/SoftAssert.cs b/Framework/BaseTest/SoftAssert.cs index 61e7bfaab..201ab6430 100644 --- a/Framework/BaseTest/SoftAssert.cs +++ b/Framework/BaseTest/SoftAssert.cs @@ -28,12 +28,12 @@ public class SoftAssert /// /// Keys of the asserts that need to be called with soft assert. /// - private readonly HashSet _expectedAsserts = new HashSet(); + private readonly HashSet _expectedAssertNames = new HashSet(); /// /// Keys of the asserts that have been called with soft assert. /// - private readonly HashSet _calledAsserts = new HashSet(); + private readonly HashSet _calledAssertNames = new HashSet(); /// /// Initializes a new instance of the SoftAssert class. @@ -273,9 +273,9 @@ public void FailTestIfAssertFailed(string message) internal void CheckForExpectedAsserts() { - foreach(var expectedAssert in _expectedAsserts) + foreach(var expectedAssert in _expectedAssertNames) { - if(!_calledAsserts.Contains(expectedAssert)) + if(!_calledAssertNames.Contains(expectedAssert)) { this.NumberOfAsserts++; this.NumberOfFailedAsserts++; @@ -289,13 +289,13 @@ internal void CheckForExpectedAsserts() /// /// The assert function /// Message to log - /// Key of expected assert being called. + /// Soft assert name or name of expected assert being called. /// True if the asset passed - public virtual bool Assert(Action assertFunction, string failureMessage = "", string assertCalledKey = null) + public virtual bool Assert(Action assertFunction, string failureMessage = "", string assertName = null) { - if(!string.IsNullOrEmpty(assertCalledKey) && _expectedAsserts.Any()) + if(!string.IsNullOrEmpty(assertName) && _expectedAssertNames.Any()) { - _calledAsserts.Add(assertCalledKey); + _calledAssertNames.Add(assertName); } // Resetting every time we invoke a test to verify the user checked for failures @@ -307,20 +307,42 @@ public virtual bool Assert(Action assertFunction, string failureMessage = "", st assertFunction.Invoke(); this.NumberOfPassedAsserts = ++this.NumberOfPassedAsserts; result = true; - this.Log.LogMessage(MessageType.SUCCESS, "SoftAssert passed for: {0}.", assertFunction.Method.Name); + if (!string.IsNullOrEmpty(assertName)) + { + this.Log.LogMessage(MessageType.SUCCESS, "SoftAssert passed for: {0}.", assertName); + } + else + { + this.Log.LogMessage(MessageType.SUCCESS, "SoftAssert passed for: {0}.", assertFunction.Method.Name); + } } catch (Exception ex) { this.NumberOfFailedAsserts = ++this.NumberOfFailedAsserts; result = false; - if (string.IsNullOrEmpty(failureMessage)) + if (!string.IsNullOrEmpty(assertName)) { - this.Log.LogMessage(MessageType.WARNING, $"SoftAssert failed for: {assertFunction.Method.Name}. {ex.Message}"); + if (string.IsNullOrEmpty(failureMessage)) + { + this.Log.LogMessage(MessageType.WARNING, $"SoftAssert failed for: {assertName}. {ex.Message}"); + } + else + { + this.Log.LogMessage(MessageType.WARNING, $"SoftAssert failed for: {assertName}. {failureMessage}. {ex.Message}"); + } } else { - this.Log.LogMessage(MessageType.WARNING, $"SoftAssert failed for: {assertFunction.Method.Name}. {failureMessage}. {ex.Message}"); + if (string.IsNullOrEmpty(failureMessage)) + { + this.Log.LogMessage(MessageType.WARNING, $"SoftAssert failed for: {assertFunction.Method.Name}. {ex.Message}"); + } + else + { + this.Log.LogMessage(MessageType.WARNING, $"SoftAssert failed for: {assertFunction.Method.Name}. {failureMessage}. {ex.Message}"); + } } + this.listOfExceptions.Add(ex); } finally @@ -475,7 +497,7 @@ public void AddExpectedAsserts(params string[] expectedAsserts) { foreach (var expectedAssert in expectedAsserts) { - _expectedAsserts.Add(expectedAssert); + _expectedAssertNames.Add(expectedAssert); } } diff --git a/Framework/DatabaseUnitTests/DatabaseSQLiteUnitTestsWithDriver.cs b/Framework/DatabaseUnitTests/DatabaseSQLiteUnitTestsWithDriver.cs index 370611f46..32632e7dc 100644 --- a/Framework/DatabaseUnitTests/DatabaseSQLiteUnitTestsWithDriver.cs +++ b/Framework/DatabaseUnitTests/DatabaseSQLiteUnitTestsWithDriver.cs @@ -246,7 +246,7 @@ INNER JOIN products p [ExpectedException(typeof(SqliteException))] public void CustomQueryException() { - var result = this.DatabaseDriver.Query((dbConnection) => + this.DatabaseDriver.Query((dbConnection) => { return dbConnection.Query("SELECT * FROM unknowntable"); }); From 15d09b2ae69b460ab5812d55e56f01dacb48f84b Mon Sep 17 00:00:00 2001 From: Jake Ferm Date: Wed, 13 Jan 2021 23:07:13 -0600 Subject: [PATCH 09/11] Moving assertName to a required parameter to follow conventions from IsTrue and IsFalse --- .../AppiumUnitTests/AppiumUtilitiesTests.cs | 8 +-- Framework/BaseAppiumTest/AppiumSoftAssert.cs | 4 +- .../BaseSeleniumTest/SeleniumSoftAssert.cs | 4 +- Framework/BaseTest/SoftAssert.cs | 49 +++++++------------ .../BaseTestUnitTests/BaseFrameworkTests.cs | 4 +- Framework/CompositeUnitTests/Base.cs | 10 ++-- .../SeleniumUnitTests/LazyElementUnitTests.cs | 2 +- .../SeleniumUnitTests/SeleniumUnitTest.cs | 4 +- .../UtilitiesUnitTests/SoftAssertUnitTests.cs | 8 +-- 9 files changed, 41 insertions(+), 52 deletions(-) diff --git a/Framework/AppiumUnitTests/AppiumUtilitiesTests.cs b/Framework/AppiumUnitTests/AppiumUtilitiesTests.cs index 04dfd4315..f07250ee8 100644 --- a/Framework/AppiumUnitTests/AppiumUtilitiesTests.cs +++ b/Framework/AppiumUnitTests/AppiumUtilitiesTests.cs @@ -173,8 +173,8 @@ public void AppiumLazyParentTest() LazyMobileElement missingChild = new LazyMobileElement(this.TestObject, By.XPath("//Missing"), "Missing"); this.SoftAssert.AreEqual(child.Text, parent.FindElement(child.By, "Child").Text); - this.SoftAssert.Assert(() => Assert.AreEqual(1, parent.FindElements(child.By, "Child").Count)); - this.SoftAssert.Assert(() => Assert.IsTrue(child.Exists, "Expect exists now")); + this.SoftAssert.Assert(() => Assert.AreEqual(1, parent.FindElements(child.By, "Child").Count), "Name1"); + this.SoftAssert.Assert(() => Assert.IsTrue(child.Exists, "Expect exists now"), "Name2"); // Override the timeout this.AppiumDriver.SetWaitDriver(new WebDriverWait(this.AppiumDriver, TimeSpan.FromSeconds(10))); @@ -196,8 +196,8 @@ public void AppiumLazyWaitOverride() this.AppiumDriver.SetWaitDriver(new WebDriverWait(this.AppiumDriver, overrideTimeSpan)); - this.SoftAssert.Assert(() => Assert.AreEqual(overrideTimeSpan, parent.WaitDriver().Timeout, "Parent wait override was not respected")); - this.SoftAssert.Assert(() => Assert.AreEqual(overrideTimeSpan, child.WaitDriver().Timeout, "Child wait override was not respected")); + this.SoftAssert.Assert(() => Assert.AreEqual(overrideTimeSpan, parent.WaitDriver().Timeout, "Parent wait override was not respected"), "Name1"); + this.SoftAssert.Assert(() => Assert.AreEqual(overrideTimeSpan, child.WaitDriver().Timeout, "Child wait override was not respected"), "Name2"); this.SoftAssert.FailTestIfAssertFailed(); } diff --git a/Framework/BaseAppiumTest/AppiumSoftAssert.cs b/Framework/BaseAppiumTest/AppiumSoftAssert.cs index eeaf56d8b..a470b5628 100644 --- a/Framework/BaseAppiumTest/AppiumSoftAssert.cs +++ b/Framework/BaseAppiumTest/AppiumSoftAssert.cs @@ -72,9 +72,9 @@ public override bool AreEqual(string expectedText, string actualText, string sof /// Message to log /// Soft assert name or name of expected assert being called. /// Boolean of the assert - public override bool Assert(Action assertFunction, string failureMessage = "", string assertName = null) + public override bool Assert(Action assertFunction, string assertName, string failureMessage = "") { - bool didPass = base.Assert(assertFunction, failureMessage, assertName); + bool didPass = base.Assert(assertFunction, assertName, failureMessage); if (!didPass && this.appiumTestObject.GetDriverManager().IsDriverIntialized()) { diff --git a/Framework/BaseSeleniumTest/SeleniumSoftAssert.cs b/Framework/BaseSeleniumTest/SeleniumSoftAssert.cs index a63ebdc21..4589a7c00 100644 --- a/Framework/BaseSeleniumTest/SeleniumSoftAssert.cs +++ b/Framework/BaseSeleniumTest/SeleniumSoftAssert.cs @@ -153,9 +153,9 @@ public override bool IsTrue(bool condition, string softAssertName, string failur /// Message to log /// Soft assert name or name of expected assert being called. /// Boolean of the assert - public override bool Assert(Action assertFunction,string failureMessage = "", string assertName = null) + public override bool Assert(Action assertFunction, string assertName, string failureMessage = "") { - bool didPass = base.Assert(assertFunction, failureMessage, assertName); + bool didPass = base.Assert(assertFunction, assertName, failureMessage); if (!didPass && this.testObject.GetDriverManager().IsDriverIntialized()) { if (SeleniumConfig.GetSoftAssertScreenshot()) diff --git a/Framework/BaseTest/SoftAssert.cs b/Framework/BaseTest/SoftAssert.cs index 201ab6430..ac6595759 100644 --- a/Framework/BaseTest/SoftAssert.cs +++ b/Framework/BaseTest/SoftAssert.cs @@ -187,7 +187,6 @@ void test() /// Failure message /// Boolean if condition is met [Obsolete("SoftAssert.IsFalse will be deprecated in MAQS 7.0. Please use SoftAssert.Assert() instead")] - public virtual bool IsFalse(bool condition, string softAssertName, string failureMessage = "") { void test() @@ -273,9 +272,9 @@ public void FailTestIfAssertFailed(string message) internal void CheckForExpectedAsserts() { - foreach(var expectedAssert in _expectedAssertNames) + foreach (var expectedAssert in _expectedAssertNames) { - if(!_calledAssertNames.Contains(expectedAssert)) + if (!_calledAssertNames.Contains(expectedAssert)) { this.NumberOfAsserts++; this.NumberOfFailedAsserts++; @@ -284,6 +283,17 @@ internal void CheckForExpectedAsserts() } } + /// + /// Wrap an assert inside a soft assert + /// + /// The assert function + /// True if the asset passed + [Obsolete("SoftAssert.Assert(Action) is obsolete. Please use SoftAssert.Assert(Action, assertName)")] + public bool Assert(Action assertFunction) + { + return this.Assert(assertFunction, string.Empty, string.Empty); + } + /// /// Wrap an assert inside a soft assert /// @@ -291,9 +301,9 @@ internal void CheckForExpectedAsserts() /// Message to log /// Soft assert name or name of expected assert being called. /// True if the asset passed - public virtual bool Assert(Action assertFunction, string failureMessage = "", string assertName = null) + public virtual bool Assert(Action assertFunction, string assertName, string failureMessage = "") { - if(!string.IsNullOrEmpty(assertName) && _expectedAssertNames.Any()) + if (!string.IsNullOrEmpty(assertName) && _expectedAssertNames.Any()) { _calledAssertNames.Add(assertName); } @@ -307,40 +317,19 @@ public virtual bool Assert(Action assertFunction, string failureMessage = "", st assertFunction.Invoke(); this.NumberOfPassedAsserts = ++this.NumberOfPassedAsserts; result = true; - if (!string.IsNullOrEmpty(assertName)) - { - this.Log.LogMessage(MessageType.SUCCESS, "SoftAssert passed for: {0}.", assertName); - } - else - { - this.Log.LogMessage(MessageType.SUCCESS, "SoftAssert passed for: {0}.", assertFunction.Method.Name); - } + this.Log.LogMessage(MessageType.SUCCESS, $"SoftAssert passed for: {assertName}."); } catch (Exception ex) { this.NumberOfFailedAsserts = ++this.NumberOfFailedAsserts; result = false; - if (!string.IsNullOrEmpty(assertName)) + if (string.IsNullOrEmpty(failureMessage)) { - if (string.IsNullOrEmpty(failureMessage)) - { - this.Log.LogMessage(MessageType.WARNING, $"SoftAssert failed for: {assertName}. {ex.Message}"); - } - else - { - this.Log.LogMessage(MessageType.WARNING, $"SoftAssert failed for: {assertName}. {failureMessage}. {ex.Message}"); - } + this.Log.LogMessage(MessageType.WARNING, $"SoftAssert failed for: {assertName}. {ex.Message}"); } else { - if (string.IsNullOrEmpty(failureMessage)) - { - this.Log.LogMessage(MessageType.WARNING, $"SoftAssert failed for: {assertFunction.Method.Name}. {ex.Message}"); - } - else - { - this.Log.LogMessage(MessageType.WARNING, $"SoftAssert failed for: {assertFunction.Method.Name}. {failureMessage}. {ex.Message}"); - } + this.Log.LogMessage(MessageType.WARNING, $"SoftAssert failed for: {assertName}. {failureMessage}. {ex.Message}"); } this.listOfExceptions.Add(ex); diff --git a/Framework/BaseTestUnitTests/BaseFrameworkTests.cs b/Framework/BaseTestUnitTests/BaseFrameworkTests.cs index 52e2277b8..5d5c6e5d4 100644 --- a/Framework/BaseTestUnitTests/BaseFrameworkTests.cs +++ b/Framework/BaseTestUnitTests/BaseFrameworkTests.cs @@ -243,8 +243,8 @@ public void SoftAssertAssertFailed() tester.Setup(); tester.Log = new FileLogger(string.Empty, $"{Guid.NewGuid()}.txt"); - tester.SoftAssert.Assert(() => throw new Exception("broke")); - tester.SoftAssert.Assert(() => throw new Exception("broke again")); + tester.SoftAssert.Assert(() => throw new Exception("broke"), "Name1"); + tester.SoftAssert.Assert(() => throw new Exception("broke again"), "Name2"); try { tester.SoftAssert.FailTestIfAssertFailed(); diff --git a/Framework/CompositeUnitTests/Base.cs b/Framework/CompositeUnitTests/Base.cs index 3e323be28..fbf72563b 100644 --- a/Framework/CompositeUnitTests/Base.cs +++ b/Framework/CompositeUnitTests/Base.cs @@ -25,10 +25,10 @@ public class Base : BaseTest public void ConfigSections() { var keysAndValues = Config.GetSection("MagenicMaqS"); - SoftAssert.Assert(() => Assert.AreEqual(9, keysAndValues.Count, "Expect 9 values, 6 from app.config plus 3 from run settings file")); - SoftAssert.Assert(() => Assert.AreEqual("TXT", keysAndValues["LogType"], "Base configuration not respected")); - SoftAssert.Assert(() => Assert.AreEqual("SAMPLEGen", keysAndValues["SectionOverride"], "Override not respected")); - SoftAssert.Assert(() => Assert.AreEqual("SAMPLEGenz", keysAndValues["SectionAdd"], "Run settings addition not respected")); + SoftAssert.Assert(() => Assert.AreEqual(9, keysAndValues.Count, "Expect 9 values, 6 from app.config plus 3 from run settings file"), "1"); + SoftAssert.Assert(() => Assert.AreEqual("TXT", keysAndValues["LogType"], "Base configuration not respected"), "2"); + SoftAssert.Assert(() => Assert.AreEqual("SAMPLEGen", keysAndValues["SectionOverride"], "Override not respected"), "3"); + SoftAssert.Assert(() => Assert.AreEqual("SAMPLEGenz", keysAndValues["SectionAdd"], "Run settings addition not respected"), "4"); } /// @@ -39,7 +39,7 @@ public void ConfigSections() public void EmptyConfigSections() { var keysAndValues = Config.GetSection("MagenicMaqSZZZ"); - SoftAssert.Assert(() => Assert.AreEqual(0, keysAndValues.Count, "Expected no matching configuration key value pairs.")); + SoftAssert.Assert(() => Assert.AreEqual(0, keysAndValues.Count, "Expected no matching configuration key value pairs."), "1"); } /// diff --git a/Framework/SeleniumUnitTests/LazyElementUnitTests.cs b/Framework/SeleniumUnitTests/LazyElementUnitTests.cs index c74669617..f181a2a9f 100644 --- a/Framework/SeleniumUnitTests/LazyElementUnitTests.cs +++ b/Framework/SeleniumUnitTests/LazyElementUnitTests.cs @@ -1067,7 +1067,7 @@ public void LazyElementFindElementsAreLazy() { foreach (IWebElement element in this.FlowerTableLazyElement.FindElements(By.CssSelector("THEAD TH"))) { - this.SoftAssert.Assert(() => Assert.IsTrue(element is LazyElement)); + this.SoftAssert.Assert(() => Assert.IsTrue(element is LazyElement), "1"); } this.SoftAssert.FailTestIfAssertFailed(); diff --git a/Framework/SeleniumUnitTests/SeleniumUnitTest.cs b/Framework/SeleniumUnitTests/SeleniumUnitTest.cs index c82efb233..b62e6aea9 100644 --- a/Framework/SeleniumUnitTests/SeleniumUnitTest.cs +++ b/Framework/SeleniumUnitTests/SeleniumUnitTest.cs @@ -720,7 +720,7 @@ public void SeleniumSoftAssertWithAssertIsTrueFalseCondition() string logLocation = ((FileLogger)Log).FilePath; string screenShotLocation = logLocation.Substring(0, logLocation.LastIndexOf('.')) + " (1).Png"; - bool isFalse = seleniumSoftAssert.Assert(() => Assert.IsTrue(false, "testSoftAssert", "message")); + bool isFalse = seleniumSoftAssert.Assert(() => Assert.IsTrue(false, "testSoftAssert", "message"), "1"); Assert.IsTrue(File.Exists(screenShotLocation), "Fail to find screenshot"); File.Delete(screenShotLocation); @@ -764,7 +764,7 @@ public void SeleniumSoftAssertWithAssertIsTrueFalseConditionPageSource() string logLocation = ((FileLogger)Log).FilePath; string pageSourceLocation = logLocation.Substring(0, logLocation.LastIndexOf('.')) + "_PS (1).txt"; - bool isFalse = seleniumSoftAssert.Assert(() => Assert.IsTrue(false, "testSoftAssert", "message")); + bool isFalse = seleniumSoftAssert.Assert(() => Assert.IsTrue(false, "testSoftAssert", "message"), "1"); Assert.IsTrue(File.Exists(pageSourceLocation), "Fail to find page source"); File.Delete(pageSourceLocation); diff --git a/Framework/UtilitiesUnitTests/SoftAssertUnitTests.cs b/Framework/UtilitiesUnitTests/SoftAssertUnitTests.cs index 46e785c2b..590153b01 100644 --- a/Framework/UtilitiesUnitTests/SoftAssertUnitTests.cs +++ b/Framework/UtilitiesUnitTests/SoftAssertUnitTests.cs @@ -188,7 +188,7 @@ public void SoftAssertIsFalseTest() public void AcceptVSAsserts() { SoftAssert softAssert = new SoftAssert(new FileLogger(LoggingConfig.GetLogDirectory(), "UnitTests.SoftAssertUnitTests.AcceptVSAsserts")); - softAssert.Assert(() => Assert.AreEqual("a", "a")); + softAssert.Assert(() => Assert.AreEqual("a", "a"), "1"); softAssert.FailTestIfAssertFailed(); } @@ -200,7 +200,7 @@ public void AcceptVSAsserts() public void AcceptNUnitAsserts() { SoftAssert softAssert = new SoftAssert(new FileLogger(LoggingConfig.GetLogDirectory(), "UnitTests.SoftAssertUnitTests.AcceptNUnitAsserts")); - softAssert.Assert(() => NUnit.Framework.Assert.AreEqual("a", "a")); + softAssert.Assert(() => NUnit.Framework.Assert.AreEqual("a", "a"), "1"); softAssert.FailTestIfAssertFailed(); } @@ -213,7 +213,7 @@ public void AcceptNUnitAsserts() public void CapturesVSAssertFail() { SoftAssert softAssert = new SoftAssert(new FileLogger(LoggingConfig.GetLogDirectory(), "UnitTests.SoftAssertUnitTests.RespectVSFailsFails")); - softAssert.Assert(() => Assert.AreEqual("a", "b")); + softAssert.Assert(() => Assert.AreEqual("a", "b"), "2"); softAssert.FailTestIfAssertFailed(); } @@ -227,7 +227,7 @@ public void CapturesVSAssertFail() public void CapturesNUnitAssertFail() { SoftAssert softAssert = new SoftAssert(new FileLogger(LoggingConfig.GetLogDirectory(), "UnitTests.SoftAssertUnitTests.RespectNUnitFails")); - softAssert.Assert(() => NUnit.Framework.Assert.AreEqual("a", "b")); + softAssert.Assert(() => NUnit.Framework.Assert.AreEqual("a", "b"), "1"); softAssert.FailTestIfAssertFailed(); } From 041cab59bd4299b9fb8915ef49da6363f6847fe6 Mon Sep 17 00:00:00 2001 From: Jake Ferm Date: Wed, 13 Jan 2021 23:15:54 -0600 Subject: [PATCH 10/11] Fixing failed unit tests --- Framework/SeleniumUnitTests/SeleniumUnitTest.cs | 2 +- Framework/UtilitiesUnitTests/SoftAssertUnitTests.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Framework/SeleniumUnitTests/SeleniumUnitTest.cs b/Framework/SeleniumUnitTests/SeleniumUnitTest.cs index b62e6aea9..95da3adaa 100644 --- a/Framework/SeleniumUnitTests/SeleniumUnitTest.cs +++ b/Framework/SeleniumUnitTests/SeleniumUnitTest.cs @@ -718,7 +718,7 @@ public void SeleniumSoftAssertWithAssertIsTrueFalseCondition() this.Log = new FileLogger(string.Empty, "SeleniumSoftAssertWithAssertIsTrueFalseCondition.txt", MessageType.GENERIC, true); SeleniumSoftAssert seleniumSoftAssert = new SeleniumSoftAssert(TestObject); string logLocation = ((FileLogger)Log).FilePath; - string screenShotLocation = logLocation.Substring(0, logLocation.LastIndexOf('.')) + " (1).Png"; + string screenShotLocation = $"{logLocation.Substring(0, logLocation.LastIndexOf('.'))} 1 (1).Png"; bool isFalse = seleniumSoftAssert.Assert(() => Assert.IsTrue(false, "testSoftAssert", "message"), "1"); diff --git a/Framework/UtilitiesUnitTests/SoftAssertUnitTests.cs b/Framework/UtilitiesUnitTests/SoftAssertUnitTests.cs index 590153b01..987c64115 100644 --- a/Framework/UtilitiesUnitTests/SoftAssertUnitTests.cs +++ b/Framework/UtilitiesUnitTests/SoftAssertUnitTests.cs @@ -244,7 +244,7 @@ public void SoftAssertManuallySetExpectedAsserts() new FileLogger(LoggingConfig.GetLogDirectory(), "UnitTests.SoftAssertManuallySetExpectedAssert")); softAssert.AddExpectedAsserts("one"); - softAssert.Assert(() => { }, "AssertionMethod", "one"); + softAssert.Assert(() => { }, "one", "AssertionMethod"); softAssert.FailTestIfAssertFailed(); } From 6c84125f003a8110846e130862aefec44905df84 Mon Sep 17 00:00:00 2001 From: Jake Ferm Date: Wed, 13 Jan 2021 23:53:56 -0600 Subject: [PATCH 11/11] Fixing SonarCloud issues --- Framework/AppiumUnitTests/App.config | 4 ++-- .../AppiumUnitTests/AppiumIosUnitTests.cs | 24 +++++++++++++++++++ .../UtilitiesUnitTests/SoftAssertUnitTests.cs | 22 ++++++++++++++++- 3 files changed, 47 insertions(+), 3 deletions(-) diff --git a/Framework/AppiumUnitTests/App.config b/Framework/AppiumUnitTests/App.config index cdb8b5197..5529669b8 100644 --- a/Framework/AppiumUnitTests/App.config +++ b/Framework/AppiumUnitTests/App.config @@ -57,7 +57,7 @@ - + - + diff --git a/Framework/AppiumUnitTests/AppiumIosUnitTests.cs b/Framework/AppiumUnitTests/AppiumIosUnitTests.cs index e4efdfa83..61b3ba390 100644 --- a/Framework/AppiumUnitTests/AppiumIosUnitTests.cs +++ b/Framework/AppiumUnitTests/AppiumIosUnitTests.cs @@ -6,10 +6,12 @@ //-------------------------------------------------- using Magenic.Maqs.BaseAppiumTest; using Magenic.Maqs.Utilities.Helper; +using Magenic.Maqs.Utilities.Logging; using Microsoft.VisualStudio.TestTools.UnitTesting; using OpenQA.Selenium; using OpenQA.Selenium.Appium; using OpenQA.Selenium.Appium.iOS; +using System.IO; namespace AppiumUnitTests { @@ -46,6 +48,28 @@ public void AppiumIOSDriverLazyTest() lazy.Click(); } + [TestMethod] + [TestCategory(TestCategories.Appium)] + public void AssertFuncFailPath() + { + Assert.IsNotNull(this.TestObject.AppiumDriver); + this.AppiumDriver.Navigate().GoToUrl(Config.GetValueForSection(ConfigSection.AppiumMaqs, "WebSiteBase")); + + + Log = new FileLogger(string.Empty, "AssertFuncFailPath.txt", MessageType.GENERIC, true); + AppiumSoftAssert appiumSoftAssert = new AppiumSoftAssert(TestObject); + string logLocation = ((FileLogger)Log).FilePath; + string screenShotLocation = $"{logLocation.Substring(0, logLocation.LastIndexOf('.'))} assertName (1).Png"; + + bool isFalse = appiumSoftAssert.Assert(() => Assert.IsTrue(false), "assertName"); + Assert.IsTrue(File.Exists(screenShotLocation), "Fail to find screenshot"); + File.Delete(screenShotLocation); + File.Delete(logLocation); + + Assert.IsFalse(isFalse); + } + + /// /// Sets capabilities for testing the iOS Driver creation /// diff --git a/Framework/UtilitiesUnitTests/SoftAssertUnitTests.cs b/Framework/UtilitiesUnitTests/SoftAssertUnitTests.cs index 987c64115..55bc0dbd7 100644 --- a/Framework/UtilitiesUnitTests/SoftAssertUnitTests.cs +++ b/Framework/UtilitiesUnitTests/SoftAssertUnitTests.cs @@ -273,7 +273,27 @@ public void SoftAssertAssertMethodWithFailureMessage() { SoftAssert softAssert = new SoftAssert(new FileLogger(LoggingConfig.GetLogDirectory(), "UnitTests.SoftAssertAssertMethodWithFailureMessage")); - softAssert.Assert(() => Assert.Fail(), "Failure Message"); + softAssert.Assert(() => Assert.Fail(), "SoftAssertName", "Failure Message"); + softAssert.FailTestIfAssertFailed(); + } + + [TestMethod] + [TestCategory(TestCategories.Utilities)] + [ExpectedException(typeof(AggregateException))] + public void SoftAssertAssertFailsWithPassingAction() + { + SoftAssert softAssert = new SoftAssert(new FileLogger(LoggingConfig.GetLogDirectory(), + "UnitTests.SoftAssertAssertFailsWithPassingAction")); + softAssert.AssertFails(() => Assert.IsTrue(true), typeof(AggregateException), "assertName"); + softAssert.FailTestIfAssertFailed(); + } + + [TestMethod] + public void SoftAssertActionWithEmptyAssertionName() + { + SoftAssert softAssert = new SoftAssert(new FileLogger(LoggingConfig.GetLogDirectory(), + "UnitTests.SoftAssertActionWithEmptyAssertionName")); + softAssert.Assert(() => Assert.IsTrue(true), string.Empty); softAssert.FailTestIfAssertFailed(); }