/// <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="assertName">Soft assert name or name of expected assert being called.</param> /// <returns>Boolean of the assert</returns> public override bool Assert(Action assertFunction, string assertName, string failureMessage = "") { bool didPass = base.Assert(assertFunction, assertName, failureMessage); if (!didPass && this.testObject.GetDriverManager <SeleniumDriverManager>().IsDriverIntialized()) { if (SeleniumConfig.GetSoftAssertScreenshot()) { SeleniumUtilities.CaptureScreenshot(this.testObject.WebDriver, this.testObject, this.TextToAppend(assertName)); } if (SeleniumConfig.GetSavePagesourceOnFail()) { SeleniumUtilities.SavePageSource(this.testObject.WebDriver, this.testObject, $" ({this.NumberOfAsserts})"); } return(false); } return(didPass); }
/// <summary> /// Take a screen shot if needed and tear down the web driver /// </summary> /// <param name="resultType">The test result</param> protected override void BeforeCleanup(TestResultType resultType) { // Try to take a screen shot try { if (this.TestObject.GetDriverManager <SeleniumDriverManager>().IsDriverIntialized() && this.Log is IFileLogger && resultType != TestResultType.PASS && this.LoggingEnabledSetting != LoggingEnabled.NO) { SeleniumUtilities.CaptureScreenshot(this.WebDriver, this.TestObject, " Final"); if (SeleniumConfig.GetSavePagesourceOnFail()) { SeleniumUtilities.SavePageSource(this.WebDriver, this.TestObject, "FinalPageSource"); } } } catch (Exception e) { this.TryToLog(MessageType.WARNING, $"Failed to get screen shot because: {e.Message}"); } }
/// <summary> /// Take a screen shot if needed and tear down the web driver /// </summary> /// <param name="resultType">The test result</param> protected override void BeforeLoggingTeardown(TestResultType resultType) { // Try to take a screen shot try { if (this.Log is FileLogger && resultType != TestResultType.PASS && this.LoggingEnabledSetting != LoggingEnabled.NO) { SeleniumUtilities.CaptureScreenshot(this.WebDriver, this.Log); if (SeleniumConfig.GetSavePagesourceOnFail()) { SeleniumUtilities.SavePageSource(this.WebDriver, this.Log, "FinalPageSource"); } } } catch (Exception e) { this.TryToLog(MessageType.WARNING, "Failed to get screen shot because: {0}", e.Message); } }
/// <summary> /// Soft assert method to check if the boolean is false /// </summary> /// <param name="condition">Boolean condition</param> /// <param name="softAssertName">Soft assert name</param> /// <param name="failureMessage">Failure message</param> /// <returns>Boolean of the assert</returns> /// <example> /// <code source = "../SeleniumUnitTesting/SeleniumUnitTest.cs" region="SoftAssertIsFalse" lang="C#" /> /// </example> public override bool IsFalse(bool condition, string softAssertName, string failureMessage = "") { bool didPass = base.IsFalse(condition, softAssertName, failureMessage); if (!didPass) { if (SeleniumConfig.GetSoftAssertScreenshot()) { SeleniumUtilities.CaptureScreenshot(this.testObject.WebDriver, this.Log, this.TextToAppend(softAssertName)); } if (SeleniumConfig.GetSavePagesourceOnFail()) { SeleniumUtilities.SavePageSource(this.testObject.WebDriver, this.Log, StringProcessor.SafeFormatter(" ({0})", this.NumberOfAsserts)); } return(false); } return(true); }
/// <summary> /// Soft assert method to check if the strings are equal /// </summary> /// <param name="expectedText">Expected text</param> /// <param name="actualText">Actual text</param> /// <param name="softAssertName">Soft assert name to use</param> /// <param name="message">Exception message if desired</param> /// <returns>Boolean if the assert is true</returns> /// <example> /// <code source = "../SeleniumUnitTesting/SeleniumUnitTest.cs" region="SoftAssertAreEqual" lang="C#" /> /// </example> public override bool AreEqual(string expectedText, string actualText, string softAssertName, string message = "") { bool didPass = base.AreEqual(expectedText, actualText, softAssertName, message); if (!didPass) { if (SeleniumConfig.GetSoftAssertScreenshot()) { SeleniumUtilities.CaptureScreenshot(this.testObject.WebDriver, this.Log, this.TextToAppend(softAssertName)); } if (SeleniumConfig.GetSavePagesourceOnFail()) { SeleniumUtilities.SavePageSource(this.testObject.WebDriver, this.Log, StringProcessor.SafeFormatter(" ({0})", this.NumberOfAsserts)); } return(false); } return(true); }
/// <summary> /// Drag and drop an element /// </summary> /// <param name="source">Element to drag and drop</param> /// <param name="destination">Where to drop the element</param> public static void DragAndDrop(this IWebElement source, IWebElement destination) { Actions builder = new Actions(SeleniumUtilities.SearchContextToWebDriver(source)); builder.DragAndDrop(source, destination).Build().Perform(); }
/// <summary> /// Performs a hover over on an element /// </summary> /// <param name="element">The web element</param> public static void HoverOver(this IWebElement element) { Actions builder = new Actions(SeleniumUtilities.SearchContextToWebDriver(element)); builder.MoveToElement(element).Build().Perform(); }
/// <summary> /// Performs a right-click on an element /// </summary> /// <param name="element">The web element</param> public static void DoubleClick(this IWebElement element) { Actions builder = new Actions(SeleniumUtilities.SearchContextToWebDriver(element)); builder.DoubleClick(element).Build().Perform(); }
/// <summary> /// Drag and drop an element to an X and Y offsets /// </summary> /// <param name="source">Element to drag and drop</param> /// <param name="pixelsXOffset">Integer of pixels to be moved (Positive or negative) horizontally</param> /// <param name="pixelsYOffset">Integer of pixels to be moved (Positive or negative) vertically </param> public static void DragAndDropToOffset(this IWebElement source, int pixelsXOffset, int pixelsYOffset) { Actions builder = new Actions(SeleniumUtilities.SearchContextToWebDriver(source)); builder.DragAndDropToOffset(source, pixelsXOffset, pixelsYOffset).Build().Perform(); }
/// <summary> /// Get the web driver from a web element or web driver /// </summary> /// <param name="searchContext">The search context</param> /// <returns>The web driver</returns> public static IWebDriver SearchContextToWebDriver(ISearchContext searchContext) { return((searchContext is IWebDriver) ? (IWebDriver)searchContext : SeleniumUtilities.WebElementToWebDriver((IWebElement)searchContext)); }
/// <summary> /// Get the javaScript executor from a web element or web driver /// </summary> /// <param name="searchContext">The search context</param> /// <returns>The javaScript executor</returns> public static IJavaScriptExecutor SearchContextToJavaScriptExecutor(ISearchContext searchContext) { return((searchContext is IJavaScriptExecutor) ? (IJavaScriptExecutor)searchContext : (IJavaScriptExecutor)SeleniumUtilities.WebElementToWebDriver((IWebElement)searchContext)); }
/// <summary> /// JavaScript method to scroll an element into the view /// </summary> /// <param name="searchContext">Web driver or element</param> /// <param name="element">IWebElement</param> public static void ScrollIntoView(this ISearchContext searchContext, IWebElement element) { IJavaScriptExecutor executor = SeleniumUtilities.SearchContextToJavaScriptExecutor(searchContext); executor.ExecuteScript("arguments[0].scrollIntoView(true);", element); }
/// <summary> /// JavaScript method to scroll an element into the view /// </summary> /// <param name="element">IWebElement</param> public static void ScrollIntoView(this IWebElement element) { var driver = SeleniumUtilities.SearchContextToWebDriver(element); driver.ScrollIntoView(element); }
/// <summary> /// Method to click an element via JavaScript /// Used for scenarios where normal click can't reach, such as hidden or hover triggered elements. /// </summary> /// <param name="element">Element to click</param> public static void ClickElementByJavaScript(this IWebElement element) { IJavaScriptExecutor executor = SeleniumUtilities.SearchContextToJavaScriptExecutor(element); executor.ExecuteScript("arguments[0].click();", element); }