From 10ba0da3f6e8832470e39f69bf6f3d6cdabe5e72 Mon Sep 17 00:00:00 2001 From: Jason Edstrom <JasonE@magenic.com> Date: Thu, 14 May 2020 16:41:06 -0500 Subject: [PATCH 01/12] JMAQS #309 - Corrected most checkstyle --- .../com/magenic/jmaqs/base/DriverManager.java | 1 + .../jmaqs/selenium/AbstractLazyElement.java | 1199 +++++----- .../jmaqs/selenium/BaseSeleniumPageModel.java | 68 +- .../jmaqs/selenium/BaseSeleniumTest.java | 9 +- .../jmaqs/selenium/LazyWebElement.java | 129 +- .../jmaqs/selenium/SeleniumTestObject.java | 30 +- .../com/magenic/jmaqs/selenium/UIWait.java | 2131 ++++++++--------- .../selenium/factories/FluentWaitFactory.java | 21 +- .../selenium/factories/UIWaitFactory.java | 185 +- .../jmaqs/utilities/helper/Config.java | 1 - .../exceptions/ExecutionFailedException.java | 26 +- .../jmaqs/utilities/logging/FileLogger.java | 2 +- 12 files changed, 1876 insertions(+), 1926 deletions(-) diff --git a/jmaqs-base/src/main/java/com/magenic/jmaqs/base/DriverManager.java b/jmaqs-base/src/main/java/com/magenic/jmaqs/base/DriverManager.java index 843aefe11..cc36407cb 100644 --- a/jmaqs-base/src/main/java/com/magenic/jmaqs/base/DriverManager.java +++ b/jmaqs-base/src/main/java/com/magenic/jmaqs/base/DriverManager.java @@ -9,6 +9,7 @@ /** * The type Driver manager. + * @param <T> Manager of type T */ public abstract class DriverManager<T> implements AutoCloseable { diff --git a/jmaqs-selenium/src/main/java/com/magenic/jmaqs/selenium/AbstractLazyElement.java b/jmaqs-selenium/src/main/java/com/magenic/jmaqs/selenium/AbstractLazyElement.java index 6f644d8c5..608f44af3 100644 --- a/jmaqs-selenium/src/main/java/com/magenic/jmaqs/selenium/AbstractLazyElement.java +++ b/jmaqs-selenium/src/main/java/com/magenic/jmaqs/selenium/AbstractLazyElement.java @@ -11,12 +11,10 @@ import com.magenic.jmaqs.utilities.helper.exceptions.TimeoutException; import com.magenic.jmaqs.utilities.helper.functionalinterfaces.Action; import com.magenic.jmaqs.utilities.logging.MessageType; - import java.util.List; import java.util.Objects; import java.util.function.Predicate; import java.util.function.Supplier; - import org.openqa.selenium.By; import org.openqa.selenium.Dimension; import org.openqa.selenium.InvalidElementStateException; @@ -32,599 +30,610 @@ */ public abstract class AbstractLazyElement { - /** - * The index in cases where the selector finds multiple elements - */ - private final Integer elementIndex; - - /** - * A user friendly name, for logging purposes - */ - protected final String userFriendlyName; - - /** - * The parent lazy element - */ - protected LazyWebElement parent; - - /** - * The 'by' selector for the element - */ - protected By by; - - /** - * The test object for the element - */ - protected SeleniumTestObject testObject; - - /** - * Cached copy of the element or null if we haven't already found the element - */ - private WebElement cachedElement; - - /** - * Initializes a new instance of the {@link #AbstractLazyElement} class - * - * @param testObject The selenium test object - * @param locator The by locator to search on - * @param userFriendlyName The user friendly name of the lazy element - */ - protected AbstractLazyElement(SeleniumTestObject testObject, By locator, String userFriendlyName) { - this.testObject = testObject; - this.by = locator; - this.userFriendlyName = userFriendlyName; - this.elementIndex = null; - } - - /** - * Initializes a new instance of the {@link #AbstractLazyElement} class - * - * @param parent The parent lazy element - * @param locator The by locator to search on - * @param userFriendlyName The user friendly name of the lazy element - */ - protected AbstractLazyElement(LazyWebElement parent, By locator, String userFriendlyName) { - this.testObject = parent.getTestObject(); - this.parent = parent; - this.by = locator; - this.userFriendlyName = userFriendlyName; - this.elementIndex = null; - } - - /** - * Initializes a new instance of the {@link #AbstractLazyElement} class - * - * @param parent The parent lazy element - * @param locator THe by locator to search on - * @param userFriendlyName The user friendly name of the lazy element - * @param elementIndex The index of the cached element - * @param cachedElement The cached element - */ - protected AbstractLazyElement( - LazyWebElement parent, - By locator, - String userFriendlyName, - Integer elementIndex, - WebElement cachedElement) { - this.testObject = parent.getTestObject(); - this.by = locator; - this.userFriendlyName = userFriendlyName; - this.parent = parent; - this.elementIndex = elementIndex; - this.cachedElement = cachedElement; - } - - /** - * Gets the by selector - * - * @return the by - */ - public By getBy() { - return this.by; - } - - /** - * Gets the cached element - * - * @return the cachedElement - */ - public WebElement getCachedElement() { - return this.cachedElement; - } - - /** - * Sets the cached Element - * - * @param cachedElementFactory the cachedElement function to set - * the cached element - */ - private void setCachedElement(Supplier<WebElement> cachedElementFactory) { - this.cachedElement = cachedElementFactory.get(); - } - - /** - * Gets the user friendly name - * - * @return the userFriendlyName - */ - public String getUserFriendlyName() { - return this.userFriendlyName; - } - - /** - * Gets the parent of the lazy element - * - * @return the parent - */ - public LazyWebElement getParent() { - return this.parent; - } - - /** - * Gets the test object - * - * @return the test object - */ - public SeleniumTestObject getTestObject() { - return this.testObject; - } - - /** - * Gets the tag name of the lazy element - * - * @return The tag name - * @throws TimeoutException If a timeout occurred while waiting for the element to be found - * @throws InterruptedException If the thread is interrupted while waiting for the element to be found - */ - public String getTagName() throws TimeoutException, InterruptedException { - this.getTestObject().getLogger().logMessage(MessageType.INFORMATION, String.format("Getting the tag name for lazy element '%s'", this.userFriendlyName)); - return GenericWait.waitFor(this.getElement(this::getRawExistingElement)::getTagName); - } - - /** - * Gets the lazy element's text - * - * @return The element text - * @throws TimeoutException If a timeout occurred while waiting for the element to be found - * @throws InterruptedException If the thread is interrupted while waiting for the element to be found - */ - public String getText() throws TimeoutException, InterruptedException { - this.getTestObject().getLogger().logMessage(MessageType.INFORMATION, String.format("Getting the text for lazy element '%s'", this.userFriendlyName)); - return GenericWait.waitFor(this.getElement(this::getRawExistingElement)::getText); - } - - /** - * Gets the lazy element's location - * - * @return the location as a Point - * @throws TimeoutException If a timeout occurred while waiting for the element to be found - * @throws InterruptedException If the thread is interrupted while waiting for the element to be found - */ - public Point getLocation() throws TimeoutException, InterruptedException { - this.getTestObject().getLogger().logMessage(MessageType.INFORMATION, String.format("Getting the location for lazy element '%s'", this.userFriendlyName)); - return GenericWait.waitFor(this.getElement(this::getRawExistingElement)::getLocation); - } - - /** - * Gets the lazy element's size - * - * @return The lazy element's size - * @throws TimeoutException If a timeout occurred while waiting for the element to be found - * @throws InterruptedException If the thread is interrupted while waiting for the element to be found - */ - public Dimension getSize() throws TimeoutException, InterruptedException { - this.getTestObject().getLogger().logMessage(MessageType.INFORMATION, String.format("Getting the size of lazy element '%s'", this.userFriendlyName)); - return GenericWait.waitFor(this.getElement(this::getRawExistingElement)::getSize); - } - - /** - * Click the lazy element - * - * @throws TimeoutException If a timeout occurred while waiting for the element to be found - * @throws InterruptedException If the thread is interrupted while waiting for the element to be found - * @throws ExecutionFailedException If error occurs while sending keys - */ - public void click() throws TimeoutException, InterruptedException, ExecutionFailedException { - this.getTestObject().getLogger().logMessage(MessageType.INFORMATION, String.format("Click '%s'", this.userFriendlyName)); - WebElement element = GenericWait.waitFor(() -> this.getElement(this::getRawClickableElement)); - this.executeEvent(element::click, "Click"); - } - - /** - * Send Secret keys with no logging - * - * @param keys the secret keys - * @throws ExecutionFailedException If error occurs while sending keys - * @throws TimeoutException If a timeout occurred while waiting for the element to be found - * @throws InterruptedException If the thread is interrupted while waiting for the element to be found - */ - public void sendSecretKeys(String keys) throws ExecutionFailedException, TimeoutException, InterruptedException { - this.getTestObject().getLogger() - .logMessage(MessageType.VERBOSE, "Send secret keys to '%s'", this.getUserFriendlyName()); - WebElement element = GenericWait.waitFor(() -> this.getElement(this::getRawVisibleElement)); - - try { - this.getTestObject().getLogger().suspendLogging(); - this.executeEvent(() -> element.sendKeys(keys), "SendKeys"); - this.getTestObject().getLogger().continueLogging(); - } catch (ExecutionFailedException e) { - this.getTestObject().getLogger().continueLogging(); - this.getTestObject().getLogger() - .logMessage(MessageType.ERROR, "Exception during sending secret keys: " + e + System.lineSeparator()); - - throw e; - } - } - - /** - * Clear the lazy element - * - * @throws TimeoutException If a timeout occurred while waiting for the element to be found - * @throws InterruptedException If the thread is interrupted while waiting for the element to be found - * @throws ExecutionFailedException If the clear action fails to execute - */ - public void clear() throws TimeoutException, InterruptedException, ExecutionFailedException { - this.getTestObject().getLogger().logMessage(MessageType.INFORMATION, String.format("Clear '%s'", this.userFriendlyName)); - WebElement element = GenericWait.waitFor(() -> this.getElement(this::getRawVisibleElement)); - this.executeEvent(element::clear, "Clear"); - } - - /** - * Submit the lazy element - * - * @throws TimeoutException If a timeout occurred while waiting for the element to be found - * @throws InterruptedException If the thread is interrupted while waiting for the element to be found - * @throws ExecutionFailedException If the submit action fails to execute - */ - public void submit() throws TimeoutException, InterruptedException, ExecutionFailedException { - this.getTestObject().getLogger().logMessage(MessageType.INFORMATION, String.format("Submit '%s'", this.userFriendlyName)); - WebElement element = GenericWait.waitFor(() -> this.getElement(this::getRawExistingElement)); - this.executeEvent(element::submit, "Submit"); - } - - /** - * Gets the value for the given attribute - * - * @param attributeName The name of the attribute - * @return The attribute - * @throws TimeoutException If a timeout occurred while waiting for the element to be found - * @throws InterruptedException If the thread is interrupted while waiting for the element to be found - */ - public String getAttribute(String attributeName) throws TimeoutException, InterruptedException { - this.getTestObject().getLogger().logMessage(MessageType.INFORMATION, String.format("Getting attribute '%s' for lazy element '%s'", attributeName, this.userFriendlyName)); - return GenericWait.waitFor(() -> this.getElement(this::getRawExistingElement).getAttribute(attributeName)); - } - - /** - * Gets the current value of an element - Useful for get input box text - * - * @return The value attribute - * @throws TimeoutException If a timeout occurred while waiting for the element to be found - * @throws InterruptedException If the thread is interrupted while waiting for the element to be found - */ - public String getValue() throws TimeoutException, InterruptedException { - this.getTestObject().getLogger().logMessage(MessageType.INFORMATION, String.format("Getting value for lazy element '%s'", this.userFriendlyName)); - return GenericWait.waitFor(() -> this.getElement(this::getRawVisibleElement).getAttribute("value")); - } - - /** - * Gets the CSS value for the given attribute - * - * @param propertyName The property name - * @return the css value for the property - * @throws TimeoutException If a timeout occurred while waiting for the element to be found - * @throws InterruptedException If the thread is interrupted while waiting for the element to be found - */ - public String getCssValue(String propertyName) throws TimeoutException, InterruptedException { - this.getTestObject().getLogger().logMessage(MessageType.INFORMATION, String.format("Getting '%s' css value for lazy element '%s'", propertyName, this.userFriendlyName)); - return GenericWait.waitFor(() -> this.getElement(this::getRawExistingElement).getCssValue(propertyName)); - } - - /** - * Wait for and get the visible web element - * - * @return The visible web element - */ - public WebElement getRawVisibleElement() { - Supplier<WebElement> elementSupplier; - - if (this.elementIndex == null) { - if (this.parent == null) { - elementSupplier = () -> { - UIWait waitDriver = UIWaitFactory.getWaitDriver(this.getTestObject().getWebDriver()); - return waitDriver.waitForVisibleElement(this.getBy()); - }; - } else { - elementSupplier = () -> { - WebElement parentElement = this.parent.getRawExistingElement(); - FluentWait<WebElement> fluentWait = FluentWaitFactory.getNewElementFluentWait(parentElement); - return fluentWait.until(e -> e.findElement(this.getBy())); - }; - } - - this.setCachedElement(elementSupplier); - } else { - elementSupplier = () -> this.getRawIndexed(WebElement::isDisplayed, "be visible"); - } - - this.setCachedElement(elementSupplier); - return this.getCachedElement(); - } - - /** - * Wait for and get the click-able web element - * - * @return The click-able web element - */ - public WebElement getRawClickableElement() { - - Supplier<WebElement> elementSupplier; - if (this.elementIndex == null) { - if (this.parent == null) { - elementSupplier = () -> { - UIWait waitDriver = UIWaitFactory.getWaitDriver(this.getTestObject().getWebDriver()); - return waitDriver.waitForClickableElement(this.getBy()); - }; - } else { - elementSupplier = () -> { - WebElement parentElement = this.getParent().getRawExistingElement(); - FluentWait<WebElement> fluentWait = FluentWaitFactory.getNewElementFluentWait(parentElement); - return fluentWait.until(e -> e.findElement(this.getBy())); - }; - } - } else { - elementSupplier = () -> this.getRawIndexed(e -> e.isDisplayed() && e.isEnabled(), "be visible"); - } - - this.setCachedElement(elementSupplier); - return this.getCachedElement(); - } - - /** - * Waits for and gets the existing web element - * - * @return The existing web element - */ - public WebElement getRawExistingElement() { - - Supplier<WebElement> elementSupplier; - - if (this.elementIndex == null) { - if (this.parent == null) { - elementSupplier = () -> { - UIWait waitDriver = UIWaitFactory.getWaitDriver(this.getTestObject().getWebDriver()); - return waitDriver.waitForPresentElement(this.getBy()); - }; - } else { - elementSupplier = () -> { - WebElement parentElement = this.getParent().getRawExistingElement(); - FluentWait<WebElement> fluentWait = FluentWaitFactory.getNewElementFluentWait(parentElement); - return fluentWait.until(e -> e.findElement(this.getBy())); - }; - } - } else { - elementSupplier = () -> this.getRawIndexed(Objects::nonNull, "exist"); - } - - this.setCachedElement(elementSupplier); + /** + * The index in cases where the selector finds multiple elements + */ + private final Integer elementIndex; + + /** + * A user friendly name, for logging purposes + */ + protected final String userFriendlyName; + + /** + * The parent lazy element + */ + protected LazyWebElement parent; + + /** + * The 'by' selector for the element + */ + protected By by; + + /** + * The test object for the element + */ + protected SeleniumTestObject testObject; + + /** + * Cached copy of the element or null if we haven't already found the element + */ + private WebElement cachedElement; + + /** + * Initializes a new instance of the {@link #AbstractLazyElement} class + * + * @param testObject The selenium test object + * @param locator The by locator to search on + * @param userFriendlyName The user friendly name of the lazy element + */ + protected AbstractLazyElement(SeleniumTestObject testObject, By locator, String userFriendlyName) { + this.testObject = testObject; + this.by = locator; + this.userFriendlyName = userFriendlyName; + this.elementIndex = null; + } + + /** + * Initializes a new instance of the {@link #AbstractLazyElement} class + * + * @param parent The parent lazy element + * @param locator The by locator to search on + * @param userFriendlyName The user friendly name of the lazy element + */ + protected AbstractLazyElement(LazyWebElement parent, By locator, String userFriendlyName) { + this.testObject = parent.getTestObject(); + this.parent = parent; + this.by = locator; + this.userFriendlyName = userFriendlyName; + this.elementIndex = null; + } + + /** + * Initializes a new instance of the {@link #AbstractLazyElement} class + * + * @param parent The parent lazy element + * @param locator THe by locator to search on + * @param userFriendlyName The user friendly name of the lazy element + * @param elementIndex The index of the cached element + * @param cachedElement The cached element + */ + protected AbstractLazyElement(LazyWebElement parent, By locator, String userFriendlyName, Integer elementIndex, + WebElement cachedElement) { + this.testObject = parent.getTestObject(); + this.by = locator; + this.userFriendlyName = userFriendlyName; + this.parent = parent; + this.elementIndex = elementIndex; + this.cachedElement = cachedElement; + } + + /** + * Gets the by selector + * + * @return the by + */ + public By getBy() { + return this.by; + } + + /** + * Gets the cached element + * + * @return the cachedElement + */ + public WebElement getCachedElement() { + return this.cachedElement; + } + + /** + * Sets the cached Element + * + * @param cachedElementFactory the cachedElement function to set + * the cached element + */ + private void setCachedElement(Supplier<WebElement> cachedElementFactory) { + this.cachedElement = cachedElementFactory.get(); + } + + /** + * Gets the user friendly name + * + * @return the userFriendlyName + */ + public String getUserFriendlyName() { + return this.userFriendlyName; + } + + /** + * Gets the parent of the lazy element + * + * @return the parent + */ + public LazyWebElement getParent() { + return this.parent; + } + + /** + * Gets the test object + * + * @return the test object + */ + public SeleniumTestObject getTestObject() { + return this.testObject; + } + + /** + * Gets the tag name of the lazy element + * + * @return The tag name + * @throws TimeoutException If a timeout occurred while waiting for the element to be found + * @throws InterruptedException If the thread is interrupted while waiting for the element to be found + */ + public String getTagName() throws TimeoutException, InterruptedException { + this.getTestObject().getLogger().logMessage(MessageType.INFORMATION, + String.format("Getting the tag name for lazy element '%s'", this.userFriendlyName)); + return GenericWait.waitFor(this.getElement(this::getRawExistingElement)::getTagName); + } + + /** + * Gets the lazy element's text + * + * @return The element text + * @throws TimeoutException If a timeout occurred while waiting for the element to be found + * @throws InterruptedException If the thread is interrupted while waiting for the element to be found + */ + public String getText() throws TimeoutException, InterruptedException { + this.getTestObject().getLogger().logMessage(MessageType.INFORMATION, + String.format("Getting the text for lazy element '%s'", this.userFriendlyName)); + return GenericWait.waitFor(this.getElement(this::getRawExistingElement)::getText); + } + + /** + * Gets the lazy element's location + * + * @return the location as a Point + * @throws TimeoutException If a timeout occurred while waiting for the element to be found + * @throws InterruptedException If the thread is interrupted while waiting for the element to be found + */ + public Point getLocation() throws TimeoutException, InterruptedException { + this.getTestObject().getLogger().logMessage(MessageType.INFORMATION, + String.format("Getting the location for lazy element '%s'", this.userFriendlyName)); + return GenericWait.waitFor(this.getElement(this::getRawExistingElement)::getLocation); + } + + /** + * Gets the lazy element's size + * + * @return The lazy element's size + * @throws TimeoutException If a timeout occurred while waiting for the element to be found + * @throws InterruptedException If the thread is interrupted while waiting for the element to be found + */ + public Dimension getSize() throws TimeoutException, InterruptedException { + this.getTestObject().getLogger().logMessage(MessageType.INFORMATION, + String.format("Getting the size of lazy element '%s'", this.userFriendlyName)); + return GenericWait.waitFor(this.getElement(this::getRawExistingElement)::getSize); + } + + /** + * Click the lazy element + * + * @throws TimeoutException If a timeout occurred while waiting for the element to be found + * @throws InterruptedException If the thread is interrupted while waiting for the element to be found + * @throws ExecutionFailedException If error occurs while sending keys + */ + public void click() throws TimeoutException, InterruptedException, ExecutionFailedException { + this.getTestObject().getLogger() + .logMessage(MessageType.INFORMATION, String.format("Click '%s'", this.userFriendlyName)); + WebElement element = GenericWait.waitFor(() -> this.getElement(this::getRawClickableElement)); + this.executeEvent(element::click, "Click"); + } + + /** + * Send Secret keys with no logging + * + * @param keys the secret keys + * @throws ExecutionFailedException If error occurs while sending keys + * @throws TimeoutException If a timeout occurred while waiting for the element to be found + * @throws InterruptedException If the thread is interrupted while waiting for the element to be found + */ + public void sendSecretKeys(String keys) throws ExecutionFailedException, TimeoutException, InterruptedException { + this.getTestObject().getLogger() + .logMessage(MessageType.VERBOSE, "Send secret keys to '%s'", this.getUserFriendlyName()); + WebElement element = GenericWait.waitFor(() -> this.getElement(this::getRawVisibleElement)); + + try { + this.getTestObject().getLogger().suspendLogging(); + this.executeEvent(() -> element.sendKeys(keys), "SendKeys"); + this.getTestObject().getLogger().continueLogging(); + } catch (ExecutionFailedException e) { + this.getTestObject().getLogger().continueLogging(); + this.getTestObject().getLogger() + .logMessage(MessageType.ERROR, "Exception during sending secret keys: " + e + System.lineSeparator()); + + throw e; + } + } + + /** + * Clear the lazy element + * + * @throws TimeoutException If a timeout occurred while waiting for the element to be found + * @throws InterruptedException If the thread is interrupted while waiting for the element to be found + * @throws ExecutionFailedException If the clear action fails to execute + */ + public void clear() throws TimeoutException, InterruptedException, ExecutionFailedException { + this.getTestObject().getLogger() + .logMessage(MessageType.INFORMATION, String.format("Clear '%s'", this.userFriendlyName)); + WebElement element = GenericWait.waitFor(() -> this.getElement(this::getRawVisibleElement)); + this.executeEvent(element::clear, "Clear"); + } + + /** + * Submit the lazy element + * + * @throws TimeoutException If a timeout occurred while waiting for the element to be found + * @throws InterruptedException If the thread is interrupted while waiting for the element to be found + * @throws ExecutionFailedException If the submit action fails to execute + */ + public void submit() throws TimeoutException, InterruptedException, ExecutionFailedException { + this.getTestObject().getLogger() + .logMessage(MessageType.INFORMATION, String.format("Submit '%s'", this.userFriendlyName)); + WebElement element = GenericWait.waitFor(() -> this.getElement(this::getRawExistingElement)); + this.executeEvent(element::submit, "Submit"); + } + + /** + * Gets the value for the given attribute + * + * @param attributeName The name of the attribute + * @return The attribute + * @throws TimeoutException If a timeout occurred while waiting for the element to be found + * @throws InterruptedException If the thread is interrupted while waiting for the element to be found + */ + public String getAttribute(String attributeName) throws TimeoutException, InterruptedException { + this.getTestObject().getLogger().logMessage(MessageType.INFORMATION, + String.format("Getting attribute '%s' for lazy element '%s'", attributeName, this.userFriendlyName)); + return GenericWait.waitFor(() -> this.getElement(this::getRawExistingElement).getAttribute(attributeName)); + } + + /** + * Gets the current value of an element - Useful for get input box text + * + * @return The value attribute + * @throws TimeoutException If a timeout occurred while waiting for the element to be found + * @throws InterruptedException If the thread is interrupted while waiting for the element to be found + */ + public String getValue() throws TimeoutException, InterruptedException { + this.getTestObject().getLogger().logMessage(MessageType.INFORMATION, + String.format("Getting value for lazy element '%s'", this.userFriendlyName)); + return GenericWait.waitFor(() -> this.getElement(this::getRawVisibleElement).getAttribute("value")); + } + + /** + * Gets the CSS value for the given attribute + * + * @param propertyName The property name + * @return the css value for the property + * @throws TimeoutException If a timeout occurred while waiting for the element to be found + * @throws InterruptedException If the thread is interrupted while waiting for the element to be found + */ + public String getCssValue(String propertyName) throws TimeoutException, InterruptedException { + this.getTestObject().getLogger().logMessage(MessageType.INFORMATION, + String.format("Getting '%s' css value for lazy element '%s'", propertyName, this.userFriendlyName)); + return GenericWait.waitFor(() -> this.getElement(this::getRawExistingElement).getCssValue(propertyName)); + } + + /** + * Wait for and get the visible web element + * + * @return The visible web element + */ + public WebElement getRawVisibleElement() { + Supplier<WebElement> elementSupplier; + + if (this.elementIndex == null) { + if (this.parent == null) { + elementSupplier = () -> { + UIWait waitDriver = UIWaitFactory.getWaitDriver(this.getTestObject().getWebDriver()); + return waitDriver.waitForVisibleElement(this.getBy()); + }; + } else { + elementSupplier = () -> { + WebElement parentElement = this.parent.getRawExistingElement(); + FluentWait<WebElement> fluentWait = FluentWaitFactory.getNewElementFluentWait(parentElement); + return fluentWait.until(e -> e.findElement(this.getBy())); + }; + } + + this.setCachedElement(elementSupplier); + } else { + elementSupplier = () -> this.getRawIndexed(WebElement::isDisplayed, "be visible"); + } + + this.setCachedElement(elementSupplier); + return this.getCachedElement(); + } + + /** + * Wait for and get the click-able web element + * + * @return The click-able web element + */ + public WebElement getRawClickableElement() { + + Supplier<WebElement> elementSupplier; + if (this.elementIndex == null) { + if (this.parent == null) { + elementSupplier = () -> { + UIWait waitDriver = UIWaitFactory.getWaitDriver(this.getTestObject().getWebDriver()); + return waitDriver.waitForClickableElement(this.getBy()); + }; + } else { + elementSupplier = () -> { + WebElement parentElement = this.getParent().getRawExistingElement(); + FluentWait<WebElement> fluentWait = FluentWaitFactory.getNewElementFluentWait(parentElement); + return fluentWait.until(e -> e.findElement(this.getBy())); + }; + } + } else { + elementSupplier = () -> this.getRawIndexed(e -> e.isDisplayed() && e.isEnabled(), "be visible"); + } + + this.setCachedElement(elementSupplier); + return this.getCachedElement(); + } + + /** + * Waits for and gets the existing web element + * + * @return The existing web element + */ + public WebElement getRawExistingElement() { + + Supplier<WebElement> elementSupplier; + + if (this.elementIndex == null) { + if (this.parent == null) { + elementSupplier = () -> { + UIWait waitDriver = UIWaitFactory.getWaitDriver(this.getTestObject().getWebDriver()); + return waitDriver.waitForPresentElement(this.getBy()); + }; + } else { + elementSupplier = () -> { + WebElement parentElement = this.getParent().getRawExistingElement(); + FluentWait<WebElement> fluentWait = FluentWaitFactory.getNewElementFluentWait(parentElement); + return fluentWait.until(e -> e.findElement(this.getBy())); + }; + } + } else { + elementSupplier = () -> this.getRawIndexed(Objects::nonNull, "exist"); + } + + this.setCachedElement(elementSupplier); + return this.getCachedElement(); + } + + /** + * Finds the first {@link WebElement WebElement} using the given method. + * + * @param by The locating mechanism to use + * @return the {@link WebElement WebElement} being found + * @throws TimeoutException If a timeout occurred while waiting for the element to be found + * @throws InterruptedException If the thread is interrupted while waiting for the element to be found + */ + public WebElement findRawElement(By by) throws TimeoutException, InterruptedException { + return GenericWait.waitFor(() -> this.getElement(this::getRawExistingElement).findElement(by)); + } + + /** + * Finds all {@link WebElement WebElement} within + * the current context using the given mechanism. + * + * @param by the locating mechanism to use + * @return the List of {@link WebElement WebElement} being found + * @throws TimeoutException If a timeout occurred while waiting for the element to be found + * @throws InterruptedException If the thread is interrupted while waiting for the element to be found + */ + public List<WebElement> findRawElements(By by) throws TimeoutException, InterruptedException { + return GenericWait.waitFor(() -> this.getElement(this::getRawExistingElement).findElements(by)); + } + + /** + * Sends the keys to the element + * + * @param keysToSend The keys being sent to the element + * @throws TimeoutException If a timeout occurred while waiting for the element to be found + * @throws InterruptedException If the thread is interrupted while waiting for the element to be found + * @throws ExecutionFailedException If the send keys action fails to execute + */ + public void sendKeys(CharSequence... keysToSend) + throws TimeoutException, InterruptedException, ExecutionFailedException { + StringBuilder keyBuilder = new StringBuilder(keysToSend.length); + + // building char sequence to String so that it can be logged + for (CharSequence cs : keysToSend) { + keyBuilder.append(cs); + } + + this.getTestObject().getLogger() + .logMessage(MessageType.VERBOSE, "Send keys '%s' to '%s'", keyBuilder.toString(), this.getUserFriendlyName()); + + WebElement element = GenericWait.waitFor(() -> this.getElement(this::getRawVisibleElement)); + this.executeEvent(() -> element.sendKeys(keysToSend), "SendKeys"); + } + + /** + * If the Element is selected + * + * @return If the element is selected + * @throws TimeoutException If a timeout occurred while waiting for the element to be found + * @throws InterruptedException If the thread is interrupted while waiting for the element to be found + */ + public boolean isSelected() throws TimeoutException, InterruptedException { + this.getTestObject().getLogger().logMessage(MessageType.INFORMATION, + String.format("Check to see if the lazy element %s is selected", this.userFriendlyName)); + return GenericWait.waitFor(this.getElement(this::getRawExistingElement)::isSelected); + } + + /** + * If the element is enabled + * + * @return If the element is enabled + * @throws TimeoutException If a timeout occurred while waiting for the element to be found + * @throws InterruptedException If the thread is interrupted while waiting for the element to be found + */ + public boolean isEnabled() throws TimeoutException, InterruptedException { + this.getTestObject().getLogger().logMessage(MessageType.INFORMATION, + String.format("Check to see if the lazy element %s is enabled", this.userFriendlyName)); + return GenericWait.waitFor(this.getElement(this::getRawExistingElement)::isEnabled); + } + + /** + * If the element is displayed + * + * @return If the element is displayed + * @throws TimeoutException If a timeout occurred while waiting for the element to be found + * @throws InterruptedException If the thread is interrupted while waiting for the element to be found + */ + public boolean isDisplayed() throws TimeoutException, InterruptedException { + this.getTestObject().getLogger().logMessage(MessageType.INFORMATION, + String.format("Check to see if the lazy element %s is displayed", this.userFriendlyName)); + return GenericWait.waitFor(this.getElement(this::getRawExistingElement)::isDisplayed); + } + + /** + * Gets the rectangle value that highlights the lazy element location + * + * @return The location and size of the element + * @throws TimeoutException If a timeout occurred while waiting for the element to be found + * @throws InterruptedException If the thread is interrupted while waiting for the element to be found + */ + public Rectangle getRect() throws TimeoutException, InterruptedException { + return new Rectangle(this.getLocation(), this.getSize()); + } + + /** + * Gets the screenshot as the target type + * + * @param target The target output type + * @return The type to get the screenshot as + * @throws TimeoutException If a timeout occurred while waiting for the element to be found + * @throws InterruptedException If the thread is interrupted while waiting for the element to be found + */ + public <X> X getScreenshotAs(OutputType<X> target) throws TimeoutException, InterruptedException { + return GenericWait.waitFor(() -> this.getElement(this::getRawExistingElement).getScreenshotAs(target)); + } + + /** + * Returns a string that represents the current object + * + * @return the lazy element string + */ + @Override + public String toString() { + String temp = ""; + + // Check if LazyElement has a parent + // If so, prefix parent's toString() + if (this.parent != null) { + temp += this.parent.toString(); + } + + return temp + this.by.toString() + this.userFriendlyName; + } + + /** + * Gets a web element using the provided factory if the cached element is + * null or if the cached element is stale + * + * @param getElement The function that gets the element + * @return The web element + * @throws NoSuchElementException If the element can not be found + */ + protected WebElement getElement(Supplier<WebElement> getElement) { + if (this.getCachedElement() != null) { + try { + this.getCachedElement().isDisplayed(); return this.getCachedElement(); - } - - /** - * Finds the first {@link WebElement WebElement} using the given method. - * - * @param by The locating mechanism to use - * @return the {@link WebElement WebElement} being found - * @throws TimeoutException If a timeout occurred while waiting for the element to be found - * @throws InterruptedException If the thread is interrupted while waiting for the element to be found - */ - public WebElement findRawElement(By by) throws TimeoutException, InterruptedException { - return GenericWait.waitFor(() -> this.getElement(this::getRawExistingElement).findElement(by)); - } - - /** - * Finds all {@link WebElement WebElement} within - * the current context using the given mechanism. - * - * @param by the locating mechanism to use - * @return the List of {@link WebElement WebElement} being found - * @throws TimeoutException If a timeout occurred while waiting for the element to be found - * @throws InterruptedException If the thread is interrupted while waiting for the element to be found - */ - public List<WebElement> findRawElements(By by) throws TimeoutException, InterruptedException { - return GenericWait.waitFor(() -> this.getElement(this::getRawExistingElement).findElements(by)); - } - - /** - * Sends the keys to the element - * - * @param keysToSend The keys being sent to the element - * @throws TimeoutException If a timeout occurred while waiting for the element to be found - * @throws InterruptedException If the thread is interrupted while waiting for the element to be found - * @throws ExecutionFailedException If the send keys action fails to execute - */ - public void sendKeys(CharSequence... keysToSend) - throws TimeoutException, InterruptedException, ExecutionFailedException { - StringBuilder keyBuilder = new StringBuilder(keysToSend.length); - - // building char sequence to String so that it can be logged - for (CharSequence cs : keysToSend) { - keyBuilder.append(cs); - } - + } catch (Exception e) { this.getTestObject().getLogger() - .logMessage(MessageType.VERBOSE, "Send keys '%s' to '%s'", keyBuilder.toString(), this.getUserFriendlyName()); - - WebElement element = GenericWait.waitFor(() -> this.getElement(this::getRawVisibleElement)); - this.executeEvent(() -> element.sendKeys(keysToSend), "SendKeys"); - } - - /** - * If the Element is selected - * - * @return If the element is selected - * @throws TimeoutException If a timeout occurred while waiting for the element to be found - * @throws InterruptedException If the thread is interrupted while waiting for the element to be found - */ - public boolean isSelected() throws TimeoutException, InterruptedException { - this.getTestObject().getLogger().logMessage(MessageType.INFORMATION, String.format("Check to see if the lazy element %s is selected", this.userFriendlyName)); - return GenericWait.waitFor(this.getElement(this::getRawExistingElement)::isSelected); - } - - /** - * If the element is enabled - * - * @return If the element is enabled - * @throws TimeoutException If a timeout occurred while waiting for the element to be found - * @throws InterruptedException If the thread is interrupted while waiting for the element to be found - */ - public boolean isEnabled() throws TimeoutException, InterruptedException { - this.getTestObject().getLogger().logMessage(MessageType.INFORMATION, String.format("Check to see if the lazy element %s is enabled", this.userFriendlyName)); - return GenericWait.waitFor(this.getElement(this::getRawExistingElement)::isEnabled); - } - - /** - * If the element is displayed - * - * @return If the element is displayed - * @throws TimeoutException If a timeout occurred while waiting for the element to be found - * @throws InterruptedException If the thread is interrupted while waiting for the element to be found - */ - public boolean isDisplayed() throws TimeoutException, InterruptedException { - this.getTestObject().getLogger().logMessage(MessageType.INFORMATION, String.format("Check to see if the lazy element %s is displayed", this.userFriendlyName)); - return GenericWait.waitFor(this.getElement(this::getRawExistingElement)::isDisplayed); - } - - /** - * Gets the rectangle value that highlights the lazy element location - * - * @return The location and size of the element - * @throws TimeoutException If a timeout occurred while waiting for the element to be found - * @throws InterruptedException If the thread is interrupted while waiting for the element to be found - */ - public Rectangle getRect() throws TimeoutException, InterruptedException { - return new Rectangle(this.getLocation(), this.getSize()); - } - - /** - * Gets the screenshot as the target type - * - * @param target The target output type - * @return The type to get the screenshot as - * @throws TimeoutException If a timeout occurred while waiting for the element to be found - * @throws InterruptedException If the thread is interrupted while waiting for the element to be found - */ - public <X> X getScreenshotAs(OutputType<X> target) throws TimeoutException, InterruptedException { - return GenericWait.waitFor(() -> this.getElement(this::getRawExistingElement).getScreenshotAs(target)); - } - - /** - * Returns a string that represents the current object - * - * @return the lazy element string - */ - @Override - public String toString() { - String temp = ""; - - // Check if LazyElement has a parent - // If so, prefix parent's toString() - if (this.parent != null) { - temp += this.parent.toString(); - } - - return temp + this.by.toString() + this.userFriendlyName; - } - - /** - * Gets a web element using the provided factory if the cached element is - * null or if the cached element is stale - * - * @param getElement The function that gets the element - * @return The web element - * @throws NoSuchElementException If the element can not be found - */ - protected WebElement getElement(Supplier<WebElement> getElement) { - if (this.getCachedElement() != null) { - try { - this.getCachedElement().isDisplayed(); - return this.getCachedElement(); - } catch (Exception e) { - this.getTestObject().getLogger().logMessage(MessageType.VERBOSE, "Refinding element because: " + e.getMessage()); - } - } - - try { - this.getTestObject().getLogger().logMessage(MessageType.VERBOSE, "Performing lazy driver find on: " + this.getBy()); - this.setCachedElement(getElement); - return this.getCachedElement(); - } catch (NoSuchElementException nsee) { - StringBuilder messageBuilder = new StringBuilder(); - - messageBuilder.append("Failed to find: " + this.userFriendlyName + System.lineSeparator()); - messageBuilder.append("Locator: " + this.getBy() + System.lineSeparator()); - messageBuilder.append("Because: " + nsee.getMessage() + System.lineSeparator()); - - throw new NoSuchElementException(messageBuilder.toString(), nsee); - } - } - - /** - * Execute an element action - * - * @param elementAction the element action - * @param caller Text to identify the caller function - * @throws ExecutionFailedException If the elementAction fails to execute - */ - private void executeEvent(Action elementAction, String caller) throws ExecutionFailedException { - try { - this.getTestObject().getLogger().logMessage(MessageType.VERBOSE, "Performing lazy driver action: " + caller); - elementAction.invoke(); - } catch (Exception e) { - StringBuilder messageBuilder = new StringBuilder(); - - messageBuilder.append("Failed to " + caller + ": " + this.userFriendlyName + System.lineSeparator()); - messageBuilder.append("Locator: " + this.getBy() + System.lineSeparator()); - messageBuilder.append("Because: " + e.getMessage() + System.lineSeparator()); - - throw new ExecutionFailedException(messageBuilder.toString(), e); - } - } - - /** - * Gets the element at the indexed value - * - * @param matchesState The predicate to see that the element found at the index matches the state - * we expect - * @param expectedState The expected state to be logged if the matchesState function returns false - * @return The WebElement at the indexed value - * @throws TimeoutException If a timeout occurred while waiting for the element to be found - * @throws InterruptedException If the thread is interrupted while waiting for the element to be found - */ - private WebElement getRawIndexed(Predicate<WebElement> matchesState, String expectedState) { - List<WebElement> elements = (this.parent == null) ? - this.getTestObject().getWebDriver().findElements(this.by) : - this.parent.getRawExistingElement().findElements(by); - WebElement indexedElement = elements.get(this.elementIndex == null ? 0 : this.elementIndex); - - if (!matchesState.test(indexedElement)) { - throw new InvalidElementStateException(String.format("Expected element to %s", expectedState)); - } - - return indexedElement; - } - - /** - * Checks if the element exists - * - * @return If the element exists in the current page state - * @throws TimeoutException If a timeout occurred while waiting for the element to be found - * @throws InterruptedException If the thread is interrupted while waiting for the element to be found - */ - public boolean doesExist() throws TimeoutException, InterruptedException { - return GenericWait.waitFor(() -> { - this.getElement(this::getRawExistingElement); - return true; - }); - } + .logMessage(MessageType.VERBOSE, "Refinding element because: " + e.getMessage()); + } + } + + try { + this.getTestObject().getLogger() + .logMessage(MessageType.VERBOSE, "Performing lazy driver find on: " + this.getBy()); + this.setCachedElement(getElement); + return this.getCachedElement(); + } catch (NoSuchElementException nsee) { + StringBuilder messageBuilder = new StringBuilder(); + + messageBuilder.append("Failed to find: " + this.userFriendlyName + System.lineSeparator()); + messageBuilder.append("Locator: " + this.getBy() + System.lineSeparator()); + messageBuilder.append("Because: " + nsee.getMessage() + System.lineSeparator()); + + throw new NoSuchElementException(messageBuilder.toString(), nsee); + } + } + + /** + * Execute an element action + * + * @param elementAction the element action + * @param caller Text to identify the caller function + * @throws ExecutionFailedException If the elementAction fails to execute + */ + private void executeEvent(Action elementAction, String caller) throws ExecutionFailedException { + try { + this.getTestObject().getLogger().logMessage(MessageType.VERBOSE, "Performing lazy driver action: " + caller); + elementAction.invoke(); + } catch (Exception e) { + StringBuilder messageBuilder = new StringBuilder(); + + messageBuilder.append("Failed to " + caller + ": " + this.userFriendlyName + System.lineSeparator()); + messageBuilder.append("Locator: " + this.getBy() + System.lineSeparator()); + messageBuilder.append("Because: " + e.getMessage() + System.lineSeparator()); + + throw new ExecutionFailedException(messageBuilder.toString(), e); + } + } + + /** + * Gets the element at the indexed value + * + * @param matchesState The predicate to see that the element found at the index matches the state + * we expect + * @param expectedState The expected state to be logged if the matchesState function returns false + * @return The WebElement at the indexed value + * @throws TimeoutException If a timeout occurred while waiting for the element to be found + * @throws InterruptedException If the thread is interrupted while waiting for the element to be found + */ + private WebElement getRawIndexed(Predicate<WebElement> matchesState, String expectedState) { + List<WebElement> elements = (this.parent == null) + ? this.getTestObject().getWebDriver().findElements(this.by) + : this.parent.getRawExistingElement().findElements(by); + WebElement indexedElement = elements.get(this.elementIndex == null ? 0 : this.elementIndex); + + if (!matchesState.test(indexedElement)) { + throw new InvalidElementStateException(String.format("Expected element to %s", expectedState)); + } + + return indexedElement; + } + + /** + * Checks if the element exists + * + * @return If the element exists in the current page state + * @throws TimeoutException If a timeout occurred while waiting for the element to be found + * @throws InterruptedException If the thread is interrupted while waiting for the element to be found + */ + public boolean doesExist() throws TimeoutException, InterruptedException { + return GenericWait.waitFor(() -> { + this.getElement(this::getRawExistingElement); + return true; + }); + } } diff --git a/jmaqs-selenium/src/main/java/com/magenic/jmaqs/selenium/BaseSeleniumPageModel.java b/jmaqs-selenium/src/main/java/com/magenic/jmaqs/selenium/BaseSeleniumPageModel.java index 622d7f983..15e5292b6 100644 --- a/jmaqs-selenium/src/main/java/com/magenic/jmaqs/selenium/BaseSeleniumPageModel.java +++ b/jmaqs-selenium/src/main/java/com/magenic/jmaqs/selenium/BaseSeleniumPageModel.java @@ -16,20 +16,20 @@ */ public abstract class BaseSeleniumPageModel { - /** - * The lazy Element store - */ - private final HashMap<String, LazyWebElement> lazyElementStore; + /** + * The lazy Element store + */ + private final HashMap<String, LazyWebElement> lazyElementStore; - /** - * The selenium test object - */ - private final SeleniumTestObject testObject; + /** + * The selenium test object + */ + private final SeleniumTestObject testObject; - /** - * The web driver - */ - private WebDriver webDriver; + /** + * The web driver + */ + private WebDriver webDriver; /** * Instantiates a new Base selenium page model. @@ -51,14 +51,14 @@ protected Logger getLogger() { return this.testObject.getLogger(); } - /** - * Gets the lazy element store - * - * @return The lazy element store - */ - protected HashMap<String, LazyWebElement> getLazyElementStore() { - return this.lazyElementStore; - } + /** + * Gets the lazy element store + * + * @return The lazy element store + */ + protected HashMap<String, LazyWebElement> getLazyElementStore() { + return this.lazyElementStore; + } /** * Gets test object. @@ -87,14 +87,14 @@ public void setWebDriver(WebDriver webDriver) { this.webDriver = webDriver; } - /** - * Gets the per timer collection - * - * @return The perf timer colection - */ - public PerfTimerCollection getPerfTimerCollection() { - return this.testObject.getPerfTimerCollection(); - } + /** + * Gets the per timer collection + * + * @return The perf timer colection + */ + public PerfTimerCollection getPerfTimerCollection() { + return this.testObject.getPerfTimerCollection(); + } /** * Is page loaded boolean. @@ -103,12 +103,12 @@ public PerfTimerCollection getPerfTimerCollection() { */ public abstract boolean isPageLoaded(); - /** - * Waits for the page to load - */ - public void waitForPageLoad() { - UIWaitFactory.getWaitDriver(getWebDriver()).waitForPageLoad(); - } + /** + * Waits for the page to load + */ + public void waitForPageLoad() { + UIWaitFactory.getWaitDriver(getWebDriver()).waitForPageLoad(); + } /** * Gets lazy element. diff --git a/jmaqs-selenium/src/main/java/com/magenic/jmaqs/selenium/BaseSeleniumTest.java b/jmaqs-selenium/src/main/java/com/magenic/jmaqs/selenium/BaseSeleniumTest.java index 24985b001..47a708b7e 100644 --- a/jmaqs-selenium/src/main/java/com/magenic/jmaqs/selenium/BaseSeleniumTest.java +++ b/jmaqs-selenium/src/main/java/com/magenic/jmaqs/selenium/BaseSeleniumTest.java @@ -35,7 +35,7 @@ public WebDriver getWebDriver() { * Sets web driver. * * @param webDriver the web driver - * @throws Exception + * @throws Exception exception */ public void setWebDriver(WebDriver webDriver) throws Exception { this.getTestObject().setWebDriver(webDriver); @@ -73,11 +73,10 @@ protected WebDriver getBrowser() throws Exception { @Override protected void createNewTestObject() { try { - this.setTestObject(new SeleniumTestObject(this.getBrowser(), this.createLogger(), - this.getFullyQualifiedTestClassName())); + this.setTestObject( + new SeleniumTestObject(this.getBrowser(), this.createLogger(), this.getFullyQualifiedTestClassName())); } catch (Exception e) { - getLogger().logMessage( - StringProcessor.safeFormatter("Test Object could not be created: %s", e.getMessage())); + getLogger().logMessage(StringProcessor.safeFormatter("Test Object could not be created: %s", e.getMessage())); } } } \ No newline at end of file diff --git a/jmaqs-selenium/src/main/java/com/magenic/jmaqs/selenium/LazyWebElement.java b/jmaqs-selenium/src/main/java/com/magenic/jmaqs/selenium/LazyWebElement.java index 141d19a4b..ada33adaa 100644 --- a/jmaqs-selenium/src/main/java/com/magenic/jmaqs/selenium/LazyWebElement.java +++ b/jmaqs-selenium/src/main/java/com/magenic/jmaqs/selenium/LazyWebElement.java @@ -1,10 +1,12 @@ +/* + * Copyright 2020 (C) Magenic, All rights Reserved + */ + package com.magenic.jmaqs.selenium; +import com.magenic.jmaqs.utilities.helper.exceptions.TimeoutException; import java.util.ArrayList; import java.util.List; - -import com.magenic.jmaqs.utilities.helper.exceptions.TimeoutException; - import org.openqa.selenium.By; import org.openqa.selenium.WebElement; @@ -13,68 +15,73 @@ */ public class LazyWebElement extends AbstractLazyElement { - /** - * Initializes a new instance of the {@link #LazyWebElement} class - * @param testObject The selenium test object - * @param locator The by locator to search on - * @param userFriendlyName The user friendly name of the lazy element - */ - public LazyWebElement(SeleniumTestObject testObject, By locator, String userFriendlyName) { - super(testObject, locator, userFriendlyName); - } - - /** - * Initializes a new instance of the {@link #LazyWebElement} class - * @param parent The parent lazy element - * @param locator The by locator to search on - * @param userFriendlyName The user friendly name of the lazy element - */ - public LazyWebElement(LazyWebElement parent, By locator, String userFriendlyName) { - super(parent, locator, userFriendlyName); - } + /** + * Initializes a new instance of the {@link #LazyWebElement} class + * + * @param testObject The selenium test object + * @param locator The by locator to search on + * @param userFriendlyName The user friendly name of the lazy element + */ + public LazyWebElement(SeleniumTestObject testObject, By locator, String userFriendlyName) { + super(testObject, locator, userFriendlyName); + } - /** - * Initializes a new instance of the {@link #LazyWebElement} class - * @param parent The parent lazy element - * @param locator THe by locator to search on - * @param userFriendlyName The user friendly name of the lazy element - * @param index The index of the cached element if cached using the {@link #findElements(By)} method - * @param cachedElement The cached element - */ - public LazyWebElement(LazyWebElement parent, By locator, String userFriendlyName, Integer index, WebElement cachedElement) { - super(parent, locator, userFriendlyName, index, cachedElement); - } + /** + * Initializes a new instance of the {@link #LazyWebElement} class + * + * @param parent The parent lazy element + * @param locator The by locator to search on + * @param userFriendlyName The user friendly name of the lazy element + */ + public LazyWebElement(LazyWebElement parent, By locator, String userFriendlyName) { + super(parent, locator, userFriendlyName); + } + /** + * Initializes a new instance of the {@link #LazyWebElement} class + * + * @param parent The parent lazy element + * @param locator THe by locator to search on + * @param userFriendlyName The user friendly name of the lazy element + * @param index The index of the cached element if cached using the {@link #findElements(By)} method + * @param cachedElement The cached element + */ + public LazyWebElement(LazyWebElement parent, By locator, String userFriendlyName, Integer index, + WebElement cachedElement) { + super(parent, locator, userFriendlyName, index, cachedElement); + } - /** - * Find the WebElement using the by and then maps it to the LazyWebElement - * @param locator The by locator - * @return The LazyWebElement of the element found - * @throws TimeoutException If a timeout occurred while waiting for the element to be found - * @throws InterruptedException If the thread is interrupted while waiting for the element to be found - */ - public LazyWebElement findElement(By locator) throws TimeoutException, InterruptedException { - WebElement elementFound = this.findRawElement(locator); - return new LazyWebElement(this, locator, this.userFriendlyName, null, elementFound); - } + /** + * Find the WebElement using the by and then maps it to the LazyWebElement + * + * @param locator The by locator + * @return The LazyWebElement of the element found + * @throws TimeoutException If a timeout occurred while waiting for the element to be found + * @throws InterruptedException If the thread is interrupted while waiting for the element to be found + */ + public LazyWebElement findElement(By locator) throws TimeoutException, InterruptedException { + WebElement elementFound = this.findRawElement(locator); + return new LazyWebElement(this, locator, this.userFriendlyName, null, elementFound); + } - /** - * Find the collection of WebElements and then map them to LazyWebElements - * @param locator The by locator - * @return The collection of WebElements mapped to LazyWebElements - * @throws TimeoutException If a timeout occurred while waiting for the element to be found - * @throws InterruptedException If the thread is interrupted while waiting for the element to be found - */ - public List<LazyWebElement> findElements(By locator) throws TimeoutException, InterruptedException { - int index = 0; - List<LazyWebElement> elements = new ArrayList<>(); + /** + * Find the collection of WebElements and then map them to LazyWebElements + * + * @param locator The by locator + * @return The collection of WebElements mapped to LazyWebElements + * @throws TimeoutException If a timeout occurred while waiting for the element to be found + * @throws InterruptedException If the thread is interrupted while waiting for the element to be found + */ + public List<LazyWebElement> findElements(By locator) throws TimeoutException, InterruptedException { + int index = 0; + List<LazyWebElement> elements = new ArrayList<>(); - for (WebElement element : this.findRawElements(locator)) { - elements.add(new LazyWebElement( - this, locator, String.format("%s - %d", this.userFriendlyName, index), index, element)); - index++; - } - - return elements; + for (WebElement element : this.findRawElements(locator)) { + elements.add( + new LazyWebElement(this, locator, String.format("%s - %d", this.userFriendlyName, index), index, element)); + index++; } + + return elements; + } } diff --git a/jmaqs-selenium/src/main/java/com/magenic/jmaqs/selenium/SeleniumTestObject.java b/jmaqs-selenium/src/main/java/com/magenic/jmaqs/selenium/SeleniumTestObject.java index 9d92dc8a8..61106afe5 100644 --- a/jmaqs-selenium/src/main/java/com/magenic/jmaqs/selenium/SeleniumTestObject.java +++ b/jmaqs-selenium/src/main/java/com/magenic/jmaqs/selenium/SeleniumTestObject.java @@ -21,11 +21,10 @@ public class SeleniumTestObject extends BaseTestObject { * @param logger the logger * @param fullyQualifiedTestName the fully qualified test name */ - public SeleniumTestObject(Supplier<WebDriver> getDriverSupplier, Logger logger, - String fullyQualifiedTestName) { + public SeleniumTestObject(Supplier<WebDriver> getDriverSupplier, Logger logger, String fullyQualifiedTestName) { super(logger, fullyQualifiedTestName); - this.getManagerStore().put((SeleniumDriverManager.class).getCanonicalName(), - new SeleniumDriverManager(getDriverSupplier, this)); + this.getManagerStore() + .put((SeleniumDriverManager.class).getCanonicalName(), new SeleniumDriverManager(getDriverSupplier, this)); } /** @@ -37,8 +36,8 @@ public SeleniumTestObject(Supplier<WebDriver> getDriverSupplier, Logger logger, */ public SeleniumTestObject(WebDriver webDriver, Logger logger, String fullyQualifiedTestName) { super(logger, fullyQualifiedTestName); - this.getManagerStore().put((SeleniumDriverManager.class).getCanonicalName(), - new SeleniumDriverManager((() -> webDriver), this)); + this.getManagerStore() + .put((SeleniumDriverManager.class).getCanonicalName(), new SeleniumDriverManager((() -> webDriver), this)); } /** @@ -56,27 +55,24 @@ public WebDriver getWebDriver() { * @return the web manager */ public SeleniumDriverManager getWebManager() { - return (SeleniumDriverManager) this.getManagerStore() - .get(SeleniumDriverManager.class.getCanonicalName()); + return (SeleniumDriverManager) this.getManagerStore().get(SeleniumDriverManager.class.getCanonicalName()); } /** * Sets web driver. * * @param driver the driver - * @throws Exception + * @throws Exception exception */ public void setWebDriver(WebDriver driver) throws Exception { - + String name = SeleniumDriverManager.class.getCanonicalName(); - if(this.getManagerStore().containsKey(name)) - { + if (this.getManagerStore().containsKey(name)) { this.getManagerStore().get(name).close(); this.getManagerStore().remove(name); } - - this.getManagerStore().put(name, - new SeleniumDriverManager((() -> driver), this)); + + this.getManagerStore().put(name, new SeleniumDriverManager((() -> driver), this)); } /** @@ -85,8 +81,8 @@ public void setWebDriver(WebDriver driver) throws Exception { * @param webDriverSupplier the web driver supplier */ public void setWebDriver(Supplier<WebDriver> webDriverSupplier) { - this.getManagerStore().put(SeleniumDriverManager.class.getCanonicalName(), - new SeleniumDriverManager(webDriverSupplier, this)); + this.getManagerStore() + .put(SeleniumDriverManager.class.getCanonicalName(), new SeleniumDriverManager(webDriverSupplier, this)); } } diff --git a/jmaqs-selenium/src/main/java/com/magenic/jmaqs/selenium/UIWait.java b/jmaqs-selenium/src/main/java/com/magenic/jmaqs/selenium/UIWait.java index de6ae471b..554ed11e0 100644 --- a/jmaqs-selenium/src/main/java/com/magenic/jmaqs/selenium/UIWait.java +++ b/jmaqs-selenium/src/main/java/com/magenic/jmaqs/selenium/UIWait.java @@ -7,11 +7,9 @@ import com.magenic.jmaqs.selenium.factories.FluentWaitFactory; import com.magenic.jmaqs.utilities.helper.Config; import com.magenic.jmaqs.utilities.helper.ConfigSection; - import java.text.MessageFormat; import java.util.List; import java.util.function.Function; - import org.openqa.selenium.By; import org.openqa.selenium.ElementNotInteractableException; import org.openqa.selenium.JavascriptExecutor; @@ -35,1102 +33,1035 @@ */ public class UIWait { - /** The default value of the Header size... Your header's size may be different. */ - private static final int HEADER_SIZE = 90; - - /** The default value of the page's body. Your page might have a different Header value. */ - private static final By BODY_BY = By.cssSelector("BODY"); - - /** The Webdriver that the test is currently running on. */ - private WebDriver driver; - - /** The retry time. */ - private int fluentRetryTime; - - /** The timeout time. */ - private int timeout; - - /** The web driver wait that the test is currently running on. */ - private WebDriverWait waitDriver; - - /** - * Constructor for {@link UIWait} object. - * - * @param driver WebDriver - */ - public UIWait(WebDriver driver) { - this(driver, - Integer.parseInt(Config.getValueForSection(ConfigSection.SeleniumMaqs, "BrowserTimeout", "30000")), - Integer.parseInt(Config.getValueForSection(ConfigSection.SeleniumMaqs, "BrowserWaitTime", "1000")), - null); - } - - public UIWait(WebDriver driver, WebDriverWait waitDriver) { - this ( - driver, - Integer.parseInt(Config.getValueForSection(ConfigSection.SeleniumMaqs, "BrowserTimeout", "30000")), - Integer.parseInt(Config.getValueForSection(ConfigSection.SeleniumMaqs, "BrowserWaitTime", "1000")), - waitDriver); - } - - /** - * Constructor for SeleniumWait object. - * - * @param driver The WebDriver - * @param timeOutInSeconds int value of the total time to wait until timing out - * @param fluentRetryTime int value of seconds to use for fluent retry - */ - public UIWait(WebDriver driver, final int timeOutInSeconds, final int fluentRetryTime, WebDriverWait webDriverWait) { - this.driver = driver; - this.timeout = timeOutInSeconds; - this.fluentRetryTime = fluentRetryTime; - this.setWaitDriver( - webDriverWait == null ? - this.getNewWaitDriver() : - webDriverWait); - } - - /** - * Get the WebDriverWait for use outside of this instance class. - * - * @return The WebDriverWait - */ - public WebDriverWait getWaitDriver() { - return this.waitDriver; - } - - /** - * Sets the WebDriverWait. - * - * @param waiter The WebDriverWait - */ - public void setWaitDriver(WebDriverWait waiter) { - this.waitDriver = waiter; - } - - /** - * Resets wait default wait driver. - * - * @return The WebDriverWait - */ - public WebDriverWait resetWaitDriver() { - WebDriverWait wait = this.getNewWaitDriver(); - this.setWaitDriver(wait); - return wait; - } - - /** - * Waits until the element is present. - * - * @param by The by selector - * @return Returns the element if present - */ - public WebElement waitForPresentElement(By by) { - return this.waitForPresentElement(by, this.getWaitDriver()); - } - - /** - * Waits until the element is present. - * - * @param by The by selector - * @param timeOutInMillis The timeout in milliseconds - * @param sleepInMillis the number of milliseconds to wait before a recheck - * @return Returns the element if present - */ - public WebElement waitForPresentElement( - By by, - final int timeOutInMillis, - final int sleepInMillis) { - WebDriverWait wait = this.getNewWaitDriver(timeOutInMillis, sleepInMillis); - return wait.until(ExpectedConditions.presenceOfElementLocated(by)); - } - - /** - * Waits For the element to be present. - * - * @param by The by selector - * @param wait The wait driver - * @return Returns the element if present - */ - public WebElement waitForPresentElement(By by, WebDriverWait wait) { - return wait.until(ExpectedConditions.presenceOfElementLocated(by)); - } - - /** - * Wait for the specified element to be visible on the pages DOM. The first element located with - * the specified By value is returned. - * - * @param by Selector to wait for, and return - * @return WebElement - first one found with by - */ - public WebElement waitForVisibleElement(final By by) { - return this.waitForVisibleElement(by, getWaitDriver()); - } - - /** - * Wait for the specified element to be visible on the pages DOM. The first element located with - * the specified By value is returned. - * - * @param by Selector to wait for, and return - * @param timeOutInMillis the number of milliseconds to wait before failing - * @param sleepInMillis the number of milliseconds to wait before a recheck - * @return WebElement - first one found with by. Null - */ - public WebElement waitForVisibleElement( - final By by, - final int timeOutInMillis, - final int sleepInMillis) { - WebDriverWait wait = this.getNewWaitDriver(timeOutInMillis, sleepInMillis); - return this.waitForVisibleElement(by, wait); - } - - /** - * Wait for the specified element to be visible on the pages DOM. The first element located with - * the specified By value is returned. - * - * @param by Selector to wait for, and return - * @param wait The wait driver - * @return WebElement - first one found with by. Null - */ - public WebElement waitForVisibleElement( - final By by, - WebDriverWait wait) { - try { - return wait.until(ExpectedConditions.visibilityOfElementLocated(by)); - } - catch (TimeoutException | NoSuchElementException e) { - return null; - } - } - - /** - * Wait until the element exists and is visible. - * - * @param by Selector to wait for - * @return boolean true if element is found - */ - public boolean waitUntilVisibleElement(final By by) { - return waitUntilVisibleElement(by, this.getWaitDriver()); - } - - /** - * Wait until the element exists and is visible. - * - * @param by Selector to wait for - * @param timeOutInMillis the number of milliseconds to wait before failing - * @param sleepInMillis the number of milliseconds to wait before a recheck - * @return boolean true if element is found - */ - public boolean waitUntilVisibleElement(final By by, - final int timeOutInMillis, - final int sleepInMillis) { - WebDriverWait wait = this.getNewWaitDriver(this.driver, timeOutInMillis, sleepInMillis); - return waitUntilVisibleElement(by, wait); - } - - /** - * Wait until the element exists and is visible. - * - * @param by Selector to wait for - * @param wait The wait driver - * @return boolean true if element is found - */ - public boolean waitUntilVisibleElement(final By by, WebDriverWait wait) { - try { - return waitForVisibleElement(by, wait) != null; - } catch (NoSuchElementException | StaleElementReferenceException | TimeoutException e) { - return false; - } - } - - /** - * Wait for the specified element to be present and enabled. The first element located with - * the specified By value is returned. - * - * @param by Selector to wait for, and return - * @return WebElement - first one found with by. Null - */ - public WebElement waitForEnabledElement(final By by) { - return waitForEnabledElement(by, this.timeout, this.fluentRetryTime); - } - - /** - * Wait for the specified element to be present and enabled. The first element located with - * the specified By value is returned. - * - * @param by Selector to wait for, and return - * @param timeOutInMillis - the number of milliseconds to wait before failing - * @param sleepInMillis - the number of milliseconds to wait before a recheck - * @return WebElement - first one found with by. Null - */ - public WebElement waitForEnabledElement(final By by, - final int timeOutInMillis, - final int sleepInMillis) { - if (waitUntilEnabledElement(by, timeOutInMillis, sleepInMillis)) { - return this.driver.findElement(by); - } - - throw new ElementNotInteractableException("Element is not enabled"); - } - - /** - * Wait until the specified element to be present and enabled. - * - * @param by - Selector to wait for, and return - * @return boolean true if element is found, else false - */ - public boolean waitUntilEnabledElement(final By by) { - return waitUntilEnabledElement(by, this.timeout, this.fluentRetryTime); - } - - /** - * Wait for the specified element to be present and enabled. - * - * @param by Selector to wait for, and return - * @param timeOutInMillis the number of milliseconds to wait before failing - * @param sleepInMillis the number of milliseconds to wait before a recheck - * @return boolean true if element is found, else false - */ - public boolean waitUntilEnabledElement( - final By by, - final int timeOutInMillis, - final int sleepInMillis) { - - WebElement element = this.waitForVisibleElement(by); - FluentWait<WebElement> fluentWait = FluentWaitFactory.getNewElementFluentWait(element, timeOutInMillis, sleepInMillis); - - Function<WebElement, Boolean> function = obj -> { - try { - return obj.isEnabled(); - } catch (NoSuchElementException | StaleElementReferenceException e) { - // Do not throw these exceptions here. - // Instead return false and let the fluentwait try again. - return false; - } - }; - - try { - return fluentWait.until(function); - } catch (TimeoutException e) { - return false; - } - } - - /** - * Waits until the element is disabled. - * - * @param by the By selector - * @return returns true if the element is disabled, else false - */ - public boolean waitUntilDisabledElement(By by) { - return waitUntilDisabledElement(by, this.timeout, this.fluentRetryTime); - } - - /** - * Waits until the element is disabled. - * - * @param by the web element - * @param timeOutInMillis he number of milliseconds to wait before failing - * @param sleepInMillis the number of milliseconds to wait before a recheck - * @return returns true if the element is disabled, else false - */ - public boolean waitUntilDisabledElement(By by, - final int timeOutInMillis, - final int sleepInMillis) { - try { - WebElement element = this.waitForVisibleElement(by, timeOutInMillis, sleepInMillis); - FluentWait<WebElement> fluentWait = FluentWaitFactory.getNewElementFluentWait(element, timeOutInMillis, sleepInMillis); - return fluentWait.until(obj -> !obj.isEnabled()); - } catch (NoSuchElementException | StaleElementReferenceException e) { - return true; - } catch (TimeoutException e) { - return false; - } - } - - /** - * Wait until the element is not displayed or visible. - * - * @param by Selector to not be displayed or visible - */ - public void waitForAbsentElement(final By by) { - this.waitForAbsentElement(by, this.timeout, this.fluentRetryTime); - } - - /** - * Wait until the element is not displayed or visible. - * - * @param by Selector to not be displayed or visible - */ - public void waitForAbsentElement(final By by, - final int timeOutInMillis, - final int sleepInMillis) { - boolean isAbsent = this.waitUntilAbsentElement(by, timeOutInMillis, sleepInMillis); - - if (!isAbsent) { - throw new TimeoutException(MessageFormat.format("Element with selector {0} is not absent", by)); - } - } - - /** - * Wait until the element is not displayed or visible. - * - * @param by Selector to not be displayed or visible - */ - public boolean waitUntilAbsentElement(final By by) { - return this.waitUntilAbsentElement(by, this.timeout, this.fluentRetryTime); - } - - /** - * Wait until the element is not displayed or visible. - * - * @param by element to not be displayed or visible - * @param timeOutInMillis the number of milliseconds to wait before failing - * @param sleepInMillis the number of milliseconds to wait before a recheck - * @return boolean - true if not displayed - */ - public boolean waitUntilAbsentElement(By by, - final int timeOutInMillis, - final int sleepInMillis) { - - try { - WebElement element = this.driver.findElement(by); - FluentWait<WebElement> fluentWait = FluentWaitFactory.getNewElementFluentWait(element, timeOutInMillis, sleepInMillis); - Function<WebElement, Boolean> function = obj -> { - try { - return !obj.isDisplayed(); - } catch (NoSuchElementException | StaleElementReferenceException e) { - // Do not throw these exceptions here. Instead return true as the element has disappeared. - return true; - } - }; - return fluentWait.until(function); - } catch (NoSuchElementException | StaleElementReferenceException e) { - return true; - } catch (TimeoutException e) { - return false; - } - } - - /** - * Wait for a selector to present, and then return a list of all WebElements that are located by - * that selector. - * - * @param by Selector value to wait for - * @return List of WebElements - all web elements found by the specified selector - */ - public List<WebElement> waitForElements(final By by) { - return this.waitForElements(by, getWaitDriver()); - } - - /** - * Wait for a selector to present, and then return a list of all WebElements that are located by - * that selector. - * - * @param by Selector value to wait for - * @param timeOutInMillis the number of milliseconds to wait before failing - * @param sleepInMillis the number of milliseconds to wait before a recheck - * @return List of WebElements - all web elements found by the specified selector - * @throws Exception if encountered - */ - public List<WebElement> waitForElements(final By by, - final int timeOutInMillis, - final int sleepInMillis) { - WebDriverWait wait = this.getNewWaitDriver(timeOutInMillis, sleepInMillis); - return waitForElements(by, wait); - } - - /** - * Wait for a selector to present, and then return a list of all WebElements that are located by - * that selector. - * - * @param by Selector value to wait for - * @param wait The wait driver - * @return List of WebElements - all web elements found by the specified selector - * @throws Exception if encountered - */ - public List<WebElement> waitForElements(final By by, WebDriverWait wait) { - return wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(by)); - } - - /** - * Wait for the specified element to have the Exact text. - * - * @param by By selector to examine - * @param text String to search for in the text - * @return WebElement - element, null if not found and assert == false - */ - public WebElement waitForExactText(final By by, final String text) { - waitUntilExactText(by, text); - return this.driver.findElement(by); - } - - /** - * Wait until the exact text is present in the specified element. - * - * @param by Selector to examine for the specified text - * @param text String value to verify the specified selector contains - * @return boolean - true if the text was found in the element - else false - */ - public boolean waitUntilExactText(final By by, final String text) { - return this.waitUntilExactText(by, text, getWaitDriver()); - } - - /** - * Wait until the exact text is present in the specified element. - * - * @param by Selector to examine for the specified text - * @param text String value to verify the specified selector contains - * @param timeOutInMillis the number of milliseconds to wait before failing - * @param sleepInMillis the number of milliseconds to wait before a recheck - * @return boolean - true if the text was found in the element - else false - */ - public boolean waitUntilExactText(final By by, - final String text, - final int timeOutInMillis, - final int sleepInMillis) { - WebDriverWait wait = this.getNewWaitDriver(timeOutInMillis, sleepInMillis); - return waitUntilExactText(by, text, wait); - } - - /** - * Wait until the exact text is present in the specified element. - * - * @param by Selector to examine for the specified text - * @param text String value to verify the specified selector contains - * @param wait The wait driver - * @return boolean - true if the text was found in the element - else false - */ - public boolean waitUntilExactText(final By by, - final String text, - WebDriverWait wait) { - try { - return wait.until((ExpectedCondition<Boolean>) function -> doesTextMatch(by, text)); - } catch (NoSuchElementException | StaleElementReferenceException | TimeoutException e) { - return false; - } - } - - /** - * Wait for an element to contain a specified text. - * - * @param by Selector to check the containing text - * @param text String that should be contained within the selector - * @return WebElement containing the text - */ - public WebElement waitForContainsText(final By by, final String text) { - if (waitUntilContainsText(by, text, getWaitDriver())) { - return this.driver.findElement(by); - } - String error = String.format("Selector [%s] couldn't be found.%n", by.toString()); - throw new NotFoundException(error); - } - - /** - * Wait until an element contains the specified text. - * - * @param by Selector to check the containing text - * @param text String that should be contained within the selector - * @return boolean - true if the text is contained in the selector, else false - */ - public boolean waitUntilContainsText(final By by, final String text) { - return this.waitUntilContainsText(by, text, getWaitDriver()); - } - - /** - * Wait until an element contains the specified text. - * - * @param by Selector to check the containing text - * @param text String that should be contained within the selector - * @param timeOutInMillis the number of milliseconds to wait before failing - * @param sleepInMillis the number of milliseconds to wait before a recheck - * @return boolean - true if the text is contained in the selector, else false - */ - public boolean waitUntilContainsText(final By by, - final String text, - final int timeOutInMillis, - final int sleepInMillis) { - WebDriverWait wait = this.getNewWaitDriver(timeOutInMillis, sleepInMillis); - return waitUntilContainsText(by, text, wait); - } - - /** - * Wait until the element contains the specified text. - * - * @param by Selector to check the containing text - * @param text String that should be contained within the selector - * @param wait int value of seconds to wait before timing out - * @return boolean - true if the text is contained in the selector, else false - */ - public boolean waitUntilContainsText(final By by, - final String text, - WebDriverWait wait) { - try { - return wait.until((ExpectedCondition<Boolean>) function -> doesContainsText(by, text)); - } catch (NoSuchElementException | StaleElementReferenceException | TimeoutException e) { - return false; - } - } - - /** - * Wait until an attribute of the specified selector to be present and equal the desired value. - * - * @param by Selector to look for - * @param attribute String value of the attribute to look at on the specified selector - * @param text String value of the text to look for in the attribute - * @return true if the attribute with the specified text value is found, else false - */ - public boolean waitUntilAttributeTextEquals(final By by, - final String attribute, - final String text) { - return this.waitUntilAttribute(by, attribute, text, getWaitDriver(), false); - } - - /** - * Wait until an attribute of the specified selector to be present. - * - * @param by Selector to look for - * @param attribute String value of the attribute to look at on the specified selector - * @param text String value of the text to look for in the attribute - * @param timeOutInMillis - the number of milliseconds to wait before failing - * @param sleepInMillis - the number of milliseconds to wait before a recheck - * @return true if the attribute with the specified text value is found, else false - */ - public boolean waitUntilAttributeTextEquals(final By by, - final String attribute, - final String text, - final int timeOutInMillis, - final int sleepInMillis) { - WebDriverWait wait = this.getNewWaitDriver(timeOutInMillis, sleepInMillis); - return waitUntilAttribute(by, attribute, text, wait, false); - } - - /** - * Wait for an attribute of the specified selector to be present. - * - * @param by Selector to look for - * @param attribute String value of the attribute to look at on the specified selector - * @param text String value of the text to look for in the attribute - * @return Webelement of the selector that is found - */ - public WebElement waitForAttributeTextEquals(final By by, - final String attribute, - final String text) { - return this.waitForAttributeTextEquals(by, attribute, text, getWaitDriver()); - } - - /** - * Wait for an attribute of the specified selector to be present and equal the desired value. - * - * @param by Selector to look for - * @param attribute String value of the attribute to look at on the specified selector - * @param text String value of the text to look for in the attribute - * @param timeOutInMillis the number of milliseconds to wait before failing - * @param sleepInMillis the number of milliseconds to wait before a recheck - * @return WebElement of the selector that is found - */ - public WebElement waitForAttributeTextEquals(final By by, - final String attribute, - final String text, - final int timeOutInMillis, - final int sleepInMillis) { - WebDriverWait wait = this.getNewWaitDriver(timeOutInMillis, sleepInMillis); - return waitForAttributeTextEquals(by, attribute, text, wait); - } - - /** - * Wait for an attribute of the specified selector to be present and equal the desired value. - * - * @param by Selector to look for - * @param attribute String value of the attribute to look at on the specified selector - * @param text String value of the text to look for in the attribute - * @param wait int version of the timeout in seconds - * @return WebElement of the selector that is found - */ - public WebElement waitForAttributeTextEquals(final By by, - final String attribute, - final String text, - WebDriverWait wait) { - if (this.waitUntilAttribute(by, attribute, text, wait, false)) { - return this.driver.findElement(by); - } - - String error = String.format("Selector [%s] couldn't be found.%n", by.toString()); - throw new NotFoundException(error); - } - - /** - * Wait for an attribute of the specified selector to be present, and contain the specified text. - * - * @param by Selector to look for - * @param attribute String value of the attribute to look at on the specified selector - * @param text String value of the text to look for in the attribute - * @return true if the attribute with the specified text value is found, else false - */ - public boolean waitUntilAttributeTextContains(final By by, - final String attribute, - final String text) { - return waitUntilAttribute(by, attribute, text, getWaitDriver(), true); - } - - /** - * Wait for an attribute of the specified selector to be present, and contain the specified text. - * - * @param by Selector to look for - * @param attribute String value of the attribute to look at on the specified selector - * @param text String value of the text to look for in the attribute - * @param timeOutInMillis the number of milliseconds to wait before failing - * @param sleepInMillis the number of milliseconds to wait before a recheck - * @return true if the attribute with the specified text value is found, else false - */ - public boolean waitUntilAttributeTextContains(final By by, - final String attribute, - final String text, - final int timeOutInMillis, - final int sleepInMillis) { - WebDriverWait wait = this.getNewWaitDriver(timeOutInMillis, sleepInMillis); - return waitUntilAttribute(by, attribute, text, wait, true); - } - - /** - * Wait for an attribute of the specified selector to be present, and contain the specified text. - * - * @param by Selector to look for - * @param attribute String value of the attribute to look at on the specified selector - * @param text String value of the text to look for in the attribute - * @param wait The wait driver - * @param contains boolean true if checking if contains, false if exact match - * @return true if the attribute with the specified text value is found, else false - */ - public boolean waitUntilAttribute(final By by, - final String attribute, - final String text, - WebDriverWait wait, - final boolean contains) { - try { - return wait.until((ExpectedCondition<Boolean>) f -> attributeMatches(f, by, attribute, text, contains)); - } catch (NoSuchElementException | StaleElementReferenceException | TimeoutException e) { - return false; - } - } - - /** - * Scroll an element into view, and wait for it to be clickable. - * - * @param by Selector to wait for and focus on - * @return WebElement - */ - public WebElement waitForClickableElementAndScrollIntoView(final By by) { - return this.waitForClickableElementAndScrollIntoView(by, getWaitDriver()); - } - - /** - * Scroll an element into view, and wait for it to be clickable. - * - * @param by Selector to wait for and focus on - * @param timeOutInMillis the number of milliseconds to wait before failing - * @param sleepInMillis the number of milliseconds to wait before a recheck - * @return WebElement - */ - public WebElement waitForClickableElementAndScrollIntoView(final By by, - final int timeOutInMillis, - final int sleepInMillis) { - WebDriverWait wait = this.getNewWaitDriver(timeOutInMillis, sleepInMillis); - return waitForClickableElementAndScrollIntoView(by, wait); - } - - /** - * Scroll an element into view, and wait for it to be clickable. - * - * @param by Selector to wait for and focus on - * @param wait int value of seconds to wait for before timing out - * @return WebElement - */ - public WebElement waitForClickableElementAndScrollIntoView(final By by, WebDriverWait wait) { - scrollIntoView(by); - waitUntilPageLoad(); - return waitForClickableElement(by, wait); - } - - /** - * Scroll an element into view, and wait for it to be clickable. - * - * @param by Selector to wait for and focus on - * @return boolean - true if the element is found and clickable - */ - public boolean waitUntilClickableElementAndScrollIntoView(final By by) { - return this.waitUntilClickableElementAndScrollIntoView(by, getWaitDriver()); - } - - /** - * Scroll an element into view, and wait for it to be clickable. - * - * @param by Selector to wait for and focus on - * @param timeOutInMillis the number of milliseconds to wait before failing - * @param sleepInMillis the number of milliseconds to wait before a recheck - * @return boolean - true if the element is found and clickable - */ - public boolean waitUntilClickableElementAndScrollIntoView(final By by, - final int timeOutInMillis, - final int sleepInMillis) { - WebDriverWait wait = this.getNewWaitDriver(timeOutInMillis, sleepInMillis); - return waitUntilClickableElementAndScrollIntoView(by, wait); - } - - /** - * Scroll an element into view, and wait for it to be clickable. - * - * @param by Selector to wait for and focus on - * @param wait The wait driver - * @return boolean - true if the element is found and clickable - */ - public boolean waitUntilClickableElementAndScrollIntoView(final By by, WebDriverWait wait) { - scrollIntoView(by); - waitUntilPageLoad(); - return waitUntilClickableElement(by, wait); - } - - /** - * Wait for the specified selector to be clickable. - * - * @param by Selector to wait for - * @return boolean - true if found and clickable, else false - */ - public boolean waitUntilClickableElement(final By by) { - return this.waitUntilClickableElement(by, getWaitDriver()); - } - - /** - * Wait for the specified selector to be clickable. - * - * @param by Selector to wait for - * @param timeOutInMillis the number of milliseconds to wait before failing - * @param sleepInMillis the number of milliseconds to wait before a recheck - * @return boolean - true if found and clickable, else false - */ - public boolean waitUntilClickableElement(final By by, final int timeOutInMillis, final int sleepInMillis) { - WebDriverWait wait = this.getNewWaitDriver(timeOutInMillis, sleepInMillis); - return waitUntilClickableElement(by, wait); - } - - /** - * Wait for the specified selector to be clickable. - * - * @param by Selector to wait for - * @param wait The wait driver - * @return boolean - true if found and clickable, else false - */ - public boolean waitUntilClickableElement(final By by, WebDriverWait wait) { - try { - if (waitForClickableElement(by, wait) == null) { - return false; - } - } catch (Exception e) { - return false; - } - return true; - } - - /** - * Wait for the element specified by the provided selector to be clickable. - * - * @param by Selector to wait for to be clickable - * @return WebElement that is located, or null if none is found - */ - public WebElement waitForClickableElement(final By by) { - return this.waitForClickableElement(by, getWaitDriver()); - } - - /** - * Wait for the element specified by the provided selector to be clickable. - * - * @param by Selector to wait for to be clickable - * @param timeOutInMillis the number of milliseconds to wait before failing - * @param sleepInMillis the number of milliseconds to wait before a recheck - * @return WebElement that is located, or null if none is found - */ - public WebElement waitForClickableElement(final By by, - final int timeOutInMillis, - final int sleepInMillis) { - - WebDriverWait wait = this.getNewWaitDriver(timeOutInMillis, sleepInMillis); - return waitForClickableElement(by, wait); - } - - /** - * Wait for the element specified by the provided selector to be clickable. - * - * @param by Selector to wait for to be clickable - * @param wait The wait driver - * @return WebElement that is located, or null if none is found - */ - public WebElement waitForClickableElement(final By by, WebDriverWait wait) { - return wait.until(ExpectedConditions.elementToBeClickable(by)); - } - - /** - * Wait for the page to load by waiting for the source to stop changing for at least a second. - * Asserts the page loads - */ - public void waitForPageLoad() { - if (!this.waitUntilPageLoad()) { - throw new TimeoutException("Page load took longer than timeout configuration."); - } - } - - /** - * Wait for the page to load by waiting for the source to stop changing for at least a second. - * - * @return true if it's successfully loaded, false if timed out and not finished loading - */ - public boolean waitUntilPageLoad() { - String before = ""; - String after = ""; - int counter = this.timeout / this.fluentRetryTime; - - do { - try { - counter--; - before = this.driver.getPageSource(); - Thread.sleep(this.fluentRetryTime); - after = this.driver.getPageSource(); - } catch (InterruptedException e) { - Thread.currentThread().interrupt(); - } - } while (!before.equals(after) && counter > 0); - - return before.equals(after); - } - - /** - * Waits for the Frame to be available. - * - * @param by The frame locator - */ - public boolean waitUntilIframeToLoad(By by) { - return waitUntilIframeToLoad(by, this.getWaitDriver()); - } - - /** - * Waits for the Frame to be available. - * - * @param by The frame locator - * @param timeOutInMillis he number of milliseconds to wait before failing - * @param sleepInMillis the number of milliseconds to wait before a recheck - */ - public boolean waitUntilIframeToLoad(By by, - final int timeOutInMillis, - final int sleepInMillis) { - WebDriverWait wait = this.getNewWaitDriver(timeOutInMillis, sleepInMillis); - return waitUntilIframeToLoad(by, wait); - } - - /** - * Waits for the Frame to be available. - * - * @param by The frame locator - */ - public boolean waitUntilIframeToLoad(By by, WebDriverWait wait) { - - try { - waitForIframeToLoad(by, wait); - return true; - } catch (NoSuchElementException - | StaleElementReferenceException - | TimeoutException e) { - return false; - } - } - - /** - * Waits for the Frame to be available. - * - * @param by The frame locator - */ - public void waitForIframeToLoad(By by) { - waitForIframeToLoad(by, this.getWaitDriver()); - } - - /** - * Waits for the Frame to be available. - * - * @param by The frame locator - * @param timeOutInMillis the number of milliseconds to wait before failing - * @param sleepInMillis the number of milliseconds to wait before a recheck - */ - public void waitForIframeToLoad(By by, - final int timeOutInMillis, - final int sleepInMillis) { - WebDriverWait wait = this.getNewWaitDriver(timeOutInMillis, sleepInMillis); - waitForIframeToLoad(by, wait); - } - - /** - * Waits for the Frame to be available. - * - * @param by The frame locator - */ - public void waitForIframeToLoad(By by, WebDriverWait wait) { - wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(by)); - } - - /** - * Returns the webDriver of this SeleniumWait object. - * - * @return the webDriver (type WebDriver) of this SeleniumWait object. - */ - protected WebDriver getWebDriver() { - return driver; - } - - /** - * Gets a new WaitDriver using the default timeout. - * - * @return new WaitDriver - */ - protected WebDriverWait getNewWaitDriver(WebDriver driver) { - return getNewWaitDriver(driver, this.timeout, this.fluentRetryTime); - } - - /** - * Gets a new WaitDriver using the default timeout. - * - * @return new WaitDriver - */ - protected WebDriverWait getNewWaitDriver() { - return getNewWaitDriver(this.getWebDriver(), this.timeout, this.fluentRetryTime); - } - - /** - * Gets a new WaitDriver using the specified timeout. - * - * @param timeOutInMillis the default timeout - * @return new WaitDriver - */ - protected WebDriverWait getNewWaitDriver(int timeOutInMillis) { - return getNewWaitDriver(this.getWebDriver(), timeOutInMillis, this.fluentRetryTime); - } - - /** - * Gets a new WaitDriver using the specified timeout. - * - * @param timeOutInMillis the time to wait before a timeout - * @param sleepInMillis the time to wait before a recheck - * @return new WaitDriver - */ - protected WebDriverWait getNewWaitDriver(int timeOutInMillis, int sleepInMillis) { - return getNewWaitDriver(this.getWebDriver(), timeOutInMillis, sleepInMillis); - } - - /** - * Gets a new WaitDriver using the specified timeout. - * - * @param timeOutInMillis the time to wait before a timeout - * @param sleepInMillis the time to wait before a recheck - * @return new WaitDriver - */ - protected WebDriverWait getNewWaitDriver(WebDriver driver, int timeOutInMillis, int sleepInMillis) { - int timeoutInSeconds = timeOutInMillis / 1000; - WebDriverWait wait = new WebDriverWait(driver, timeoutInSeconds, sleepInMillis); - setWaitDriver(wait); - return wait; - } - - /** - * Scroll the web page so the selector is visible. - * - * @param by Selector to make visible - */ - private void scrollIntoView(By by) { - WebElement element = waitForVisibleElement(by); - int counter = 0; - final int max = 5; - - try { - ((JavascriptExecutor) this.driver).executeScript( - "arguments[0].scrollIntoView(true);", - element); - } - catch (Exception e) { - String error = String.format("Failed JavaScript scroll into view...%s%n", (Object[])e.getStackTrace()); - System.err.print(error); - } - - Coordinates coord = ((Locatable) element).getCoordinates(); - while (coord.inViewPort().getY() < HEADER_SIZE && counter < max) { - waitForVisibleElement(BODY_BY).sendKeys(Keys.ARROW_UP); - counter++; - } - } - - - - /** - * Check the text value of an attribute. - * - * @param driver - web driver - * @param by Selector to get the attribute from - * @param attribute String value of the attribute to examine - * @param text String to check against the attribute - * @param contains boolean - true to see if the string is contained, false if exact match - * @return boolean - true if they match, else false - */ - private static boolean attributeMatches( - WebDriver driver, - By by, - String attribute, - String text, - boolean contains) { - try { - if (contains) { - return driver.findElement(by).getAttribute(attribute).contains(text); - } else { - return driver.findElement(by).getAttribute(attribute).equals(text); - } - } - catch (NullPointerException e) { - return false; - } - } - - /** - * Checks if the text of the elements are equal - * - * @param by Selector to examine - * @param text Text that is being compared to the selector - * @return boolean - true if equal - */ - private boolean doesTextMatch(By by, String text) { - try { - WebElement element = this.waitForVisibleElement(by); - - if (element != null && element.getText().equals(text)) { - return true; - } - } catch (Exception e) { - return false; - } - return false; - } - - /** - * Checks if the text of the elements are equal. - * - * @param by Selector to examine - * @param text Text that is being compared to the selector - * @return boolean - true if equal - */ - private boolean doesContainsText(By by, String text) { - try { - WebElement element = this.waitForVisibleElement(by); - - if (element != null && element.getText().contains(text)) { - return true; - } - } - catch (Exception e) { - return false; - } - return false; - } + /** + * The default value of the Header size... Your header's size may be different. + */ + private static final int HEADER_SIZE = 90; + + /** + * The default value of the page's body. Your page might have a different Header value. + */ + private static final By BODY_BY = By.cssSelector("BODY"); + + /** + * The Webdriver that the test is currently running on. + */ + private WebDriver driver; + + /** + * The retry time. + */ + private int fluentRetryTime; + + /** + * The timeout time. + */ + private int timeout; + + /** + * The web driver wait that the test is currently running on. + */ + private WebDriverWait waitDriver; + + /** + * Constructor for {@link UIWait} object. + * + * @param driver WebDriver + */ + public UIWait(WebDriver driver) { + this(driver, Integer.parseInt(Config.getValueForSection(ConfigSection.SeleniumMaqs, "BrowserTimeout", "30000")), + Integer.parseInt(Config.getValueForSection(ConfigSection.SeleniumMaqs, "BrowserWaitTime", "1000")), null); + } + + public UIWait(WebDriver driver, WebDriverWait waitDriver) { + this(driver, Integer.parseInt(Config.getValueForSection(ConfigSection.SeleniumMaqs, "BrowserTimeout", "30000")), + Integer.parseInt(Config.getValueForSection(ConfigSection.SeleniumMaqs, "BrowserWaitTime", "1000")), waitDriver); + } + + /** + * Constructor for SeleniumWait object. + * + * @param driver The WebDriver + * @param timeOutInSeconds int value of the total time to wait until timing out + * @param fluentRetryTime int value of seconds to use for fluent retry + */ + public UIWait(WebDriver driver, final int timeOutInSeconds, final int fluentRetryTime, WebDriverWait webDriverWait) { + this.driver = driver; + this.timeout = timeOutInSeconds; + this.fluentRetryTime = fluentRetryTime; + this.setWaitDriver(webDriverWait == null ? this.getNewWaitDriver() : webDriverWait); + } + + /** + * Get the WebDriverWait for use outside of this instance class. + * + * @return The WebDriverWait + */ + public WebDriverWait getWaitDriver() { + return this.waitDriver; + } + + /** + * Sets the WebDriverWait. + * + * @param waiter The WebDriverWait + */ + public void setWaitDriver(WebDriverWait waiter) { + this.waitDriver = waiter; + } + + /** + * Resets wait default wait driver. + * + * @return The WebDriverWait + */ + public WebDriverWait resetWaitDriver() { + WebDriverWait wait = this.getNewWaitDriver(); + this.setWaitDriver(wait); + return wait; + } + + /** + * Waits until the element is present. + * + * @param by The by selector + * @return Returns the element if present + */ + public WebElement waitForPresentElement(By by) { + return this.waitForPresentElement(by, this.getWaitDriver()); + } + + /** + * Waits until the element is present. + * + * @param by The by selector + * @param timeOutInMillis The timeout in milliseconds + * @param sleepInMillis the number of milliseconds to wait before a recheck + * @return Returns the element if present + */ + public WebElement waitForPresentElement(By by, final int timeOutInMillis, final int sleepInMillis) { + WebDriverWait wait = this.getNewWaitDriver(timeOutInMillis, sleepInMillis); + return wait.until(ExpectedConditions.presenceOfElementLocated(by)); + } + + /** + * Waits For the element to be present. + * + * @param by The by selector + * @param wait The wait driver + * @return Returns the element if present + */ + public WebElement waitForPresentElement(By by, WebDriverWait wait) { + return wait.until(ExpectedConditions.presenceOfElementLocated(by)); + } + + /** + * Wait for the specified element to be visible on the pages DOM. The first element located with + * the specified By value is returned. + * + * @param by Selector to wait for, and return + * @return WebElement - first one found with by + */ + public WebElement waitForVisibleElement(final By by) { + return this.waitForVisibleElement(by, getWaitDriver()); + } + + /** + * Wait for the specified element to be visible on the pages DOM. The first element located with + * the specified By value is returned. + * + * @param by Selector to wait for, and return + * @param timeOutInMillis the number of milliseconds to wait before failing + * @param sleepInMillis the number of milliseconds to wait before a recheck + * @return WebElement - first one found with by. Null + */ + public WebElement waitForVisibleElement(final By by, final int timeOutInMillis, final int sleepInMillis) { + WebDriverWait wait = this.getNewWaitDriver(timeOutInMillis, sleepInMillis); + return this.waitForVisibleElement(by, wait); + } + + /** + * Wait for the specified element to be visible on the pages DOM. The first element located with + * the specified By value is returned. + * + * @param by Selector to wait for, and return + * @param wait The wait driver + * @return WebElement - first one found with by. Null + */ + public WebElement waitForVisibleElement(final By by, WebDriverWait wait) { + try { + return wait.until(ExpectedConditions.visibilityOfElementLocated(by)); + } catch (TimeoutException | NoSuchElementException e) { + return null; + } + } + + /** + * Wait until the element exists and is visible. + * + * @param by Selector to wait for + * @return boolean true if element is found + */ + public boolean waitUntilVisibleElement(final By by) { + return waitUntilVisibleElement(by, this.getWaitDriver()); + } + + /** + * Wait until the element exists and is visible. + * + * @param by Selector to wait for + * @param timeOutInMillis the number of milliseconds to wait before failing + * @param sleepInMillis the number of milliseconds to wait before a recheck + * @return boolean true if element is found + */ + public boolean waitUntilVisibleElement(final By by, final int timeOutInMillis, final int sleepInMillis) { + WebDriverWait wait = this.getNewWaitDriver(this.driver, timeOutInMillis, sleepInMillis); + return waitUntilVisibleElement(by, wait); + } + + /** + * Wait until the element exists and is visible. + * + * @param by Selector to wait for + * @param wait The wait driver + * @return boolean true if element is found + */ + public boolean waitUntilVisibleElement(final By by, WebDriverWait wait) { + try { + return waitForVisibleElement(by, wait) != null; + } catch (NoSuchElementException | StaleElementReferenceException | TimeoutException e) { + return false; + } + } + + /** + * Wait for the specified element to be present and enabled. The first element located with + * the specified By value is returned. + * + * @param by Selector to wait for, and return + * @return WebElement - first one found with by. Null + */ + public WebElement waitForEnabledElement(final By by) { + return waitForEnabledElement(by, this.timeout, this.fluentRetryTime); + } + + /** + * Wait for the specified element to be present and enabled. The first element located with + * the specified By value is returned. + * + * @param by Selector to wait for, and return + * @param timeOutInMillis - the number of milliseconds to wait before failing + * @param sleepInMillis - the number of milliseconds to wait before a recheck + * @return WebElement - first one found with by. Null + */ + public WebElement waitForEnabledElement(final By by, final int timeOutInMillis, final int sleepInMillis) { + if (waitUntilEnabledElement(by, timeOutInMillis, sleepInMillis)) { + return this.driver.findElement(by); + } + + throw new ElementNotInteractableException("Element is not enabled"); + } + + /** + * Wait until the specified element to be present and enabled. + * + * @param by - Selector to wait for, and return + * @return boolean true if element is found, else false + */ + public boolean waitUntilEnabledElement(final By by) { + return waitUntilEnabledElement(by, this.timeout, this.fluentRetryTime); + } + + /** + * Wait for the specified element to be present and enabled. + * + * @param by Selector to wait for, and return + * @param timeOutInMillis the number of milliseconds to wait before failing + * @param sleepInMillis the number of milliseconds to wait before a recheck + * @return boolean true if element is found, else false + */ + public boolean waitUntilEnabledElement(final By by, final int timeOutInMillis, final int sleepInMillis) { + + WebElement element = this.waitForVisibleElement(by); + FluentWait<WebElement> fluentWait = FluentWaitFactory + .getNewElementFluentWait(element, timeOutInMillis, sleepInMillis); + + Function<WebElement, Boolean> function = obj -> { + try { + return obj.isEnabled(); + } catch (NoSuchElementException | StaleElementReferenceException e) { + // Do not throw these exceptions here. + // Instead return false and let the fluentwait try again. + return false; + } + }; + + try { + return fluentWait.until(function); + } catch (TimeoutException e) { + return false; + } + } + + /** + * Waits until the element is disabled. + * + * @param by the By selector + * @return returns true if the element is disabled, else false + */ + public boolean waitUntilDisabledElement(By by) { + return waitUntilDisabledElement(by, this.timeout, this.fluentRetryTime); + } + + /** + * Waits until the element is disabled. + * + * @param by the web element + * @param timeOutInMillis he number of milliseconds to wait before failing + * @param sleepInMillis the number of milliseconds to wait before a recheck + * @return returns true if the element is disabled, else false + */ + public boolean waitUntilDisabledElement(By by, final int timeOutInMillis, final int sleepInMillis) { + try { + WebElement element = this.waitForVisibleElement(by, timeOutInMillis, sleepInMillis); + FluentWait<WebElement> fluentWait = FluentWaitFactory + .getNewElementFluentWait(element, timeOutInMillis, sleepInMillis); + return fluentWait.until(obj -> !obj.isEnabled()); + } catch (NoSuchElementException | StaleElementReferenceException e) { + return true; + } catch (TimeoutException e) { + return false; + } + } + + /** + * Wait until the element is not displayed or visible. + * + * @param by Selector to not be displayed or visible + */ + public void waitForAbsentElement(final By by) { + this.waitForAbsentElement(by, this.timeout, this.fluentRetryTime); + } + + /** + * Wait until the element is not displayed or visible. + * + * @param by Selector to not be displayed or visible + */ + public void waitForAbsentElement(final By by, final int timeOutInMillis, final int sleepInMillis) { + boolean isAbsent = this.waitUntilAbsentElement(by, timeOutInMillis, sleepInMillis); + + if (!isAbsent) { + throw new TimeoutException(MessageFormat.format("Element with selector {0} is not absent", by)); + } + } + + /** + * Wait until the element is not displayed or visible. + * + * @param by Selector to not be displayed or visible + */ + public boolean waitUntilAbsentElement(final By by) { + return this.waitUntilAbsentElement(by, this.timeout, this.fluentRetryTime); + } + + /** + * Wait until the element is not displayed or visible. + * + * @param by element to not be displayed or visible + * @param timeOutInMillis the number of milliseconds to wait before failing + * @param sleepInMillis the number of milliseconds to wait before a recheck + * @return boolean - true if not displayed + */ + public boolean waitUntilAbsentElement(By by, final int timeOutInMillis, final int sleepInMillis) { + + try { + WebElement element = this.driver.findElement(by); + FluentWait<WebElement> fluentWait = FluentWaitFactory + .getNewElementFluentWait(element, timeOutInMillis, sleepInMillis); + Function<WebElement, Boolean> function = obj -> { + try { + return !obj.isDisplayed(); + } catch (NoSuchElementException | StaleElementReferenceException e) { + // Do not throw these exceptions here. Instead return true as the element has disappeared. + return true; + } + }; + return fluentWait.until(function); + } catch (NoSuchElementException | StaleElementReferenceException e) { + return true; + } catch (TimeoutException e) { + return false; + } + } + + /** + * Wait for a selector to present, and then return a list of all WebElements that are located by + * that selector. + * + * @param by Selector value to wait for + * @return List of WebElements - all web elements found by the specified selector + */ + public List<WebElement> waitForElements(final By by) { + return this.waitForElements(by, getWaitDriver()); + } + + /** + * Wait for a selector to present, and then return a list of all WebElements that are located by + * that selector. + * + * @param by Selector value to wait for + * @param timeOutInMillis the number of milliseconds to wait before failing + * @param sleepInMillis the number of milliseconds to wait before a recheck + * @return List of WebElements - all web elements found by the specified selector + * @throws Exception if encountered + */ + public List<WebElement> waitForElements(final By by, final int timeOutInMillis, final int sleepInMillis) { + WebDriverWait wait = this.getNewWaitDriver(timeOutInMillis, sleepInMillis); + return waitForElements(by, wait); + } + + /** + * Wait for a selector to present, and then return a list of all WebElements that are located by + * that selector. + * + * @param by Selector value to wait for + * @param wait The wait driver + * @return List of WebElements - all web elements found by the specified selector + * @throws Exception if encountered + */ + public List<WebElement> waitForElements(final By by, WebDriverWait wait) { + return wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(by)); + } + + /** + * Wait for the specified element to have the Exact text. + * + * @param by By selector to examine + * @param text String to search for in the text + * @return WebElement - element, null if not found and assert == false + */ + public WebElement waitForExactText(final By by, final String text) { + waitUntilExactText(by, text); + return this.driver.findElement(by); + } + + /** + * Wait until the exact text is present in the specified element. + * + * @param by Selector to examine for the specified text + * @param text String value to verify the specified selector contains + * @return boolean - true if the text was found in the element - else false + */ + public boolean waitUntilExactText(final By by, final String text) { + return this.waitUntilExactText(by, text, getWaitDriver()); + } + + /** + * Wait until the exact text is present in the specified element. + * + * @param by Selector to examine for the specified text + * @param text String value to verify the specified selector contains + * @param timeOutInMillis the number of milliseconds to wait before failing + * @param sleepInMillis the number of milliseconds to wait before a recheck + * @return boolean - true if the text was found in the element - else false + */ + public boolean waitUntilExactText(final By by, final String text, final int timeOutInMillis, + final int sleepInMillis) { + WebDriverWait wait = this.getNewWaitDriver(timeOutInMillis, sleepInMillis); + return waitUntilExactText(by, text, wait); + } + + /** + * Wait until the exact text is present in the specified element. + * + * @param by Selector to examine for the specified text + * @param text String value to verify the specified selector contains + * @param wait The wait driver + * @return boolean - true if the text was found in the element - else false + */ + public boolean waitUntilExactText(final By by, final String text, WebDriverWait wait) { + try { + return wait.until((ExpectedCondition<Boolean>) function -> doesTextMatch(by, text)); + } catch (NoSuchElementException | StaleElementReferenceException | TimeoutException e) { + return false; + } + } + + /** + * Wait for an element to contain a specified text. + * + * @param by Selector to check the containing text + * @param text String that should be contained within the selector + * @return WebElement containing the text + */ + public WebElement waitForContainsText(final By by, final String text) { + if (waitUntilContainsText(by, text, getWaitDriver())) { + return this.driver.findElement(by); + } + String error = String.format("Selector [%s] couldn't be found.%n", by.toString()); + throw new NotFoundException(error); + } + + /** + * Wait until an element contains the specified text. + * + * @param by Selector to check the containing text + * @param text String that should be contained within the selector + * @return boolean - true if the text is contained in the selector, else false + */ + public boolean waitUntilContainsText(final By by, final String text) { + return this.waitUntilContainsText(by, text, getWaitDriver()); + } + + /** + * Wait until an element contains the specified text. + * + * @param by Selector to check the containing text + * @param text String that should be contained within the selector + * @param timeOutInMillis the number of milliseconds to wait before failing + * @param sleepInMillis the number of milliseconds to wait before a recheck + * @return boolean - true if the text is contained in the selector, else false + */ + public boolean waitUntilContainsText(final By by, final String text, final int timeOutInMillis, + final int sleepInMillis) { + WebDriverWait wait = this.getNewWaitDriver(timeOutInMillis, sleepInMillis); + return waitUntilContainsText(by, text, wait); + } + + /** + * Wait until the element contains the specified text. + * + * @param by Selector to check the containing text + * @param text String that should be contained within the selector + * @param wait int value of seconds to wait before timing out + * @return boolean - true if the text is contained in the selector, else false + */ + public boolean waitUntilContainsText(final By by, final String text, WebDriverWait wait) { + try { + return wait.until((ExpectedCondition<Boolean>) function -> doesContainsText(by, text)); + } catch (NoSuchElementException | StaleElementReferenceException | TimeoutException e) { + return false; + } + } + + /** + * Wait until an attribute of the specified selector to be present and equal the desired value. + * + * @param by Selector to look for + * @param attribute String value of the attribute to look at on the specified selector + * @param text String value of the text to look for in the attribute + * @return true if the attribute with the specified text value is found, else false + */ + public boolean waitUntilAttributeTextEquals(final By by, final String attribute, final String text) { + return this.waitUntilAttribute(by, attribute, text, getWaitDriver(), false); + } + + /** + * Wait until an attribute of the specified selector to be present. + * + * @param by Selector to look for + * @param attribute String value of the attribute to look at on the specified selector + * @param text String value of the text to look for in the attribute + * @param timeOutInMillis - the number of milliseconds to wait before failing + * @param sleepInMillis - the number of milliseconds to wait before a recheck + * @return true if the attribute with the specified text value is found, else false + */ + public boolean waitUntilAttributeTextEquals(final By by, final String attribute, final String text, + final int timeOutInMillis, final int sleepInMillis) { + WebDriverWait wait = this.getNewWaitDriver(timeOutInMillis, sleepInMillis); + return waitUntilAttribute(by, attribute, text, wait, false); + } + + /** + * Wait for an attribute of the specified selector to be present. + * + * @param by Selector to look for + * @param attribute String value of the attribute to look at on the specified selector + * @param text String value of the text to look for in the attribute + * @return Webelement of the selector that is found + */ + public WebElement waitForAttributeTextEquals(final By by, final String attribute, final String text) { + return this.waitForAttributeTextEquals(by, attribute, text, getWaitDriver()); + } + + /** + * Wait for an attribute of the specified selector to be present and equal the desired value. + * + * @param by Selector to look for + * @param attribute String value of the attribute to look at on the specified selector + * @param text String value of the text to look for in the attribute + * @param timeOutInMillis the number of milliseconds to wait before failing + * @param sleepInMillis the number of milliseconds to wait before a recheck + * @return WebElement of the selector that is found + */ + public WebElement waitForAttributeTextEquals(final By by, final String attribute, final String text, + final int timeOutInMillis, final int sleepInMillis) { + WebDriverWait wait = this.getNewWaitDriver(timeOutInMillis, sleepInMillis); + return waitForAttributeTextEquals(by, attribute, text, wait); + } + + /** + * Wait for an attribute of the specified selector to be present and equal the desired value. + * + * @param by Selector to look for + * @param attribute String value of the attribute to look at on the specified selector + * @param text String value of the text to look for in the attribute + * @param wait int version of the timeout in seconds + * @return WebElement of the selector that is found + */ + public WebElement waitForAttributeTextEquals(final By by, final String attribute, final String text, + WebDriverWait wait) { + if (this.waitUntilAttribute(by, attribute, text, wait, false)) { + return this.driver.findElement(by); + } + + String error = String.format("Selector [%s] couldn't be found.%n", by.toString()); + throw new NotFoundException(error); + } + + /** + * Wait for an attribute of the specified selector to be present, and contain the specified text. + * + * @param by Selector to look for + * @param attribute String value of the attribute to look at on the specified selector + * @param text String value of the text to look for in the attribute + * @return true if the attribute with the specified text value is found, else false + */ + public boolean waitUntilAttributeTextContains(final By by, final String attribute, final String text) { + return waitUntilAttribute(by, attribute, text, getWaitDriver(), true); + } + + /** + * Wait for an attribute of the specified selector to be present, and contain the specified text. + * + * @param by Selector to look for + * @param attribute String value of the attribute to look at on the specified selector + * @param text String value of the text to look for in the attribute + * @param timeOutInMillis the number of milliseconds to wait before failing + * @param sleepInMillis the number of milliseconds to wait before a recheck + * @return true if the attribute with the specified text value is found, else false + */ + public boolean waitUntilAttributeTextContains(final By by, final String attribute, final String text, + final int timeOutInMillis, final int sleepInMillis) { + WebDriverWait wait = this.getNewWaitDriver(timeOutInMillis, sleepInMillis); + return waitUntilAttribute(by, attribute, text, wait, true); + } + + /** + * Wait for an attribute of the specified selector to be present, and contain the specified text. + * + * @param by Selector to look for + * @param attribute String value of the attribute to look at on the specified selector + * @param text String value of the text to look for in the attribute + * @param wait The wait driver + * @param contains boolean true if checking if contains, false if exact match + * @return true if the attribute with the specified text value is found, else false + */ + public boolean waitUntilAttribute(final By by, final String attribute, final String text, WebDriverWait wait, + final boolean contains) { + try { + return wait.until((ExpectedCondition<Boolean>) f -> attributeMatches(f, by, attribute, text, contains)); + } catch (NoSuchElementException | StaleElementReferenceException | TimeoutException e) { + return false; + } + } + + /** + * Scroll an element into view, and wait for it to be clickable. + * + * @param by Selector to wait for and focus on + * @return WebElement + */ + public WebElement waitForClickableElementAndScrollIntoView(final By by) { + return this.waitForClickableElementAndScrollIntoView(by, getWaitDriver()); + } + + /** + * Scroll an element into view, and wait for it to be clickable. + * + * @param by Selector to wait for and focus on + * @param timeOutInMillis the number of milliseconds to wait before failing + * @param sleepInMillis the number of milliseconds to wait before a recheck + * @return WebElement + */ + public WebElement waitForClickableElementAndScrollIntoView(final By by, final int timeOutInMillis, + final int sleepInMillis) { + WebDriverWait wait = this.getNewWaitDriver(timeOutInMillis, sleepInMillis); + return waitForClickableElementAndScrollIntoView(by, wait); + } + + /** + * Scroll an element into view, and wait for it to be clickable. + * + * @param by Selector to wait for and focus on + * @param wait int value of seconds to wait for before timing out + * @return WebElement + */ + public WebElement waitForClickableElementAndScrollIntoView(final By by, WebDriverWait wait) { + scrollIntoView(by); + waitUntilPageLoad(); + return waitForClickableElement(by, wait); + } + + /** + * Scroll an element into view, and wait for it to be clickable. + * + * @param by Selector to wait for and focus on + * @return boolean - true if the element is found and clickable + */ + public boolean waitUntilClickableElementAndScrollIntoView(final By by) { + return this.waitUntilClickableElementAndScrollIntoView(by, getWaitDriver()); + } + + /** + * Scroll an element into view, and wait for it to be clickable. + * + * @param by Selector to wait for and focus on + * @param timeOutInMillis the number of milliseconds to wait before failing + * @param sleepInMillis the number of milliseconds to wait before a recheck + * @return boolean - true if the element is found and clickable + */ + public boolean waitUntilClickableElementAndScrollIntoView(final By by, final int timeOutInMillis, + final int sleepInMillis) { + WebDriverWait wait = this.getNewWaitDriver(timeOutInMillis, sleepInMillis); + return waitUntilClickableElementAndScrollIntoView(by, wait); + } + + /** + * Scroll an element into view, and wait for it to be clickable. + * + * @param by Selector to wait for and focus on + * @param wait The wait driver + * @return boolean - true if the element is found and clickable + */ + public boolean waitUntilClickableElementAndScrollIntoView(final By by, WebDriverWait wait) { + scrollIntoView(by); + waitUntilPageLoad(); + return waitUntilClickableElement(by, wait); + } + + /** + * Wait for the specified selector to be clickable. + * + * @param by Selector to wait for + * @return boolean - true if found and clickable, else false + */ + public boolean waitUntilClickableElement(final By by) { + return this.waitUntilClickableElement(by, getWaitDriver()); + } + + /** + * Wait for the specified selector to be clickable. + * + * @param by Selector to wait for + * @param timeOutInMillis the number of milliseconds to wait before failing + * @param sleepInMillis the number of milliseconds to wait before a recheck + * @return boolean - true if found and clickable, else false + */ + public boolean waitUntilClickableElement(final By by, final int timeOutInMillis, final int sleepInMillis) { + WebDriverWait wait = this.getNewWaitDriver(timeOutInMillis, sleepInMillis); + return waitUntilClickableElement(by, wait); + } + + /** + * Wait for the specified selector to be clickable. + * + * @param by Selector to wait for + * @param wait The wait driver + * @return boolean - true if found and clickable, else false + */ + public boolean waitUntilClickableElement(final By by, WebDriverWait wait) { + try { + if (waitForClickableElement(by, wait) == null) { + return false; + } + } catch (Exception e) { + return false; + } + return true; + } + + /** + * Wait for the element specified by the provided selector to be clickable. + * + * @param by Selector to wait for to be clickable + * @return WebElement that is located, or null if none is found + */ + public WebElement waitForClickableElement(final By by) { + return this.waitForClickableElement(by, getWaitDriver()); + } + + /** + * Wait for the element specified by the provided selector to be clickable. + * + * @param by Selector to wait for to be clickable + * @param timeOutInMillis the number of milliseconds to wait before failing + * @param sleepInMillis the number of milliseconds to wait before a recheck + * @return WebElement that is located, or null if none is found + */ + public WebElement waitForClickableElement(final By by, final int timeOutInMillis, final int sleepInMillis) { + + WebDriverWait wait = this.getNewWaitDriver(timeOutInMillis, sleepInMillis); + return waitForClickableElement(by, wait); + } + + /** + * Wait for the element specified by the provided selector to be clickable. + * + * @param by Selector to wait for to be clickable + * @param wait The wait driver + * @return WebElement that is located, or null if none is found + */ + public WebElement waitForClickableElement(final By by, WebDriverWait wait) { + return wait.until(ExpectedConditions.elementToBeClickable(by)); + } + + /** + * Wait for the page to load by waiting for the source to stop changing for at least a second. + * Asserts the page loads + */ + public void waitForPageLoad() { + if (!this.waitUntilPageLoad()) { + throw new TimeoutException("Page load took longer than timeout configuration."); + } + } + + /** + * Wait for the page to load by waiting for the source to stop changing for at least a second. + * + * @return true if it's successfully loaded, false if timed out and not finished loading + */ + public boolean waitUntilPageLoad() { + String before = ""; + String after = ""; + int counter = this.timeout / this.fluentRetryTime; + + do { + try { + counter--; + before = this.driver.getPageSource(); + Thread.sleep(this.fluentRetryTime); + after = this.driver.getPageSource(); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } + } while (!before.equals(after) && counter > 0); + + return before.equals(after); + } + + /** + * Waits for the Frame to be available. + * + * @param by The frame locator + */ + public boolean waitUntilIframeToLoad(By by) { + return waitUntilIframeToLoad(by, this.getWaitDriver()); + } + + /** + * Waits for the Frame to be available. + * + * @param by The frame locator + * @param timeOutInMillis he number of milliseconds to wait before failing + * @param sleepInMillis the number of milliseconds to wait before a recheck + */ + public boolean waitUntilIframeToLoad(By by, final int timeOutInMillis, final int sleepInMillis) { + WebDriverWait wait = this.getNewWaitDriver(timeOutInMillis, sleepInMillis); + return waitUntilIframeToLoad(by, wait); + } + + /** + * Waits for the Frame to be available. + * + * @param by The frame locator + */ + public boolean waitUntilIframeToLoad(By by, WebDriverWait wait) { + + try { + waitForIframeToLoad(by, wait); + return true; + } catch (NoSuchElementException | StaleElementReferenceException | TimeoutException e) { + return false; + } + } + + /** + * Waits for the Frame to be available. + * + * @param by The frame locator + */ + public void waitForIframeToLoad(By by) { + waitForIframeToLoad(by, this.getWaitDriver()); + } + + /** + * Waits for the Frame to be available. + * + * @param by The frame locator + * @param timeOutInMillis the number of milliseconds to wait before failing + * @param sleepInMillis the number of milliseconds to wait before a recheck + */ + public void waitForIframeToLoad(By by, final int timeOutInMillis, final int sleepInMillis) { + WebDriverWait wait = this.getNewWaitDriver(timeOutInMillis, sleepInMillis); + waitForIframeToLoad(by, wait); + } + + /** + * Waits for the Frame to be available. + * + * @param by The frame locator + */ + public void waitForIframeToLoad(By by, WebDriverWait wait) { + wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(by)); + } + + /** + * Returns the webDriver of this SeleniumWait object. + * + * @return the webDriver (type WebDriver) of this SeleniumWait object. + */ + protected WebDriver getWebDriver() { + return driver; + } + + /** + * Gets a new WaitDriver using the default timeout. + * + * @return new WaitDriver + */ + protected WebDriverWait getNewWaitDriver(WebDriver driver) { + return getNewWaitDriver(driver, this.timeout, this.fluentRetryTime); + } + + /** + * Gets a new WaitDriver using the default timeout. + * + * @return new WaitDriver + */ + protected WebDriverWait getNewWaitDriver() { + return getNewWaitDriver(this.getWebDriver(), this.timeout, this.fluentRetryTime); + } + + /** + * Gets a new WaitDriver using the specified timeout. + * + * @param timeOutInMillis the default timeout + * @return new WaitDriver + */ + protected WebDriverWait getNewWaitDriver(int timeOutInMillis) { + return getNewWaitDriver(this.getWebDriver(), timeOutInMillis, this.fluentRetryTime); + } + + /** + * Gets a new WaitDriver using the specified timeout. + * + * @param timeOutInMillis the time to wait before a timeout + * @param sleepInMillis the time to wait before a recheck + * @return new WaitDriver + */ + protected WebDriverWait getNewWaitDriver(int timeOutInMillis, int sleepInMillis) { + return getNewWaitDriver(this.getWebDriver(), timeOutInMillis, sleepInMillis); + } + + /** + * Gets a new WaitDriver using the specified timeout. + * + * @param timeOutInMillis the time to wait before a timeout + * @param sleepInMillis the time to wait before a recheck + * @return new WaitDriver + */ + protected WebDriverWait getNewWaitDriver(WebDriver driver, int timeOutInMillis, int sleepInMillis) { + int timeoutInSeconds = timeOutInMillis / 1000; + WebDriverWait wait = new WebDriverWait(driver, timeoutInSeconds, sleepInMillis); + setWaitDriver(wait); + return wait; + } + + /** + * Scroll the web page so the selector is visible. + * + * @param by Selector to make visible + */ + private void scrollIntoView(By by) { + WebElement element = waitForVisibleElement(by); + int counter = 0; + final int max = 5; + + try { + ((JavascriptExecutor) this.driver).executeScript("arguments[0].scrollIntoView(true);", element); + } catch (Exception e) { + String error = String.format("Failed JavaScript scroll into view...%s%n", (Object[]) e.getStackTrace()); + System.err.print(error); + } + + Coordinates coord = ((Locatable) element).getCoordinates(); + while (coord.inViewPort().getY() < HEADER_SIZE && counter < max) { + waitForVisibleElement(BODY_BY).sendKeys(Keys.ARROW_UP); + counter++; + } + } + + /** + * Check the text value of an attribute. + * + * @param driver - web driver + * @param by Selector to get the attribute from + * @param attribute String value of the attribute to examine + * @param text String to check against the attribute + * @param contains boolean - true to see if the string is contained, false if exact match + * @return boolean - true if they match, else false + */ + private static boolean attributeMatches(WebDriver driver, By by, String attribute, String text, boolean contains) { + try { + if (contains) { + return driver.findElement(by).getAttribute(attribute).contains(text); + } else { + return driver.findElement(by).getAttribute(attribute).equals(text); + } + } catch (NullPointerException e) { + return false; + } + } + + /** + * Checks if the text of the elements are equal + * + * @param by Selector to examine + * @param text Text that is being compared to the selector + * @return boolean - true if equal + */ + private boolean doesTextMatch(By by, String text) { + try { + WebElement element = this.waitForVisibleElement(by); + + if (element != null && element.getText().equals(text)) { + return true; + } + } catch (Exception e) { + return false; + } + return false; + } + + /** + * Checks if the text of the elements are equal. + * + * @param by Selector to examine + * @param text Text that is being compared to the selector + * @return boolean - true if equal + */ + private boolean doesContainsText(By by, String text) { + try { + WebElement element = this.waitForVisibleElement(by); + + if (element != null && element.getText().contains(text)) { + return true; + } + } catch (Exception e) { + return false; + } + return false; + } } \ No newline at end of file diff --git a/jmaqs-selenium/src/main/java/com/magenic/jmaqs/selenium/factories/FluentWaitFactory.java b/jmaqs-selenium/src/main/java/com/magenic/jmaqs/selenium/factories/FluentWaitFactory.java index e7ba8489d..a6a222c0b 100644 --- a/jmaqs-selenium/src/main/java/com/magenic/jmaqs/selenium/factories/FluentWaitFactory.java +++ b/jmaqs-selenium/src/main/java/com/magenic/jmaqs/selenium/factories/FluentWaitFactory.java @@ -4,9 +4,8 @@ package com.magenic.jmaqs.selenium.factories; -import java.time.Duration; - import com.magenic.jmaqs.selenium.SeleniumConfig; +import java.time.Duration; import org.openqa.selenium.NotFoundException; import org.openqa.selenium.WebElement; import org.openqa.selenium.support.ui.FluentWait; @@ -29,24 +28,20 @@ private FluentWaitFactory() { * @param sleepInMillis the polling milliseconds before retry * @return new fluent wait */ - public static FluentWait<WebElement> getNewElementFluentWait(WebElement element, - int timeOutInMillis, int sleepInMillis) { - return new FluentWait<>(element) - .withTimeout(Duration.ofMillis(timeOutInMillis)) - .pollingEvery(Duration.ofMillis(sleepInMillis)) - .ignoring(NotFoundException.class); + public static FluentWait<WebElement> getNewElementFluentWait(WebElement element, int timeOutInMillis, + int sleepInMillis) { + return new FluentWait<>(element).withTimeout(Duration.ofMillis(timeOutInMillis)) + .pollingEvery(Duration.ofMillis(sleepInMillis)).ignoring(NotFoundException.class); } /** * Returns a new {@link org.openqa.selenium.support.ui.FluentWait FluentWait} object. * - * @param element the element + * @param element the element * @return new fluent wait */ public static FluentWait<WebElement> getNewElementFluentWait(WebElement element) { - return getNewElementFluentWait( - element, - (int)SeleniumConfig.getTimeoutTime().toMillis(), - (int)SeleniumConfig.getWaitTime().toMillis()); + return getNewElementFluentWait(element, (int) SeleniumConfig.getTimeoutTime().toMillis(), + (int) SeleniumConfig.getWaitTime().toMillis()); } } diff --git a/jmaqs-selenium/src/main/java/com/magenic/jmaqs/selenium/factories/UIWaitFactory.java b/jmaqs-selenium/src/main/java/com/magenic/jmaqs/selenium/factories/UIWaitFactory.java index bd3a474c5..3153b84bf 100644 --- a/jmaqs-selenium/src/main/java/com/magenic/jmaqs/selenium/factories/UIWaitFactory.java +++ b/jmaqs-selenium/src/main/java/com/magenic/jmaqs/selenium/factories/UIWaitFactory.java @@ -1,104 +1,113 @@ -package com.magenic.jmaqs.selenium.factories; +/* + * Copyright 2020 (C) Magenic, All rights Reserved + */ -import java.util.concurrent.ConcurrentHashMap; +package com.magenic.jmaqs.selenium.factories; import com.magenic.jmaqs.selenium.SeleniumConfig; +import com.magenic.jmaqs.selenium.SeleniumUtilities; import com.magenic.jmaqs.selenium.UIWait; +import java.util.concurrent.ConcurrentHashMap; import org.openqa.selenium.SearchContext; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; - -import com.magenic.jmaqs.selenium.SeleniumUtilities; import org.openqa.selenium.support.ui.WebDriverWait; /** - * Factory class that is used for creating and maintaining a - * thread-safe collection of wait drivers. + * Factory class that is used for creating and maintaining a + * thread-safe collection of wait drivers. */ public class UIWaitFactory { - - /** the collection of wait objects */ - private static ConcurrentHashMap<WebDriver, WebDriverWait> waitCollection = new ConcurrentHashMap<>(); - - /** private constructor so class can't be instantiated */ - private UIWaitFactory() { } - - /** - * Creates a {@link UIWait} object using it's cached WebDriverWait if the search context. - * already exists in the wait collection. If none exist, then a new - * one is created and cached using the driver provided. - * - * @param searchContext The web driver to associate with the wait - * @return the Wait object - */ - public static UIWait getWaitDriver(SearchContext searchContext) { - WebDriver baseDriver = getLowLevelDriver(searchContext); - return new UIWait(baseDriver, getWebDriverWait(baseDriver)); - } - /** - * Gets the WebDriverWait for building the UIWait object - * @param searchContext The search context - * @return The WebDriverWait - */ - private static WebDriverWait getWebDriverWait(SearchContext searchContext) { - WebDriver unwrappedDriver = getLowLevelDriver(searchContext); + /** + * the collection of wait objects + */ + private static ConcurrentHashMap<WebDriver, WebDriverWait> waitCollection = new ConcurrentHashMap<>(); + + /** + * private constructor so class can't be instantiated + */ + private UIWaitFactory() { + } + + /** + * Creates a {@link UIWait} object using it's cached WebDriverWait if the search context. + * already exists in the wait collection. If none exist, then a new + * one is created and cached using the driver provided. + * + * @param searchContext The web driver to associate with the wait + * @return the Wait object + */ + public static UIWait getWaitDriver(SearchContext searchContext) { + WebDriver baseDriver = getLowLevelDriver(searchContext); + return new UIWait(baseDriver, getWebDriverWait(baseDriver)); + } + + /** + * Gets the {@link UIWait} object from the wait collection. If none exists, + * one is created and cached using the driver provided. + * + * @param searchContext The web driver to associate with the wait + * @param timeOutInSeconds The timeout for the UIWait + * @param fluentRetryTime The fluent retry time for the UIWait + * @return the Wait object + */ + public static UIWait getWaitDriver(SearchContext searchContext, final int timeOutInSeconds, + final int fluentRetryTime) { + WebDriver baseDriver = getLowLevelDriver(searchContext); + return new UIWait(baseDriver, timeOutInSeconds, fluentRetryTime, getWebDriverWait(baseDriver)); + } + + /** + * Gets the WebDriverWait for building the UIWait object + * + * @param searchContext The search context + * @return The WebDriverWait + */ + private static WebDriverWait getWebDriverWait(SearchContext searchContext) { + WebDriver unwrappedDriver = getLowLevelDriver(searchContext); + + if (waitCollection.containsKey(unwrappedDriver)) { + return waitCollection.get(unwrappedDriver); + } else { + WebDriverWait waitDriver = new WebDriverWait(unwrappedDriver, SeleniumConfig.getTimeoutTime().getSeconds()); + setWaitDriver(unwrappedDriver, waitDriver); + return waitDriver; + } + } + + /** + * Adds the waitDriver to the collection + * + * @param driver The web driver + * @param waitDriver the Wait object + */ + public static void setWaitDriver(SearchContext driver, WebDriverWait waitDriver) { + WebDriver baseDriver = getLowLevelDriver(driver); + + waitCollection.put(baseDriver, waitDriver); + } + + /** + * Removes the waitDriver from the collection + * + * @param driver the web driver + */ + public static void removeWaitDriver(SearchContext driver) { + WebDriver baseDriver = getLowLevelDriver(driver); - if (waitCollection.containsKey(unwrappedDriver)) { - return waitCollection.get(unwrappedDriver); - } - else { - WebDriverWait waitDriver = new WebDriverWait(unwrappedDriver, SeleniumConfig.getTimeoutTime().getSeconds()); - setWaitDriver(unwrappedDriver, waitDriver); - return waitDriver; - } - } + waitCollection.remove(baseDriver); + } - /** - * Gets the {@link UIWait} object from the wait collection. If none exists, - * one is created and cached using the driver provided. - * - * @param searchContext The web driver to associate with the wait - * @param timeOutInSeconds The timeout for the UIWait - * @param fluentRetryTime The fluent retry time for the UIWait - * @return the Wait object - */ - public static UIWait getWaitDriver(SearchContext searchContext, final int timeOutInSeconds, final int fluentRetryTime) { - WebDriver baseDriver = getLowLevelDriver(searchContext); - return new UIWait(baseDriver, timeOutInSeconds, fluentRetryTime, getWebDriverWait(baseDriver)); - } - - /** - * Adds the waitDriver to the collection - * - * @param driver The web driver - * @param waitDriver the Wait object - */ - public static void setWaitDriver(SearchContext driver, WebDriverWait waitDriver) { - WebDriver baseDriver = getLowLevelDriver(driver); - - waitCollection.put(baseDriver, waitDriver); - } - - /** - * Removes the waitDriver from the collection - * - * @param driver the web driver - */ - public static void removeWaitDriver(SearchContext driver) { - WebDriver baseDriver = getLowLevelDriver(driver); - - waitCollection.remove(baseDriver); - } - - /** - * Get the underlying web driver. - * - * @param searchContext The web driver - * @return the underlying web driver - */ - private static WebDriver getLowLevelDriver(SearchContext searchContext) { - return (searchContext instanceof WebDriver) ? - (WebDriver)searchContext : SeleniumUtilities.webElementToWebDriver((WebElement)searchContext); - } + /** + * Get the underlying web driver. + * + * @param searchContext The web driver + * @return the underlying web driver + */ + private static WebDriver getLowLevelDriver(SearchContext searchContext) { + return (searchContext instanceof WebDriver) + ? (WebDriver) searchContext + : SeleniumUtilities.webElementToWebDriver((WebElement) searchContext); + } } diff --git a/jmaqs-utilities/src/main/java/com/magenic/jmaqs/utilities/helper/Config.java b/jmaqs-utilities/src/main/java/com/magenic/jmaqs/utilities/helper/Config.java index 6dda56cb3..02c752eeb 100644 --- a/jmaqs-utilities/src/main/java/com/magenic/jmaqs/utilities/helper/Config.java +++ b/jmaqs-utilities/src/main/java/com/magenic/jmaqs/utilities/helper/Config.java @@ -8,7 +8,6 @@ import java.util.HashMap; import java.util.Iterator; import java.util.Map; - import org.apache.commons.configuration2.XMLConfiguration; import org.apache.commons.configuration2.builder.FileBasedConfigurationBuilder; import org.apache.commons.configuration2.builder.fluent.Configurations; diff --git a/jmaqs-utilities/src/main/java/com/magenic/jmaqs/utilities/helper/exceptions/ExecutionFailedException.java b/jmaqs-utilities/src/main/java/com/magenic/jmaqs/utilities/helper/exceptions/ExecutionFailedException.java index 8268374b9..2f9ec7a73 100644 --- a/jmaqs-utilities/src/main/java/com/magenic/jmaqs/utilities/helper/exceptions/ExecutionFailedException.java +++ b/jmaqs-utilities/src/main/java/com/magenic/jmaqs/utilities/helper/exceptions/ExecutionFailedException.java @@ -1,17 +1,21 @@ +/* + * Copyright 2020 (C) Magenic, All rights Reserved + */ + package com.magenic.jmaqs.utilities.helper.exceptions; public class ExecutionFailedException extends Exception { - private static final long serialVersionUID = 1; + private static final long serialVersionUID = 1; - /** - * Instantiates the Execution Failed Exception. This exception is thrown when a - * function that is invoked fails. - * - * @param message The message for the exception - * @param exception The exception that was thrown by the failed event - */ - public ExecutionFailedException(String message, Exception exception) { - super(message, exception); - } + /** + * Instantiates the Execution Failed Exception. This exception is thrown when a + * function that is invoked fails. + * + * @param message The message for the exception + * @param exception The exception that was thrown by the failed event + */ + public ExecutionFailedException(String message, Exception exception) { + super(message, exception); + } } diff --git a/jmaqs-utilities/src/main/java/com/magenic/jmaqs/utilities/logging/FileLogger.java b/jmaqs-utilities/src/main/java/com/magenic/jmaqs/utilities/logging/FileLogger.java index ee7522446..e0f65fddb 100644 --- a/jmaqs-utilities/src/main/java/com/magenic/jmaqs/utilities/logging/FileLogger.java +++ b/jmaqs-utilities/src/main/java/com/magenic/jmaqs/utilities/logging/FileLogger.java @@ -20,7 +20,7 @@ public class FileLogger extends Logger { /** - * Default extension type + * Default extension type. */ private static final String TXT = ".txt"; From 6faf5540fdac0aa0a2c00aa0f10d71d88c3932f3 Mon Sep 17 00:00:00 2001 From: Jason Edstrom <JasonE@magenic.com> Date: Thu, 14 May 2020 16:42:02 -0500 Subject: [PATCH 02/12] JMAQS #309 - Copyrights --- .../src/main/java/com/magenic/jmaqs/selenium/UIWait.java | 2 +- .../com/magenic/jmaqs/selenium/factories/FluentWaitFactory.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/jmaqs-selenium/src/main/java/com/magenic/jmaqs/selenium/UIWait.java b/jmaqs-selenium/src/main/java/com/magenic/jmaqs/selenium/UIWait.java index 554ed11e0..38f53adc7 100644 --- a/jmaqs-selenium/src/main/java/com/magenic/jmaqs/selenium/UIWait.java +++ b/jmaqs-selenium/src/main/java/com/magenic/jmaqs/selenium/UIWait.java @@ -1,5 +1,5 @@ /* - * Copyright 2019 (C) Magenic, All rights Reserved + * Copyright 2020 (C) Magenic, All rights Reserved */ package com.magenic.jmaqs.selenium; diff --git a/jmaqs-selenium/src/main/java/com/magenic/jmaqs/selenium/factories/FluentWaitFactory.java b/jmaqs-selenium/src/main/java/com/magenic/jmaqs/selenium/factories/FluentWaitFactory.java index a6a222c0b..a790b2889 100644 --- a/jmaqs-selenium/src/main/java/com/magenic/jmaqs/selenium/factories/FluentWaitFactory.java +++ b/jmaqs-selenium/src/main/java/com/magenic/jmaqs/selenium/factories/FluentWaitFactory.java @@ -1,5 +1,5 @@ /* - * Copyright 2019 (C) Magenic, All rights Reserved + * Copyright 2020 (C) Magenic, All rights Reserved */ package com.magenic.jmaqs.selenium.factories; From 2b1cb1824ff7d2df756677b1968e397b2481c4f5 Mon Sep 17 00:00:00 2001 From: Jason Edstrom <JasonE@magenic.com> Date: Thu, 14 May 2020 17:04:51 -0500 Subject: [PATCH 03/12] JMAQS #309 - Enum changes for sonarlint --- .../magenic/jmaqs/appium/AppiumConfig.java | 4 +- .../jmaqs/appium/AppiumConfigUnitTest.java | 2 +- .../jmaqs/database/DatabaseConfig.java | 4 +- .../jmaqs/selenium/SeleniumConfig.java | 4 +- .../com/magenic/jmaqs/selenium/UIWait.java | 8 ++-- .../jmaqs/utilities/helper/Config.java | 2 +- .../jmaqs/utilities/helper/ConfigSection.java | 40 ++++++++++++------- .../jmaqs/utilities/ConfigUnitTest.java | 10 ++--- .../webservices/jdk8/WebServiceConfig.java | 6 +-- 9 files changed, 44 insertions(+), 36 deletions(-) diff --git a/jmaqs-appium/src/main/java/com/magenic/jmaqs/appium/AppiumConfig.java b/jmaqs-appium/src/main/java/com/magenic/jmaqs/appium/AppiumConfig.java index 9dd48b3af..43665e0d7 100644 --- a/jmaqs-appium/src/main/java/com/magenic/jmaqs/appium/AppiumConfig.java +++ b/jmaqs-appium/src/main/java/com/magenic/jmaqs/appium/AppiumConfig.java @@ -22,12 +22,12 @@ public class AppiumConfig { /** * The appium configuration section. */ - private static final ConfigSection APPIUM_SECTION = ConfigSection.AppiumMaqs; + private static final ConfigSection APPIUM_SECTION = ConfigSection.APPIUM_MAQS; /** * The appium capabilities configuration section. */ - private static final ConfigSection APPIUM_CAPS_SECTION = ConfigSection.AppiumCapsMaqs; + private static final ConfigSection APPIUM_CAPS_SECTION = ConfigSection.APPIUM_CAPS_MAQS; private AppiumConfig() { diff --git a/jmaqs-appium/src/test/java/com/magenic/jmaqs/appium/AppiumConfigUnitTest.java b/jmaqs-appium/src/test/java/com/magenic/jmaqs/appium/AppiumConfigUnitTest.java index 4836472f4..50f079cac 100644 --- a/jmaqs-appium/src/test/java/com/magenic/jmaqs/appium/AppiumConfigUnitTest.java +++ b/jmaqs-appium/src/test/java/com/magenic/jmaqs/appium/AppiumConfigUnitTest.java @@ -143,7 +143,7 @@ public void testGetCommandTimeout() { public void testGetCommandTimeoutError() { HashMap<String, String> configValues = new HashMap<>(); configValues.put("MobileCommandTimeout", "sixty thousand"); - Config.addTestSettingValues(configValues, ConfigSection.AppiumMaqs, true); + Config.addTestSettingValues(configValues, ConfigSection.APPIUM_MAQS, true); AppiumConfig.getCommandTimeout(); } diff --git a/jmaqs-database/src/main/java/com/magenic/jmaqs/database/DatabaseConfig.java b/jmaqs-database/src/main/java/com/magenic/jmaqs/database/DatabaseConfig.java index f8b8e8217..07579d3ac 100644 --- a/jmaqs-database/src/main/java/com/magenic/jmaqs/database/DatabaseConfig.java +++ b/jmaqs-database/src/main/java/com/magenic/jmaqs/database/DatabaseConfig.java @@ -24,12 +24,12 @@ private DatabaseConfig() { /** * The Database section. */ - private static final ConfigSection DATABASE_SECTION = ConfigSection.DatabaseMaqs; + private static final ConfigSection DATABASE_SECTION = ConfigSection.DATABASE_MAQS; /** * Field DATABASE_CAPS_SECTION. */ - private static final ConfigSection DATABASE_CAPS_SECTION = ConfigSection.DatabaseCapsMaqs; + private static final ConfigSection DATABASE_CAPS_SECTION = ConfigSection.DATABASE_CAPS_MAQS; /** * Gets connection string. diff --git a/jmaqs-selenium/src/main/java/com/magenic/jmaqs/selenium/SeleniumConfig.java b/jmaqs-selenium/src/main/java/com/magenic/jmaqs/selenium/SeleniumConfig.java index 265a87b75..28e511c1e 100644 --- a/jmaqs-selenium/src/main/java/com/magenic/jmaqs/selenium/SeleniumConfig.java +++ b/jmaqs-selenium/src/main/java/com/magenic/jmaqs/selenium/SeleniumConfig.java @@ -28,12 +28,12 @@ private SeleniumConfig() { /** * The web service configuration section. */ - public static final ConfigSection SELENIUM_SECTION = ConfigSection.SeleniumMaqs; + public static final ConfigSection SELENIUM_SECTION = ConfigSection.SELENIUM_MAQS; /** * The remote selenium configuration section. */ - private static final ConfigSection SELENIUM_CAPS_SECTION = ConfigSection.RemoteSeleniumCapsMaqs; + private static final ConfigSection SELENIUM_CAPS_SECTION = ConfigSection.REMOTE_SELENIUM_CAPS_MAQS; /** * Get the browser type. diff --git a/jmaqs-selenium/src/main/java/com/magenic/jmaqs/selenium/UIWait.java b/jmaqs-selenium/src/main/java/com/magenic/jmaqs/selenium/UIWait.java index 38f53adc7..740e5a96d 100644 --- a/jmaqs-selenium/src/main/java/com/magenic/jmaqs/selenium/UIWait.java +++ b/jmaqs-selenium/src/main/java/com/magenic/jmaqs/selenium/UIWait.java @@ -69,13 +69,13 @@ public class UIWait { * @param driver WebDriver */ public UIWait(WebDriver driver) { - this(driver, Integer.parseInt(Config.getValueForSection(ConfigSection.SeleniumMaqs, "BrowserTimeout", "30000")), - Integer.parseInt(Config.getValueForSection(ConfigSection.SeleniumMaqs, "BrowserWaitTime", "1000")), null); + this(driver, Integer.parseInt(Config.getValueForSection(ConfigSection.SELENIUM_MAQS, "BrowserTimeout", "30000")), + Integer.parseInt(Config.getValueForSection(ConfigSection.SELENIUM_MAQS, "BrowserWaitTime", "1000")), null); } public UIWait(WebDriver driver, WebDriverWait waitDriver) { - this(driver, Integer.parseInt(Config.getValueForSection(ConfigSection.SeleniumMaqs, "BrowserTimeout", "30000")), - Integer.parseInt(Config.getValueForSection(ConfigSection.SeleniumMaqs, "BrowserWaitTime", "1000")), waitDriver); + this(driver, Integer.parseInt(Config.getValueForSection(ConfigSection.SELENIUM_MAQS, "BrowserTimeout", "30000")), + Integer.parseInt(Config.getValueForSection(ConfigSection.SELENIUM_MAQS, "BrowserWaitTime", "1000")), waitDriver); } /** diff --git a/jmaqs-utilities/src/main/java/com/magenic/jmaqs/utilities/helper/Config.java b/jmaqs-utilities/src/main/java/com/magenic/jmaqs/utilities/helper/Config.java index 02c752eeb..b4109d376 100644 --- a/jmaqs-utilities/src/main/java/com/magenic/jmaqs/utilities/helper/Config.java +++ b/jmaqs-utilities/src/main/java/com/magenic/jmaqs/utilities/helper/Config.java @@ -31,7 +31,7 @@ private Config() { /** * The default section MagenicMaqs. */ - public static final ConfigSection DEFAULT_MAQS_SECTION = ConfigSection.MagenicMaqs; + public static final ConfigSection DEFAULT_MAQS_SECTION = ConfigSection.MAGENIC_MAQS; /** * The default config.xml file name. diff --git a/jmaqs-utilities/src/main/java/com/magenic/jmaqs/utilities/helper/ConfigSection.java b/jmaqs-utilities/src/main/java/com/magenic/jmaqs/utilities/helper/ConfigSection.java index 791bb4863..b1c5f223a 100644 --- a/jmaqs-utilities/src/main/java/com/magenic/jmaqs/utilities/helper/ConfigSection.java +++ b/jmaqs-utilities/src/main/java/com/magenic/jmaqs/utilities/helper/ConfigSection.java @@ -10,48 +10,58 @@ public enum ConfigSection { /** - * The default magenic maqs section. + * The default appium capabilities section. */ - MagenicMaqs, + APPIUM_CAPS_MAQS("AppiumCapsMaqs"), /** * The default appium maqs section. */ - AppiumMaqs, + APPIUM_MAQS("AppiumMaqs"), /** - * The default appium capabilities section. + * Database Caps Section. */ - AppiumCapsMaqs, + DATABASE_CAPS_MAQS("DatabaseCapsMaqs"), /** * The default database maqs section. */ - DatabaseMaqs, + DATABASE_MAQS("DatabaseMaqs"), /** - * Database Caps Section. + * The default email maqs section. */ - DatabaseCapsMaqs, + EMAIL_MAQS("EmailMaqs"), /** - * The default email maqs section. + * The default magenic maqs section. */ - EmailMaqs, + MAGENIC_MAQS("MagenicMaqs"), /** - * The default selenium maqs section. + * The default remote selenium capabilities section. */ - SeleniumMaqs, + REMOTE_SELENIUM_CAPS_MAQS("RemoteSeleniumCapsMaqs"), /** - * The default remote selenium capabilities section. + * The default selenium maqs section. */ - RemoteSeleniumCapsMaqs, + SELENIUM_MAQS("SeleniumMaqs"), /** * The default web service section. */ - WebServiceMaqs; + WEB_SERVICE_MAQS("WebServiceMaqs"); + + private final String sectionName; + + ConfigSection(String sectionName) { + this.sectionName = sectionName; + } + @Override + public String toString() { + return sectionName; + } } diff --git a/jmaqs-utilities/src/test/java/com/magenic/jmaqs/utilities/ConfigUnitTest.java b/jmaqs-utilities/src/test/java/com/magenic/jmaqs/utilities/ConfigUnitTest.java index 2edae8fd6..5b079147f 100644 --- a/jmaqs-utilities/src/test/java/com/magenic/jmaqs/utilities/ConfigUnitTest.java +++ b/jmaqs-utilities/src/test/java/com/magenic/jmaqs/utilities/ConfigUnitTest.java @@ -7,9 +7,7 @@ import com.magenic.jmaqs.utilities.helper.Config; import com.magenic.jmaqs.utilities.helper.ConfigSection; import com.magenic.jmaqs.utilities.helper.TestCategories; - import java.util.HashMap; - import java.util.Map; import org.testng.Assert; import org.testng.annotations.Test; @@ -23,7 +21,7 @@ public class ConfigUnitTest { */ @Test(groups = TestCategories.UTILITIES) public void getSectionWithConfigSecEnumTest() { - Map<String, String> testSection = Config.getSection(ConfigSection.SeleniumMaqs); + Map<String, String> testSection = Config.getSection(ConfigSection.SELENIUM_MAQS); Assert.assertEquals(testSection.get("TestKey"), "testValueTwo"); Assert.assertEquals(testSection.get("Browser"), "Internet Explorer"); } @@ -102,7 +100,7 @@ public void getGeneralValueTest() { @Test(groups = TestCategories.UTILITIES) public void getValueForSectionTest() { Assert.assertEquals(Config.getValueForSection("SeleniumMaqs", "TestKey"), "testValueTwo"); - Assert.assertEquals(Config.getValueForSection(ConfigSection.SeleniumMaqs, "Browser"), "Internet Explorer"); + Assert.assertEquals(Config.getValueForSection(ConfigSection.SELENIUM_MAQS, "Browser"), "Internet Explorer"); Assert.assertEquals(Config.getValueForSection("SeleniumMaqs", "nonExistentKey", "defaultValue"), "defaultValue"); } @@ -122,7 +120,7 @@ public void getValueTest() { public void doesKeyExistTest() { Assert.assertTrue(Config.doesKeyExist("SeleniumMaqs.TestKey")); Assert.assertTrue(Config.doesGeneralKeyExist("TimeoutOverride")); - Assert.assertTrue(Config.doesKeyExist("HubAddress", ConfigSection.SeleniumMaqs)); - Assert.assertFalse(Config.doesKeyExist("HubAddress", ConfigSection.MagenicMaqs)); + Assert.assertTrue(Config.doesKeyExist("HubAddress", ConfigSection.SELENIUM_MAQS)); + Assert.assertFalse(Config.doesKeyExist("HubAddress", ConfigSection.MAGENIC_MAQS)); } } diff --git a/jmaqs-webservices-jdk8/src/main/java/com/magenic/jmaqs/webservices/jdk8/WebServiceConfig.java b/jmaqs-webservices-jdk8/src/main/java/com/magenic/jmaqs/webservices/jdk8/WebServiceConfig.java index 83844566d..d26f5360d 100644 --- a/jmaqs-webservices-jdk8/src/main/java/com/magenic/jmaqs/webservices/jdk8/WebServiceConfig.java +++ b/jmaqs-webservices-jdk8/src/main/java/com/magenic/jmaqs/webservices/jdk8/WebServiceConfig.java @@ -18,7 +18,7 @@ private WebServiceConfig() { /** * The web service configuration section. */ - private static final ConfigSection WEBSERVICE_SECTION = ConfigSection.WebServiceMaqs; + private static final ConfigSection WEBSERVICE_SECTION = ConfigSection.WEB_SERVICE_MAQS; /** * Grabs the URI for the Web Service. @@ -45,7 +45,7 @@ public static int getWebServiceTimeOut() { * @return True if we want to use the proxy */ public static boolean getUseProxy() { - return Config.getValueForSection(ConfigSection.WebServiceMaqs, "UseProxy", "No").equals("Yes"); + return Config.getValueForSection(ConfigSection.WEB_SERVICE_MAQS, "UseProxy", "No").equals("Yes"); } /** @@ -54,6 +54,6 @@ public static boolean getUseProxy() { * @return The proxy address */ public static String getProxyAddress() { - return Config.getValueForSection(ConfigSection.WebServiceMaqs, "ProxyAddress"); + return Config.getValueForSection(ConfigSection.WEB_SERVICE_MAQS, "ProxyAddress"); } } From fe75952ddd92a4d4c95dbb33102ae174f1c0b37f Mon Sep 17 00:00:00 2001 From: Jason Edstrom <JasonE@magenic.com> Date: Thu, 14 May 2020 17:05:24 -0500 Subject: [PATCH 04/12] JMAQS #309 - Enabled Checkstyle in maven builds --- pom.xml | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/pom.xml b/pom.xml index 3f3b43991..352baf19d 100644 --- a/pom.xml +++ b/pom.xml @@ -1,3 +1,7 @@ +<!-- + ~ Copyright 2020 (C) Magenic, All rights Reserved + --> + <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> @@ -277,7 +281,7 @@ <artifactId>maven-deploy-plugin</artifactId> <version>${maven-deploy-plugin.version}</version> </plugin> - <!--<plugin> + <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-checkstyle-plugin</artifactId> <version>${mvncheckstyle.plugin.version}</version> @@ -297,7 +301,7 @@ <phase>validate</phase> <configuration> <failOnWarnings>false</failOnWarnings> - <failOnError>true</failOnError> + <failOnError>true</failOnError> <configLocation>maqs_checks.xml</configLocation> <encoding>${project.build.sourceEncoding}</encoding> <consoleOutput>true</consoleOutput> @@ -306,7 +310,7 @@ </configuration> </execution> </executions> - </plugin>--> + </plugin> </plugins> </build> </project> From c2c78728989da7b8aecf5cf9261843072106f032 Mon Sep 17 00:00:00 2001 From: Jason Edstrom <JasonE@magenic.com> Date: Thu, 14 May 2020 17:10:22 -0500 Subject: [PATCH 05/12] JMAQS #309 - Removed unneeded casts --- .../main/java/com/magenic/jmaqs/selenium/UIWait.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/jmaqs-selenium/src/main/java/com/magenic/jmaqs/selenium/UIWait.java b/jmaqs-selenium/src/main/java/com/magenic/jmaqs/selenium/UIWait.java index 740e5a96d..ecd37d686 100644 --- a/jmaqs-selenium/src/main/java/com/magenic/jmaqs/selenium/UIWait.java +++ b/jmaqs-selenium/src/main/java/com/magenic/jmaqs/selenium/UIWait.java @@ -22,7 +22,6 @@ import org.openqa.selenium.WebElement; import org.openqa.selenium.interactions.Coordinates; import org.openqa.selenium.interactions.Locatable; -import org.openqa.selenium.support.ui.ExpectedCondition; import org.openqa.selenium.support.ui.ExpectedConditions; import org.openqa.selenium.support.ui.FluentWait; import org.openqa.selenium.support.ui.WebDriverWait; @@ -75,7 +74,8 @@ public UIWait(WebDriver driver) { public UIWait(WebDriver driver, WebDriverWait waitDriver) { this(driver, Integer.parseInt(Config.getValueForSection(ConfigSection.SELENIUM_MAQS, "BrowserTimeout", "30000")), - Integer.parseInt(Config.getValueForSection(ConfigSection.SELENIUM_MAQS, "BrowserWaitTime", "1000")), waitDriver); + Integer.parseInt(Config.getValueForSection(ConfigSection.SELENIUM_MAQS, "BrowserWaitTime", "1000")), + waitDriver); } /** @@ -482,7 +482,7 @@ public boolean waitUntilExactText(final By by, final String text, final int time */ public boolean waitUntilExactText(final By by, final String text, WebDriverWait wait) { try { - return wait.until((ExpectedCondition<Boolean>) function -> doesTextMatch(by, text)); + return wait.until(function -> doesTextMatch(by, text)); } catch (NoSuchElementException | StaleElementReferenceException | TimeoutException e) { return false; } @@ -539,7 +539,7 @@ public boolean waitUntilContainsText(final By by, final String text, final int t */ public boolean waitUntilContainsText(final By by, final String text, WebDriverWait wait) { try { - return wait.until((ExpectedCondition<Boolean>) function -> doesContainsText(by, text)); + return wait.until(function -> doesContainsText(by, text)); } catch (NoSuchElementException | StaleElementReferenceException | TimeoutException e) { return false; } @@ -661,7 +661,7 @@ public boolean waitUntilAttributeTextContains(final By by, final String attribut public boolean waitUntilAttribute(final By by, final String attribute, final String text, WebDriverWait wait, final boolean contains) { try { - return wait.until((ExpectedCondition<Boolean>) f -> attributeMatches(f, by, attribute, text, contains)); + return wait.until(f -> attributeMatches(f, by, attribute, text, contains)); } catch (NoSuchElementException | StaleElementReferenceException | TimeoutException e) { return false; } From e28ee58ade9d9c3df0fbb3ff44a4f7edb994df10 Mon Sep 17 00:00:00 2001 From: Jason Edstrom <JasonE@magenic.com> Date: Thu, 14 May 2020 17:10:52 -0500 Subject: [PATCH 06/12] JMAQS #309 - Replaced string with constant variables --- .../jmaqs/utilities/logging/LoggingConfig.java | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/jmaqs-utilities/src/main/java/com/magenic/jmaqs/utilities/logging/LoggingConfig.java b/jmaqs-utilities/src/main/java/com/magenic/jmaqs/utilities/logging/LoggingConfig.java index 646d6c3e5..ef0cf3541 100644 --- a/jmaqs-utilities/src/main/java/com/magenic/jmaqs/utilities/logging/LoggingConfig.java +++ b/jmaqs-utilities/src/main/java/com/magenic/jmaqs/utilities/logging/LoggingConfig.java @@ -13,6 +13,9 @@ */ public class LoggingConfig { + public static final String CONSOLE = "CONSOLE"; + public static final String TXT = "TXT"; + private LoggingConfig() { //Hiding implicit contructor } @@ -80,14 +83,14 @@ public static Logger getLogger(String fileName) { String logDirectory = getLogDirectory(); MessageType loggingLevel = getLoggingLevelSetting(); - switch (Config.getGeneralValue("LogType", "CONSOLE").toUpperCase()) { - case "CONSOLE": + switch (Config.getGeneralValue("LogType", CONSOLE).toUpperCase()) { + case CONSOLE: return new ConsoleLogger(loggingLevel); - case "TXT": + case TXT: return new FileLogger(false, logDirectory, fileName, loggingLevel); default: throw new IllegalArgumentException(StringProcessor - .safeFormatter("Log type %s is not a valid option", Config.getGeneralValue("LogType", "CONSOLE"))); + .safeFormatter("Log type %s is not a valid option", Config.getGeneralValue("LogType", CONSOLE))); } } From 692029bac5659d97f149526690992ce389168bce Mon Sep 17 00:00:00 2001 From: Jason Edstrom <JasonE@magenic.com> Date: Thu, 14 May 2020 17:35:09 -0500 Subject: [PATCH 07/12] JMAQS #309 - Removed generalized exceptions with more dedicated exceptions --- .../jmaqs/selenium/ElementHandler.java | 21 +++++++------------ .../jmaqs/selenium/WebDriverFactory.java | 12 ++++++----- .../exceptions/DriverNotFoundException.java | 11 ++++++++++ .../exceptions/ElementHandlerException.java | 17 +++++++++++++++ .../exceptions/WebDriverFactoryException.java | 11 ++++++++++ .../jmaqs/utilities/helper/Config.java | 3 ++- .../FrameworkConfigurationException.java | 11 ++++++++++ 7 files changed, 67 insertions(+), 19 deletions(-) create mode 100644 jmaqs-selenium/src/main/java/com/magenic/jmaqs/selenium/exceptions/DriverNotFoundException.java create mode 100644 jmaqs-selenium/src/main/java/com/magenic/jmaqs/selenium/exceptions/ElementHandlerException.java create mode 100644 jmaqs-selenium/src/main/java/com/magenic/jmaqs/selenium/exceptions/WebDriverFactoryException.java create mode 100644 jmaqs-utilities/src/main/java/com/magenic/jmaqs/utilities/helper/exceptions/FrameworkConfigurationException.java diff --git a/jmaqs-selenium/src/main/java/com/magenic/jmaqs/selenium/ElementHandler.java b/jmaqs-selenium/src/main/java/com/magenic/jmaqs/selenium/ElementHandler.java index 5762e2ea3..dfabea68f 100644 --- a/jmaqs-selenium/src/main/java/com/magenic/jmaqs/selenium/ElementHandler.java +++ b/jmaqs-selenium/src/main/java/com/magenic/jmaqs/selenium/ElementHandler.java @@ -4,6 +4,7 @@ package com.magenic.jmaqs.selenium; +import com.magenic.jmaqs.selenium.exceptions.ElementHandlerException; import com.magenic.jmaqs.selenium.factories.UIWaitFactory; import com.magenic.jmaqs.utilities.helper.ListProcessor; import com.magenic.jmaqs.utilities.logging.Logger; @@ -163,10 +164,8 @@ public static void clickButton(WebDriver webDriver, By by, boolean waitForButton * @param by By selector for the element * @param elementsTextToSelect ArrayList items as Strings to select from List box */ - public static void selectMultipleElementsFromListBox(WebDriver webDriver, By by, - List<String> elementsTextToSelect) { - Select selectItem = new Select( - UIWaitFactory.getWaitDriver(webDriver).waitForClickableElement(by)); + public static void selectMultipleElementsFromListBox(WebDriver webDriver, By by, List<String> elementsTextToSelect) { + Select selectItem = new Select(UIWaitFactory.getWaitDriver(webDriver).waitForClickableElement(by)); // Select all desired items in the Listbox for (String text : elementsTextToSelect) { @@ -181,10 +180,8 @@ public static void selectMultipleElementsFromListBox(WebDriver webDriver, By by, * @param by By selector for the element * @param values ArrayList items as Strings to select from List box */ - public static void selectMultipleElementsFromListBoxByValue(WebDriver webDriver, By by, - List<String> values) { - Select selectItem = new Select( - UIWaitFactory.getWaitDriver(webDriver).waitForClickableElement(by)); + public static void selectMultipleElementsFromListBoxByValue(WebDriver webDriver, By by, List<String> values) { + Select selectItem = new Select(UIWaitFactory.getWaitDriver(webDriver).waitForClickableElement(by)); // Select all desired items in the Listbox for (String value : values) { @@ -234,7 +231,7 @@ public static void setTextBox(WebDriver webDriver, By by, String textToEnter, bo element.clear(); element.sendKeys(textToEnter); } else { - throw new RuntimeException("String is either null or empty"); + throw new ElementHandlerException("String is either null or empty"); } } @@ -347,8 +344,7 @@ public static void executeScrolling(WebElement webElement, int x, int y) { */ public static void slowType(WebDriver webDriver, By by, String textToEnter) { for (char singleLetter : textToEnter.toCharArray()) { - UIWaitFactory.getWaitDriver(webDriver).waitForClickableElement(by) - .sendKeys(Character.toString(singleLetter)); + UIWaitFactory.getWaitDriver(webDriver).waitForClickableElement(by).sendKeys(Character.toString(singleLetter)); try { Thread.sleep(500); @@ -381,8 +377,7 @@ public static void sendSecretKeys(WebDriver webDriver, By by, String textToEnter String stackTrace = sw.toString(); logger.logMessage(MessageType.ERROR, - "Exception during sending secret keys: " + e.getMessage() + System.lineSeparator() - + stackTrace); + "Exception during sending secret keys: " + e.getMessage() + System.lineSeparator() + stackTrace); throw e; } } diff --git a/jmaqs-selenium/src/main/java/com/magenic/jmaqs/selenium/WebDriverFactory.java b/jmaqs-selenium/src/main/java/com/magenic/jmaqs/selenium/WebDriverFactory.java index 13376bed5..3f05e325b 100644 --- a/jmaqs-selenium/src/main/java/com/magenic/jmaqs/selenium/WebDriverFactory.java +++ b/jmaqs-selenium/src/main/java/com/magenic/jmaqs/selenium/WebDriverFactory.java @@ -7,6 +7,8 @@ import com.magenic.jmaqs.selenium.constants.BrowserType; import com.magenic.jmaqs.selenium.constants.RemoteBrowserType; import com.magenic.jmaqs.selenium.constants.WebDriverFile; +import com.magenic.jmaqs.selenium.exceptions.DriverNotFoundException; +import com.magenic.jmaqs.selenium.exceptions.WebDriverFactoryException; import com.magenic.jmaqs.utilities.helper.StringProcessor; import java.io.File; import java.net.URL; @@ -46,7 +48,7 @@ private WebDriverFactory() { * @return the default browser * @throws Exception the exception */ - public static WebDriver getDefaultBrowser() throws Exception { + public static WebDriver getDefaultBrowser() throws WebDriverFactoryException { return getBrowserWithDefaultConfiguration(SeleniumConfig.getBrowserType()); } @@ -55,9 +57,9 @@ public static WebDriver getDefaultBrowser() throws Exception { * * @param browser the browser * @return the browser with default configuration - * @throws Exception the exception + * @throws WebDriverFactoryException the exception */ - public static WebDriver getBrowserWithDefaultConfiguration(BrowserType browser) throws Exception { + public static WebDriver getBrowserWithDefaultConfiguration(BrowserType browser) throws WebDriverFactoryException { String size = SeleniumConfig.getBrowserSize(); try { @@ -83,7 +85,7 @@ public static WebDriver getBrowserWithDefaultConfiguration(BrowserType browser) throw e; } catch (Exception e) { // Log that something went wrong - throw new Exception("Your web driver may be out of date or unsupported.", e); + throw new WebDriverFactoryException("Your web driver may be out of date or unsupported.", e); } } @@ -497,7 +499,7 @@ public static String getDriverLocation(String driverFile, String defaultHintPath // We didn't find the web driver so throw an error if we need to know where it is if (mustExist) { - throw new RuntimeException( + throw new DriverNotFoundException( StringProcessor.safeFormatter("Unable to find driver for '%s'", driverFile)); } diff --git a/jmaqs-selenium/src/main/java/com/magenic/jmaqs/selenium/exceptions/DriverNotFoundException.java b/jmaqs-selenium/src/main/java/com/magenic/jmaqs/selenium/exceptions/DriverNotFoundException.java new file mode 100644 index 000000000..8caa0a119 --- /dev/null +++ b/jmaqs-selenium/src/main/java/com/magenic/jmaqs/selenium/exceptions/DriverNotFoundException.java @@ -0,0 +1,11 @@ +/* + * Copyright 2020 (C) Magenic, All rights Reserved + */ + +package com.magenic.jmaqs.selenium.exceptions; + +public class DriverNotFoundException extends RuntimeException { + public DriverNotFoundException(String message) { + super(message); + } +} diff --git a/jmaqs-selenium/src/main/java/com/magenic/jmaqs/selenium/exceptions/ElementHandlerException.java b/jmaqs-selenium/src/main/java/com/magenic/jmaqs/selenium/exceptions/ElementHandlerException.java new file mode 100644 index 000000000..6a9c7c753 --- /dev/null +++ b/jmaqs-selenium/src/main/java/com/magenic/jmaqs/selenium/exceptions/ElementHandlerException.java @@ -0,0 +1,17 @@ +/* + * Copyright 2020 (C) Magenic, All rights Reserved + */ + +package com.magenic.jmaqs.selenium.exceptions; + +/** + * The type Element handler exception. + */ +public class ElementHandlerException extends RuntimeException { + + private static final long serialVersionUID = 1; + + public ElementHandlerException(String message) { + super(message); + } +} diff --git a/jmaqs-selenium/src/main/java/com/magenic/jmaqs/selenium/exceptions/WebDriverFactoryException.java b/jmaqs-selenium/src/main/java/com/magenic/jmaqs/selenium/exceptions/WebDriverFactoryException.java new file mode 100644 index 000000000..78aba60bf --- /dev/null +++ b/jmaqs-selenium/src/main/java/com/magenic/jmaqs/selenium/exceptions/WebDriverFactoryException.java @@ -0,0 +1,11 @@ +/* + * Copyright 2020 (C) Magenic, All rights Reserved + */ + +package com.magenic.jmaqs.selenium.exceptions; + +public class WebDriverFactoryException extends Exception { + public WebDriverFactoryException(String message, Exception exception) { + super(message, exception); + } +} diff --git a/jmaqs-utilities/src/main/java/com/magenic/jmaqs/utilities/helper/Config.java b/jmaqs-utilities/src/main/java/com/magenic/jmaqs/utilities/helper/Config.java index b4109d376..6de740247 100644 --- a/jmaqs-utilities/src/main/java/com/magenic/jmaqs/utilities/helper/Config.java +++ b/jmaqs-utilities/src/main/java/com/magenic/jmaqs/utilities/helper/Config.java @@ -4,6 +4,7 @@ package com.magenic.jmaqs.utilities.helper; +import com.magenic.jmaqs.utilities.helper.exceptions.FrameworkConfigurationException; import java.io.File; import java.util.HashMap; import java.util.Iterator; @@ -68,7 +69,7 @@ private Config() { overrideConfig = new XMLConfiguration(); overrideConfig.setSynchronizer(new ReadWriteSynchronizer()); } catch (ConfigurationException exception) { - throw new RuntimeException(StringProcessor + throw new FrameworkConfigurationException(StringProcessor .safeFormatter("Exception creating the xml configuration object from the file : %s", exception)); } } diff --git a/jmaqs-utilities/src/main/java/com/magenic/jmaqs/utilities/helper/exceptions/FrameworkConfigurationException.java b/jmaqs-utilities/src/main/java/com/magenic/jmaqs/utilities/helper/exceptions/FrameworkConfigurationException.java new file mode 100644 index 000000000..478823a60 --- /dev/null +++ b/jmaqs-utilities/src/main/java/com/magenic/jmaqs/utilities/helper/exceptions/FrameworkConfigurationException.java @@ -0,0 +1,11 @@ +/* + * Copyright 2020 (C) Magenic, All rights Reserved + */ + +package com.magenic.jmaqs.utilities.helper.exceptions; + +public class FrameworkConfigurationException extends RuntimeException { + public FrameworkConfigurationException(String message) { + super(message); + } +} From 1d69f522c70d21ea0e1ded4ced48057db7f82329 Mon Sep 17 00:00:00 2001 From: Jason Edstrom <JasonE@magenic.com> Date: Thu, 14 May 2020 18:26:56 -0500 Subject: [PATCH 08/12] JMAQS #309 - Removed generalized exceptions with more dedicated exceptions --- .../com/magenic/jmaqs/appium/AppiumConfig.java | 17 +++++++---------- .../exceptions/AppiumConfigException.java | 11 +++++++++++ .../java/com/magenic/jmaqs/base/BaseTest.java | 17 ++++++----------- .../com/magenic/jmaqs/base/BaseTestObject.java | 4 +++- .../magenic/jmaqs/base/ManagerDictionary.java | 5 +++-- .../exceptions/DriverDisposalException.java | 11 +++++++++++ .../exceptions/ManagerDisposalException.java | 11 +++++++++++ .../jmaqs/selenium/BaseSeleniumTest.java | 5 +++-- .../magenic/jmaqs/utilities/helper/Config.java | 2 +- 9 files changed, 56 insertions(+), 27 deletions(-) create mode 100644 jmaqs-appium/src/main/java/com/magenic/jmaqs/appium/exceptions/AppiumConfigException.java create mode 100644 jmaqs-base/src/main/java/com/magenic/jmaqs/base/exceptions/DriverDisposalException.java create mode 100644 jmaqs-base/src/main/java/com/magenic/jmaqs/base/exceptions/ManagerDisposalException.java diff --git a/jmaqs-appium/src/main/java/com/magenic/jmaqs/appium/AppiumConfig.java b/jmaqs-appium/src/main/java/com/magenic/jmaqs/appium/AppiumConfig.java index 43665e0d7..28bafeae7 100644 --- a/jmaqs-appium/src/main/java/com/magenic/jmaqs/appium/AppiumConfig.java +++ b/jmaqs-appium/src/main/java/com/magenic/jmaqs/appium/AppiumConfig.java @@ -5,6 +5,7 @@ package com.magenic.jmaqs.appium; import com.magenic.jmaqs.appium.constants.PlatformType; +import com.magenic.jmaqs.appium.exceptions.AppiumConfigException; import com.magenic.jmaqs.utilities.helper.Config; import com.magenic.jmaqs.utilities.helper.ConfigSection; import com.magenic.jmaqs.utilities.helper.StringProcessor; @@ -66,8 +67,7 @@ public static String getDeviceName() { * @return the save page source on fail */ public static boolean getSavePageSourceOnFail() { - return Config.getValueForSection(APPIUM_SECTION, "SavePageSourceOnFail") - .equalsIgnoreCase("Yes"); + return Config.getValueForSection(APPIUM_SECTION, "SavePageSourceOnFail").equalsIgnoreCase("Yes"); } /** @@ -76,8 +76,7 @@ public static boolean getSavePageSourceOnFail() { * @return the soft assert screen shot */ public static boolean getSoftAssertScreenShot() { - return Config.getValueForSection(APPIUM_SECTION, "SoftAssertScreenShot") - .equalsIgnoreCase("Yes"); + return Config.getValueForSection(APPIUM_SECTION, "SoftAssertScreenShot").equalsIgnoreCase("Yes"); } /** @@ -99,7 +98,7 @@ public static URL getMobileHubUrl() { try { url = new URL(getMobileHubUrlString()); } catch (MalformedURLException e) { - e.printStackTrace(); + throw new AppiumConfigException(e); } return url; } @@ -115,8 +114,8 @@ public static Duration getCommandTimeout() { try { timeoutValue = Integer.parseInt(value); } catch (NumberFormatException ex) { - throw new NumberFormatException("MobileCommandTimeout in " + APPIUM_SECTION - + " should be a number, but the current value is: " + value); + throw new NumberFormatException( + "MobileCommandTimeout in " + APPIUM_SECTION + " should be a number, but the current value is: " + value); } return Duration.ofMillis((long) timeoutValue); @@ -128,11 +127,9 @@ public static Duration getCommandTimeout() { * @return the mobile timeout */ public static Duration getMobileTimeout() { - return Duration.ofMillis( - Integer.parseInt(Config.getValueForSection(APPIUM_SECTION, "MobileTimeout", "0"))); + return Duration.ofMillis(Integer.parseInt(Config.getValueForSection(APPIUM_SECTION, "MobileTimeout", "0"))); } - /** * Gets capabilities as strings. * diff --git a/jmaqs-appium/src/main/java/com/magenic/jmaqs/appium/exceptions/AppiumConfigException.java b/jmaqs-appium/src/main/java/com/magenic/jmaqs/appium/exceptions/AppiumConfigException.java new file mode 100644 index 000000000..ef91304d9 --- /dev/null +++ b/jmaqs-appium/src/main/java/com/magenic/jmaqs/appium/exceptions/AppiumConfigException.java @@ -0,0 +1,11 @@ +/* + * Copyright 2020 (C) Magenic, All rights Reserved + */ + +package com.magenic.jmaqs.appium.exceptions; + +public class AppiumConfigException extends RuntimeException { + public AppiumConfigException(Exception e) { + super(e); + } +} diff --git a/jmaqs-base/src/main/java/com/magenic/jmaqs/base/BaseTest.java b/jmaqs-base/src/main/java/com/magenic/jmaqs/base/BaseTest.java index 723040a91..1d3c5b9b2 100644 --- a/jmaqs-base/src/main/java/com/magenic/jmaqs/base/BaseTest.java +++ b/jmaqs-base/src/main/java/com/magenic/jmaqs/base/BaseTest.java @@ -30,7 +30,6 @@ import org.testng.annotations.AfterMethod; import org.testng.annotations.BeforeMethod; - /** * Base test class. */ @@ -154,8 +153,7 @@ public List<String> getLoggedExceptions() { * @param loggedExceptionList ArrayList of logged exceptions to use. */ public void setLoggedExceptions(List<String> loggedExceptionList) { - this.loggedExceptions - .put(this.fullyQualifiedTestClassName.get(), (ArrayList<String>) loggedExceptionList); + this.loggedExceptions.put(this.fullyQualifiedTestClassName.get(), (ArrayList<String>) loggedExceptionList); } /** @@ -238,8 +236,7 @@ public void teardown() { try { this.beforeLoggingTeardown(testResult); } catch (Exception e) { - this.tryToLog(MessageType.WARNING, "Failed before logging teardown because: %s", - e.getMessage()); + this.tryToLog(MessageType.WARNING, "Failed before logging teardown because: %s", e.getMessage()); } // Log the test result @@ -309,10 +306,9 @@ protected Logger createLogger() { this.setLoggedExceptions(new ArrayList<String>()); if (this.loggingEnabledSetting != LoggingEnabled.NO) { - log = LoggingConfig.getLogger(StringProcessor - .safeFormatter("%s - %s", this.fullyQualifiedTestClassName.get(), - DateTimeFormatter.ofPattern("uuuu-MM-dd-HH-mm-ss-SSSS", Locale.getDefault()) - .format(LocalDateTime.now(Clock.systemUTC())))); + log = LoggingConfig.getLogger(StringProcessor.safeFormatter("%s - %s", this.fullyQualifiedTestClassName.get(), + DateTimeFormatter.ofPattern("uuuu-MM-dd-HH-mm-ss-SSSS", Locale.getDefault()) + .format(LocalDateTime.now(Clock.systemUTC())))); } else { log = new ConsoleLogger(); } @@ -405,8 +401,7 @@ protected void logVerbose(String message, Object... args) { for (StackTraceElement element : Thread.currentThread().getStackTrace()) { // If the stack trace element is from the com.magenic package (excluding this method) append the stack trace line - if (element.toString().startsWith("com.magenic") && !element.toString() - .contains("BaseTest.logVerbose")) { + if (element.toString().startsWith("com.magenic") && !element.toString().contains("BaseTest.logVerbose")) { messages.append(element.toString() + System.lineSeparator()); } } diff --git a/jmaqs-base/src/main/java/com/magenic/jmaqs/base/BaseTestObject.java b/jmaqs-base/src/main/java/com/magenic/jmaqs/base/BaseTestObject.java index b6d24d083..237a6c1ca 100644 --- a/jmaqs-base/src/main/java/com/magenic/jmaqs/base/BaseTestObject.java +++ b/jmaqs-base/src/main/java/com/magenic/jmaqs/base/BaseTestObject.java @@ -4,6 +4,8 @@ package com.magenic.jmaqs.base; +import com.magenic.jmaqs.base.exceptions.DriverDisposalException; +import com.magenic.jmaqs.utilities.helper.StringProcessor; import com.magenic.jmaqs.utilities.logging.Logger; import com.magenic.jmaqs.utilities.logging.MessageType; import com.magenic.jmaqs.utilities.performance.PerfTimerCollection; @@ -315,7 +317,7 @@ public void close() { try { singleDriver.close(); } catch (final Exception e) { - e.printStackTrace(); + throw new DriverDisposalException(StringProcessor.safeFormatter("Unable to properly dispose of driver"), e); } } this.managerStore = null; diff --git a/jmaqs-base/src/main/java/com/magenic/jmaqs/base/ManagerDictionary.java b/jmaqs-base/src/main/java/com/magenic/jmaqs/base/ManagerDictionary.java index 58bd518b9..6c03929b0 100644 --- a/jmaqs-base/src/main/java/com/magenic/jmaqs/base/ManagerDictionary.java +++ b/jmaqs-base/src/main/java/com/magenic/jmaqs/base/ManagerDictionary.java @@ -4,6 +4,7 @@ package com.magenic.jmaqs.base; +import com.magenic.jmaqs.base.exceptions.ManagerDisposalException; import java.util.HashMap; import java.util.Map; @@ -23,7 +24,7 @@ public void clear() { try { entry.getValue().close(); } catch (Exception e) { - e.printStackTrace(); + throw new ManagerDisposalException(e); } } super.clear(); @@ -81,7 +82,7 @@ public boolean remove(String key) { try { this.get(key).close(); } catch (Exception e) { - e.printStackTrace(); + throw new ManagerDisposalException(e); } } diff --git a/jmaqs-base/src/main/java/com/magenic/jmaqs/base/exceptions/DriverDisposalException.java b/jmaqs-base/src/main/java/com/magenic/jmaqs/base/exceptions/DriverDisposalException.java new file mode 100644 index 000000000..e3693f67e --- /dev/null +++ b/jmaqs-base/src/main/java/com/magenic/jmaqs/base/exceptions/DriverDisposalException.java @@ -0,0 +1,11 @@ +/* + * Copyright 2020 (C) Magenic, All rights Reserved + */ + +package com.magenic.jmaqs.base.exceptions; + +public class DriverDisposalException extends RuntimeException { + public DriverDisposalException(String message, Exception exception) { + super(message, exception); + } +} diff --git a/jmaqs-base/src/main/java/com/magenic/jmaqs/base/exceptions/ManagerDisposalException.java b/jmaqs-base/src/main/java/com/magenic/jmaqs/base/exceptions/ManagerDisposalException.java new file mode 100644 index 000000000..e44bbecdb --- /dev/null +++ b/jmaqs-base/src/main/java/com/magenic/jmaqs/base/exceptions/ManagerDisposalException.java @@ -0,0 +1,11 @@ +/* + * Copyright 2020 (C) Magenic, All rights Reserved + */ + +package com.magenic.jmaqs.base.exceptions; + +public class ManagerDisposalException extends RuntimeException { + public ManagerDisposalException(Exception e) { + super(e); + } +} diff --git a/jmaqs-selenium/src/main/java/com/magenic/jmaqs/selenium/BaseSeleniumTest.java b/jmaqs-selenium/src/main/java/com/magenic/jmaqs/selenium/BaseSeleniumTest.java index 47a708b7e..34ac3c07e 100644 --- a/jmaqs-selenium/src/main/java/com/magenic/jmaqs/selenium/BaseSeleniumTest.java +++ b/jmaqs-selenium/src/main/java/com/magenic/jmaqs/selenium/BaseSeleniumTest.java @@ -5,6 +5,7 @@ package com.magenic.jmaqs.selenium; import com.magenic.jmaqs.base.BaseExtendableTest; +import com.magenic.jmaqs.selenium.exceptions.WebDriverFactoryException; import com.magenic.jmaqs.utilities.helper.StringProcessor; import com.magenic.jmaqs.utilities.logging.LoggingEnabled; import com.magenic.jmaqs.utilities.logging.MessageType; @@ -63,9 +64,9 @@ protected void beforeLoggingTeardown(ITestResult resultType) { * Get the current browser. * * @return Current browser Web Driver - * @throws Exception Throws exception + * @throws WebDriverFactoryException Throws exception */ - protected WebDriver getBrowser() throws Exception { + protected WebDriver getBrowser() throws WebDriverFactoryException { // Returns the web driver return WebDriverFactory.getDefaultBrowser(); } diff --git a/jmaqs-utilities/src/main/java/com/magenic/jmaqs/utilities/helper/Config.java b/jmaqs-utilities/src/main/java/com/magenic/jmaqs/utilities/helper/Config.java index 6de740247..595581a11 100644 --- a/jmaqs-utilities/src/main/java/com/magenic/jmaqs/utilities/helper/Config.java +++ b/jmaqs-utilities/src/main/java/com/magenic/jmaqs/utilities/helper/Config.java @@ -252,7 +252,7 @@ public static String getValue(String key, String defaultValue) { * @return True if the key exists, false otherwise */ public static boolean doesKeyExist(String key) { - return overrideConfig.containsKey(key) ? true : configValues.containsKey(key); + return overrideConfig.containsKey(key) || configValues.containsKey(key); } /** From 26f203652d5a4b8e3f3ab2396c613fa228720fb6 Mon Sep 17 00:00:00 2001 From: Jason Edstrom <JasonE@magenic.com> Date: Thu, 14 May 2020 22:31:59 -0500 Subject: [PATCH 09/12] JMAQS #309 - Periods in the javadocs... --- .../jmaqs/selenium/AbstractLazyElement.java | 82 +++++++++---------- .../jmaqs/selenium/BaseSeleniumPageModel.java | 12 +-- .../jmaqs/selenium/LazyWebElement.java | 12 +-- .../com/magenic/jmaqs/selenium/UIWait.java | 2 +- .../selenium/factories/FluentWaitFactory.java | 2 +- .../selenium/factories/UIWaitFactory.java | 10 +-- 6 files changed, 60 insertions(+), 60 deletions(-) diff --git a/jmaqs-selenium/src/main/java/com/magenic/jmaqs/selenium/AbstractLazyElement.java b/jmaqs-selenium/src/main/java/com/magenic/jmaqs/selenium/AbstractLazyElement.java index 608f44af3..17b40e922 100644 --- a/jmaqs-selenium/src/main/java/com/magenic/jmaqs/selenium/AbstractLazyElement.java +++ b/jmaqs-selenium/src/main/java/com/magenic/jmaqs/selenium/AbstractLazyElement.java @@ -26,42 +26,42 @@ import org.openqa.selenium.support.ui.FluentWait; /** - * Abstract structure for dynamically finding and interacting with elements + * Abstract structure for dynamically finding and interacting with elements. */ public abstract class AbstractLazyElement { /** - * The index in cases where the selector finds multiple elements + * The index in cases where the selector finds multiple elements. */ private final Integer elementIndex; /** - * A user friendly name, for logging purposes + * A user friendly name, for logging purposes. */ protected final String userFriendlyName; /** - * The parent lazy element + * The parent lazy element. */ protected LazyWebElement parent; /** - * The 'by' selector for the element + * The 'by' selector for the element. */ protected By by; /** - * The test object for the element + * The test object for the element. */ protected SeleniumTestObject testObject; /** - * Cached copy of the element or null if we haven't already found the element + * Cached copy of the element or null if we haven't already found the element. */ private WebElement cachedElement; /** - * Initializes a new instance of the {@link #AbstractLazyElement} class + * Initializes a new instance of the {@link #AbstractLazyElement} class. * * @param testObject The selenium test object * @param locator The by locator to search on @@ -75,7 +75,7 @@ protected AbstractLazyElement(SeleniumTestObject testObject, By locator, String } /** - * Initializes a new instance of the {@link #AbstractLazyElement} class + * Initializes a new instance of the {@link #AbstractLazyElement} class. * * @param parent The parent lazy element * @param locator The by locator to search on @@ -90,7 +90,7 @@ protected AbstractLazyElement(LazyWebElement parent, By locator, String userFrie } /** - * Initializes a new instance of the {@link #AbstractLazyElement} class + * Initializes a new instance of the {@link #AbstractLazyElement} class. * * @param parent The parent lazy element * @param locator THe by locator to search on @@ -109,7 +109,7 @@ protected AbstractLazyElement(LazyWebElement parent, By locator, String userFrie } /** - * Gets the by selector + * Gets the by selector. * * @return the by */ @@ -118,7 +118,7 @@ public By getBy() { } /** - * Gets the cached element + * Gets the cached element. * * @return the cachedElement */ @@ -127,7 +127,7 @@ public WebElement getCachedElement() { } /** - * Sets the cached Element + * Sets the cached Element. * * @param cachedElementFactory the cachedElement function to set * the cached element @@ -137,7 +137,7 @@ private void setCachedElement(Supplier<WebElement> cachedElementFactory) { } /** - * Gets the user friendly name + * Gets the user friendly name. * * @return the userFriendlyName */ @@ -146,7 +146,7 @@ public String getUserFriendlyName() { } /** - * Gets the parent of the lazy element + * Gets the parent of the lazy element. * * @return the parent */ @@ -155,7 +155,7 @@ public LazyWebElement getParent() { } /** - * Gets the test object + * Gets the test object. * * @return the test object */ @@ -164,7 +164,7 @@ public SeleniumTestObject getTestObject() { } /** - * Gets the tag name of the lazy element + * Gets the tag name of the lazy element. * * @return The tag name * @throws TimeoutException If a timeout occurred while waiting for the element to be found @@ -177,7 +177,7 @@ public String getTagName() throws TimeoutException, InterruptedException { } /** - * Gets the lazy element's text + * Gets the lazy element's text. * * @return The element text * @throws TimeoutException If a timeout occurred while waiting for the element to be found @@ -190,7 +190,7 @@ public String getText() throws TimeoutException, InterruptedException { } /** - * Gets the lazy element's location + * Gets the lazy element's location. * * @return the location as a Point * @throws TimeoutException If a timeout occurred while waiting for the element to be found @@ -203,7 +203,7 @@ public Point getLocation() throws TimeoutException, InterruptedException { } /** - * Gets the lazy element's size + * Gets the lazy element's size. * * @return The lazy element's size * @throws TimeoutException If a timeout occurred while waiting for the element to be found @@ -216,7 +216,7 @@ public Dimension getSize() throws TimeoutException, InterruptedException { } /** - * Click the lazy element + * Click the lazy element. * * @throws TimeoutException If a timeout occurred while waiting for the element to be found * @throws InterruptedException If the thread is interrupted while waiting for the element to be found @@ -230,7 +230,7 @@ public void click() throws TimeoutException, InterruptedException, ExecutionFail } /** - * Send Secret keys with no logging + * Send Secret keys with no logging. * * @param keys the secret keys * @throws ExecutionFailedException If error occurs while sending keys @@ -256,7 +256,7 @@ public void sendSecretKeys(String keys) throws ExecutionFailedException, Timeout } /** - * Clear the lazy element + * Clear the lazy element. * * @throws TimeoutException If a timeout occurred while waiting for the element to be found * @throws InterruptedException If the thread is interrupted while waiting for the element to be found @@ -270,7 +270,7 @@ public void clear() throws TimeoutException, InterruptedException, ExecutionFail } /** - * Submit the lazy element + * Submit the lazy element. * * @throws TimeoutException If a timeout occurred while waiting for the element to be found * @throws InterruptedException If the thread is interrupted while waiting for the element to be found @@ -284,7 +284,7 @@ public void submit() throws TimeoutException, InterruptedException, ExecutionFai } /** - * Gets the value for the given attribute + * Gets the value for the given attribute. * * @param attributeName The name of the attribute * @return The attribute @@ -298,7 +298,7 @@ public String getAttribute(String attributeName) throws TimeoutException, Interr } /** - * Gets the current value of an element - Useful for get input box text + * Gets the current value of an element - Useful for get input box text. * * @return The value attribute * @throws TimeoutException If a timeout occurred while waiting for the element to be found @@ -311,7 +311,7 @@ public String getValue() throws TimeoutException, InterruptedException { } /** - * Gets the CSS value for the given attribute + * Gets the CSS value for the given attribute. * * @param propertyName The property name * @return the css value for the property @@ -325,7 +325,7 @@ public String getCssValue(String propertyName) throws TimeoutException, Interrup } /** - * Wait for and get the visible web element + * Wait for and get the visible web element. * * @return The visible web element */ @@ -356,7 +356,7 @@ public WebElement getRawVisibleElement() { } /** - * Wait for and get the click-able web element + * Wait for and get the click-able web element. * * @return The click-able web element */ @@ -385,7 +385,7 @@ public WebElement getRawClickableElement() { } /** - * Waits for and gets the existing web element + * Waits for and gets the existing web element. * * @return The existing web element */ @@ -440,7 +440,7 @@ public List<WebElement> findRawElements(By by) throws TimeoutException, Interrup } /** - * Sends the keys to the element + * Sends the keys to the element. * * @param keysToSend The keys being sent to the element * @throws TimeoutException If a timeout occurred while waiting for the element to be found @@ -464,7 +464,7 @@ public void sendKeys(CharSequence... keysToSend) } /** - * If the Element is selected + * If the Element is selected. * * @return If the element is selected * @throws TimeoutException If a timeout occurred while waiting for the element to be found @@ -477,7 +477,7 @@ public boolean isSelected() throws TimeoutException, InterruptedException { } /** - * If the element is enabled + * If the element is enabled. * * @return If the element is enabled * @throws TimeoutException If a timeout occurred while waiting for the element to be found @@ -490,7 +490,7 @@ public boolean isEnabled() throws TimeoutException, InterruptedException { } /** - * If the element is displayed + * If the element is displayed. * * @return If the element is displayed * @throws TimeoutException If a timeout occurred while waiting for the element to be found @@ -503,7 +503,7 @@ public boolean isDisplayed() throws TimeoutException, InterruptedException { } /** - * Gets the rectangle value that highlights the lazy element location + * Gets the rectangle value that highlights the lazy element location. * * @return The location and size of the element * @throws TimeoutException If a timeout occurred while waiting for the element to be found @@ -514,7 +514,7 @@ public Rectangle getRect() throws TimeoutException, InterruptedException { } /** - * Gets the screenshot as the target type + * Gets the screenshot as the target type. * * @param target The target output type * @return The type to get the screenshot as @@ -526,7 +526,7 @@ public <X> X getScreenshotAs(OutputType<X> target) throws TimeoutException, Inte } /** - * Returns a string that represents the current object + * Returns a string that represents the current object. * * @return the lazy element string */ @@ -545,7 +545,7 @@ public String toString() { /** * Gets a web element using the provided factory if the cached element is - * null or if the cached element is stale + * null or if the cached element is stale. * * @param getElement The function that gets the element * @return The web element @@ -579,7 +579,7 @@ protected WebElement getElement(Supplier<WebElement> getElement) { } /** - * Execute an element action + * Execute an element action. * * @param elementAction the element action * @param caller Text to identify the caller function @@ -601,7 +601,7 @@ private void executeEvent(Action elementAction, String caller) throws ExecutionF } /** - * Gets the element at the indexed value + * Gets the element at the indexed value. * * @param matchesState The predicate to see that the element found at the index matches the state * we expect @@ -624,7 +624,7 @@ private WebElement getRawIndexed(Predicate<WebElement> matchesState, String expe } /** - * Checks if the element exists + * Checks if the element exists. * * @return If the element exists in the current page state * @throws TimeoutException If a timeout occurred while waiting for the element to be found diff --git a/jmaqs-selenium/src/main/java/com/magenic/jmaqs/selenium/BaseSeleniumPageModel.java b/jmaqs-selenium/src/main/java/com/magenic/jmaqs/selenium/BaseSeleniumPageModel.java index 15e5292b6..262906593 100644 --- a/jmaqs-selenium/src/main/java/com/magenic/jmaqs/selenium/BaseSeleniumPageModel.java +++ b/jmaqs-selenium/src/main/java/com/magenic/jmaqs/selenium/BaseSeleniumPageModel.java @@ -17,17 +17,17 @@ public abstract class BaseSeleniumPageModel { /** - * The lazy Element store + * The lazy Element store. */ private final HashMap<String, LazyWebElement> lazyElementStore; /** - * The selenium test object + * The selenium test object. */ private final SeleniumTestObject testObject; /** - * The web driver + * The web driver. */ private WebDriver webDriver; @@ -52,7 +52,7 @@ protected Logger getLogger() { } /** - * Gets the lazy element store + * Gets the lazy element store. * * @return The lazy element store */ @@ -88,7 +88,7 @@ public void setWebDriver(WebDriver webDriver) { } /** - * Gets the per timer collection + * Gets the per timer collection. * * @return The perf timer colection */ @@ -104,7 +104,7 @@ public PerfTimerCollection getPerfTimerCollection() { public abstract boolean isPageLoaded(); /** - * Waits for the page to load + * Waits for the page to load. */ public void waitForPageLoad() { UIWaitFactory.getWaitDriver(getWebDriver()).waitForPageLoad(); diff --git a/jmaqs-selenium/src/main/java/com/magenic/jmaqs/selenium/LazyWebElement.java b/jmaqs-selenium/src/main/java/com/magenic/jmaqs/selenium/LazyWebElement.java index ada33adaa..0c9770cc2 100644 --- a/jmaqs-selenium/src/main/java/com/magenic/jmaqs/selenium/LazyWebElement.java +++ b/jmaqs-selenium/src/main/java/com/magenic/jmaqs/selenium/LazyWebElement.java @@ -11,12 +11,12 @@ import org.openqa.selenium.WebElement; /** - * Driver for dynamically finding and interacting with elements + * Driver for dynamically finding and interacting with elements. */ public class LazyWebElement extends AbstractLazyElement { /** - * Initializes a new instance of the {@link #LazyWebElement} class + * Initializes a new instance of the {@link #LazyWebElement} class. * * @param testObject The selenium test object * @param locator The by locator to search on @@ -27,7 +27,7 @@ public LazyWebElement(SeleniumTestObject testObject, By locator, String userFrie } /** - * Initializes a new instance of the {@link #LazyWebElement} class + * Initializes a new instance of the {@link #LazyWebElement} class. * * @param parent The parent lazy element * @param locator The by locator to search on @@ -38,7 +38,7 @@ public LazyWebElement(LazyWebElement parent, By locator, String userFriendlyName } /** - * Initializes a new instance of the {@link #LazyWebElement} class + * Initializes a new instance of the {@link #LazyWebElement} class. * * @param parent The parent lazy element * @param locator THe by locator to search on @@ -52,7 +52,7 @@ public LazyWebElement(LazyWebElement parent, By locator, String userFriendlyName } /** - * Find the WebElement using the by and then maps it to the LazyWebElement + * Find the WebElement using the by and then maps it to the LazyWebElement. * * @param locator The by locator * @return The LazyWebElement of the element found @@ -65,7 +65,7 @@ public LazyWebElement findElement(By locator) throws TimeoutException, Interrupt } /** - * Find the collection of WebElements and then map them to LazyWebElements + * Find the collection of WebElements and then map them to LazyWebElements. * * @param locator The by locator * @return The collection of WebElements mapped to LazyWebElements diff --git a/jmaqs-selenium/src/main/java/com/magenic/jmaqs/selenium/UIWait.java b/jmaqs-selenium/src/main/java/com/magenic/jmaqs/selenium/UIWait.java index ecd37d686..cd9405605 100644 --- a/jmaqs-selenium/src/main/java/com/magenic/jmaqs/selenium/UIWait.java +++ b/jmaqs-selenium/src/main/java/com/magenic/jmaqs/selenium/UIWait.java @@ -1026,7 +1026,7 @@ private static boolean attributeMatches(WebDriver driver, By by, String attribut } /** - * Checks if the text of the elements are equal + * Checks if the text of the elements are equal. * * @param by Selector to examine * @param text Text that is being compared to the selector diff --git a/jmaqs-selenium/src/main/java/com/magenic/jmaqs/selenium/factories/FluentWaitFactory.java b/jmaqs-selenium/src/main/java/com/magenic/jmaqs/selenium/factories/FluentWaitFactory.java index a790b2889..e7b61b2af 100644 --- a/jmaqs-selenium/src/main/java/com/magenic/jmaqs/selenium/factories/FluentWaitFactory.java +++ b/jmaqs-selenium/src/main/java/com/magenic/jmaqs/selenium/factories/FluentWaitFactory.java @@ -11,7 +11,7 @@ import org.openqa.selenium.support.ui.FluentWait; /** - * Handles the creation of {@link org.openqa.selenium.support.ui.FluentWait FluentWait} objects + * Handles the creation of {@link org.openqa.selenium.support.ui.FluentWait FluentWait} objects. */ public class FluentWaitFactory { diff --git a/jmaqs-selenium/src/main/java/com/magenic/jmaqs/selenium/factories/UIWaitFactory.java b/jmaqs-selenium/src/main/java/com/magenic/jmaqs/selenium/factories/UIWaitFactory.java index 3153b84bf..0b9f1085c 100644 --- a/jmaqs-selenium/src/main/java/com/magenic/jmaqs/selenium/factories/UIWaitFactory.java +++ b/jmaqs-selenium/src/main/java/com/magenic/jmaqs/selenium/factories/UIWaitFactory.java @@ -20,12 +20,12 @@ public class UIWaitFactory { /** - * the collection of wait objects + * the collection of wait objects. */ private static ConcurrentHashMap<WebDriver, WebDriverWait> waitCollection = new ConcurrentHashMap<>(); /** - * private constructor so class can't be instantiated + * private constructor so class can't be instantiated. */ private UIWaitFactory() { } @@ -59,7 +59,7 @@ public static UIWait getWaitDriver(SearchContext searchContext, final int timeOu } /** - * Gets the WebDriverWait for building the UIWait object + * Gets the WebDriverWait for building the UIWait object. * * @param searchContext The search context * @return The WebDriverWait @@ -77,7 +77,7 @@ private static WebDriverWait getWebDriverWait(SearchContext searchContext) { } /** - * Adds the waitDriver to the collection + * Adds the waitDriver to the collection. * * @param driver The web driver * @param waitDriver the Wait object @@ -89,7 +89,7 @@ public static void setWaitDriver(SearchContext driver, WebDriverWait waitDriver) } /** - * Removes the waitDriver from the collection + * Removes the waitDriver from the collection. * * @param driver the web driver */ From 14631023d3071140fa37dcab84685cd045fcd878 Mon Sep 17 00:00:00 2001 From: Jason Edstrom <JasonE@magenic.com> Date: Thu, 14 May 2020 23:35:04 -0500 Subject: [PATCH 10/12] JMAQS #309 - checkstyle for pull request --- .github/workflows/checkstyle.yml | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 .github/workflows/checkstyle.yml diff --git a/.github/workflows/checkstyle.yml b/.github/workflows/checkstyle.yml new file mode 100644 index 000000000..71b76c52b --- /dev/null +++ b/.github/workflows/checkstyle.yml @@ -0,0 +1,20 @@ +# This workflow will build a Java project with Maven +# For more information see: https://help.github.com/actions/language-and-framework-guides/building-and-testing-java-with-maven + +name: Checkstyle Validation + +on: + pull_request: + branches: [ master ] + +jobs: + check: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - name: Set up JDK 11 + uses: actions/setup-java@v1 + with: + java-version: 11 + - name: Run Checkstyle + run: mvn -B checkstyle:check --file pom.xml -e \ No newline at end of file From 3333a0cbe2bd9b5230fb037e73b5aab95975fc5e Mon Sep 17 00:00:00 2001 From: Jason Edstrom <jasone@magenic.com> Date: Fri, 15 May 2020 00:02:12 -0500 Subject: [PATCH 11/12] Update checkstyle.yml changed goal and add continue on error argument --- .github/workflows/checkstyle.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/checkstyle.yml b/.github/workflows/checkstyle.yml index 71b76c52b..a886ce06d 100644 --- a/.github/workflows/checkstyle.yml +++ b/.github/workflows/checkstyle.yml @@ -17,4 +17,4 @@ jobs: with: java-version: 11 - name: Run Checkstyle - run: mvn -B checkstyle:check --file pom.xml -e \ No newline at end of file + run: mvn -B validate --file pom.xml -e -fae From d78890ad86509bbd11aaf09a584619d76b74325e Mon Sep 17 00:00:00 2001 From: Jason Edstrom <JasonE@magenic.com> Date: Fri, 15 May 2020 00:25:58 -0500 Subject: [PATCH 12/12] JMAQS #309 - removed fixme tag --- .../jmaqs/selenium/EventHandlerUnitTest.java | 36 +++++-------------- 1 file changed, 8 insertions(+), 28 deletions(-) diff --git a/jmaqs-selenium/src/test/java/com/magenic/jmaqs/selenium/EventHandlerUnitTest.java b/jmaqs-selenium/src/test/java/com/magenic/jmaqs/selenium/EventHandlerUnitTest.java index dfde58e81..c80ef74cb 100644 --- a/jmaqs-selenium/src/test/java/com/magenic/jmaqs/selenium/EventHandlerUnitTest.java +++ b/jmaqs-selenium/src/test/java/com/magenic/jmaqs/selenium/EventHandlerUnitTest.java @@ -20,14 +20,6 @@ import org.testng.annotations.Test; import org.testng.asserts.SoftAssert; -/* -FIXME: Commenting out tests until repaired. All failing for same general casting issue. -FIXME: java.lang.ClassCastException: com.magenic.jmaqs.utilities.logging.ConsoleLogger - cannot be cast to com.magenic.jmaqs.utilities.logging.FileLogger at - com.magenic.jmaqs.selenium.EventHandlerUnitTest.eventHandlerSwitchWindow - (EventHandlerUnitTest.java:253) -*/ - /** * Unit tests for EventHandler class. */ @@ -45,18 +37,6 @@ public class EventHandlerUnitTest extends BaseSeleniumTest { private static String siteAutomationUrl = siteUrl + "Automation/"; - /** - * Event Handler. - */ - - // private EventHandler eventHandler; - - /** - * Event Firing Web Driver. - */ - - // private EventFiringWebDriver eventFiringWebDriver; - /** * Home button. */ @@ -158,8 +138,8 @@ public void eventHandlerFindBy() { SoftAssert softAssert = new SoftAssert(); softAssert.assertTrue(logText.contains("Before finding element By"), "Expected message to be logged before clicking element."); - softAssert.assertTrue(logText.contains("Found element By"), - "Expected message to be logged after clicking element."); + softAssert + .assertTrue(logText.contains("Found element By"), "Expected message to be logged after clicking element."); softAssert.assertAll(); } @@ -305,8 +285,8 @@ public void eventHandlerSwitchWindow() { SoftAssert softAssert = new SoftAssert(); softAssert.assertTrue(logText.contains("Before switching to window"), "Expected message to be logged before switching windows."); - softAssert.assertTrue(logText.contains("Switched to window"), - "Expected message to be logged after switching windows."); + softAssert + .assertTrue(logText.contains("Switched to window"), "Expected message to be logged after switching windows."); softAssert.assertAll(); } @@ -331,8 +311,8 @@ public void eventHandlerAcceptAlert() { SoftAssert softAssert = new SoftAssert(); softAssert.assertTrue(logText.contains("Before accepting the alert"), "Expected message to be logged before accepting an alert."); - softAssert.assertTrue(logText.contains("Alert accepted"), - "Expected message to be logged after accepting an alert."); + softAssert + .assertTrue(logText.contains("Alert accepted"), "Expected message to be logged after accepting an alert."); softAssert.assertAll(); } @@ -357,8 +337,8 @@ public void eventHandlerAcceptDismiss() { SoftAssert softAssert = new SoftAssert(); softAssert.assertTrue(logText.contains("Before dismissing the alert"), "Expected message to be logged before dismissing an alert."); - softAssert.assertTrue(logText.contains("Alert dismissed"), - "Expected message to be logged after dismissing an alert."); + softAssert + .assertTrue(logText.contains("Alert dismissed"), "Expected message to be logged after dismissing an alert."); softAssert.assertAll(); }