public static void AssertEquals(IWebDriver driver, ExtentTest extentTest, int actualValue, int expectedValue, string reportingMessage) { try { Assert.AreEqual(expectedValue, actualValue); extentTest.Pass(reportingMessage); } catch (AssertionException) { if (driver != null) { extentTest.Fail("Failure occurred when executing check '" + reportingMessage + "', actual value was " + actualValue, MediaEntityBuilder.CreateScreenCaptureFromPath(ReportingMethods.CreateScreenshot(driver)).Build()); } else { extentTest.Fail("Failure occurred when executing check '" + reportingMessage + "', actual value was " + actualValue); } throw; } }
public static void AssertTrue(IWebDriver driver, ExtentTest extentTest, bool assertedValue, string reportingMessage) { try { Assert.IsTrue(assertedValue); extentTest.Pass(reportingMessage); } catch (AssertionException) { extentTest.Fail("Failure occurred when executing check '" + reportingMessage + "'", MediaEntityBuilder.CreateScreenCaptureFromPath(ReportingMethods.CreateScreenshot(driver)).Build()); throw; } }
// Returns the value of the text property for the specified element // Mostly used for retrieving values for input elements (text boxes) // Catches and handles exceptions that might occur public static string GetElementText(IWebDriver driver, By by) { string returnValue = ""; try { new WebDriverWait(driver, TimeSpan.FromSeconds(Constants.DefaultTimeout)).Until(ExpectedConditions.ElementIsVisible(by)); returnValue = driver.FindElement(by).Text; } catch (Exception ex) when(ex is NoSuchElementException || ex is WebDriverTimeoutException) { ExtentTest test = ScenarioContext.Current.Get <ExtentTest>(); test.Error("Could not perform GetElementText on element identified by " + by.ToString() + " after " + Constants.DefaultTimeout.ToString() + " seconds", MediaEntityBuilder.CreateScreenCaptureFromPath(ReportingMethods.CreateScreenshot(driver)).Build()); Assert.Fail(); } return(returnValue); }
// Returns whether an element is visible // Takes into account a predefined timeout // Logs to HTML if the element is not present and visible after this timeout public static bool CheckElementIsVisible(IWebDriver driver, By by) { try { new WebDriverWait(driver, TimeSpan.FromSeconds(Constants.DefaultTimeout)).Until(ExpectedConditions.ElementIsVisible((by))); } catch (Exception ex) when(ex is NoSuchElementException || ex is WebDriverTimeoutException) { ScenarioContext.Current.Get <ExtentTest>().Error("Element identified by " + by.ToString() + " not visible after " + Constants.DefaultTimeout.ToString() + " seconds, but was expected to be visible", MediaEntityBuilder.CreateScreenCaptureFromPath(ReportingMethods.CreateScreenshot(driver)).Build()); return(false); } return(true); }
// Tries to click an element taking into account a predefined timeout // This can generate a variety of exception that are all handled in this method public static void Click(IWebDriver driver, By by) { try { new WebDriverWait(driver, TimeSpan.FromSeconds(Constants.DefaultTimeout)).Until(ExpectedConditions.ElementToBeClickable(by)); driver.FindElement(by).Click(); } catch (Exception ex) when(ex is WebDriverTimeoutException || ex is NoSuchElementException) { ExtentTest test = ScenarioContext.Current.Get <ExtentTest>(); test.Error("Element identified by " + by.ToString() + " not clickable after " + Constants.DefaultTimeout.ToString() + " seconds", MediaEntityBuilder.CreateScreenCaptureFromPath(ReportingMethods.CreateScreenshot(driver)).Build()); Assert.Fail(); } }
// Tries to send the given input string to the element specified taking into account the predefined timeout // Catches and handles exceptions that might occur public static void SendKeys(IWebDriver driver, By by, string valueToType, bool inputValidation = false) { try { new WebDriverWait(driver, TimeSpan.FromSeconds(Constants.DefaultTimeout)).Until(ExpectedConditions.ElementIsVisible(by)); driver.FindElement(by).Clear(); driver.FindElement(by).SendKeys(valueToType); } catch (Exception ex) when(ex is NoSuchElementException || ex is WebDriverTimeoutException) { ExtentTest test = ScenarioContext.Current.Get <ExtentTest>(); test.Error("Could not perform SendKeys on element identified by " + by.ToString() + " after " + Constants.DefaultTimeout.ToString() + " seconds", MediaEntityBuilder.CreateScreenCaptureFromPath(ReportingMethods.CreateScreenshot(driver)).Build()); Assert.Fail(); } catch (Exception ex) when(ex is StaleElementReferenceException) { // find element again and retry new WebDriverWait(driver, TimeSpan.FromSeconds(Constants.DefaultTimeout)).Until(ExpectedConditions.ElementIsVisible(by)); driver.FindElement(by).Clear(); driver.FindElement(by).SendKeys(valueToType); } }
/// <summary> /// Ensure that the component is currently loaded. /// </summary> /// <returns>The loaded component.</returns> /// <remarks>This is equivalent to the Get() method in Java version.</remarks> public virtual T Load() { if (this.IsLoaded) { return((T)this); } else { this.TryLoad(); } if (!this.IsLoaded) { ExtentTest test = ScenarioContext.Current.Get <ExtentTest>(); IWebDriver driver = ScenarioContext.Current.Get <IWebDriver>(); test.Error("LoadableComponentException caught during page load: " + this.UnableToLoadMessage, MediaEntityBuilder.CreateScreenCaptureFromPath(ReportingMethods.CreateScreenshot(driver)).Build()); throw new LoadableComponentException(this.UnableToLoadMessage); } return((T)this); }