Skip to content
This repository has been archived by the owner on Apr 30, 2022. It is now read-only.

Impement SoftAssert.Assert overrides for SeleniumSoftAssert and AppiumSoftAssert #594

Merged
merged 16 commits into from
Jan 14, 2021
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions Framework/BaseAppiumTest/AppiumSoftAssert.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,34 @@ public override bool AreEqual(string expectedText, string actualText, string sof
return true;
}

/// <summary>
/// Soft assert method to check if the Action is false
/// </summary>
/// <param name="assertFunction">Function to use</param>
/// <param name="failureMessage">Message to log</param>
/// <param name="assertCalledKey">Key of expected assert being called.</param>
/// <returns>Boolean of the assert</returns>
public override bool Assert(Action assertFunction, string failureMessage = "", string assertCalledKey = null)
FermJacob marked this conversation as resolved.
Show resolved Hide resolved
{
bool didPass = base.Assert(assertFunction, failureMessage, assertCalledKey);

if (!didPass && this.appiumTestObject.GetDriverManager<MobileDriverManager>().IsDriverIntialized())
{
if (AppiumConfig.GetSoftAssertScreenshot())
{
AppiumUtilities.CaptureScreenshot(this.appiumTestObject.AppiumDriver, this.appiumTestObject, this.TextToAppend(assertCalledKey));
}

if (AppiumConfig.GetSavePagesourceOnFail())
{
AppiumUtilities.SavePageSource(this.appiumTestObject.AppiumDriver, this.appiumTestObject, StringProcessor.SafeFormatter(" ({0})", this.NumberOfAsserts));
}

return false;
}
return didPass;
}

/// <summary>
/// Method to determine the text to be appended to the screenshot file names
/// </summary>
Expand Down
27 changes: 27 additions & 0 deletions Framework/BaseSeleniumTest/SeleniumSoftAssert.cs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,33 @@ public override bool IsTrue(bool condition, string softAssertName, string failur
return true;
}

/// <summary>
/// Soft assert method to check if the Action is false
/// </summary>
/// <param name="assertFunction">Function to use</param>
/// <param name="failureMessage">Message to log</param>
/// <param name="assertCalledKey">Key of expected assert being called.</param>
/// <returns>Boolean of the assert</returns>
public override bool Assert(Action assertFunction,string failureMessage = "", string assertCalledKey = null)
{
bool didPass = base.Assert(assertFunction, failureMessage, assertCalledKey);
if (!didPass && this.testObject.GetDriverManager<SeleniumDriverManager>().IsDriverIntialized())
{
if (SeleniumConfig.GetSoftAssertScreenshot())
{
SeleniumUtilities.CaptureScreenshot(this.testObject.WebDriver, this.testObject, this.TextToAppend(assertCalledKey));
}

if (SeleniumConfig.GetSavePagesourceOnFail())
{
SeleniumUtilities.SavePageSource(this.testObject.WebDriver, this.testObject, StringProcessor.SafeFormatter(" ({0})", this.NumberOfAsserts));
}

return false;
}
return didPass;
}

/// <summary>
/// Method to determine the text to be appended to the screenshot file names
/// </summary>
Expand Down
12 changes: 10 additions & 2 deletions Framework/BaseTest/SoftAssert.cs
Original file line number Diff line number Diff line change
Expand Up @@ -286,9 +286,10 @@ internal void CheckForExpectedAsserts()
/// Wrap an assert inside a soft assert
/// </summary>
/// <param name="assertFunction">The assert function</param>
/// <param name="failureMessage">Message to log</param>
/// <param name="assertCalledKey">Key of expected assert being called.</param>
/// <returns>True if the asset passed</returns>
public bool Assert(Action assertFunction, string assertCalledKey = null)
public virtual bool Assert(Action assertFunction, string failureMessage = "", string assertCalledKey = null)
{
if(!string.IsNullOrEmpty(assertCalledKey) && _expectedAsserts.Any())
{
Expand All @@ -310,7 +311,14 @@ public 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
Expand Down
44 changes: 44 additions & 0 deletions Framework/SeleniumUnitTests/SeleniumUnitTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -707,6 +707,28 @@ public void SeleniumSoftAssertIsTrueFalseCondition()
Assert.IsFalse(isFalse);
}

/// <summary>
/// Verify that a screenshot is taken if the SeleniumSoftAssert.IsTrue gets a false condition and the logger is set to log screenshots
/// </summary>
[TestMethod]
[TestCategory(TestCategories.Selenium)]
public void SeleniumSoftAssertWithAssertIsTrueFalseCondition()
{
this.WebDriver.Navigate().GoToUrl(TestSiteAutomationUrl);
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";

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);
}

/// <summary>
/// Verify that page source is saved if the SeleniumSoftAssert.IsTrue gets a false condition and the logger is set to save Page Source
/// </summary>
Expand All @@ -729,6 +751,28 @@ public void SeleniumSoftAssertIsTrueFalseConditionPageSource()
Assert.IsFalse(isFalse);
}

/// <summary>
/// Verify that page source is saved if the SeleniumSoftAssert.IsTrue gets a false condition and the logger is set to save Page Source
/// </summary>
[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);
}

/// <summary>
/// Verify that a screenshot is taken if the SeleniumSoftAssert.IsFalse gets a true condition and the logger is set to log screenshots
/// </summary>
Expand Down
12 changes: 6 additions & 6 deletions Framework/UtilitiesUnitTests/SoftAssertUnitTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand All @@ -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();
Expand Down Expand Up @@ -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();
}

Expand Down