com.vaadin.testbench.

Class AbstractBrowserTestBase

java.lang.Object
com.vaadin.testbench.AbstractBrowserTestBase

All Implemented Interfaces:

HasDriver, HasElementQuery, HasSearchContext, HasTestBenchCommandExecutor

Direct Known Subclasses:

AbstractBrowserDriverTestBase

public abstract class AbstractBrowserTestBase extends Object implements HasDriver, HasTestBenchCommandExecutor, HasElementQuery

A superclass with helper methods to aid TestBench developers create a JUnit 5 based tests.

  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    protected static void
    assertEquals(org.openqa.selenium.WebElement expectedElement, org.openqa.selenium.WebElement actualElement)

    Assert that the two elements are equal.

    void

    Calls the blur() function on the current active element of the page, if any.

    protected void

    Checks browser's log entries, throws an error for any client-side error and logs any client-side warnings.

    protected void
    checkLogsForErrors(Predicate<String> acceptableMessagePredicate)

    Checks browser's log entries, throws an error for any client-side error and logs any client-side warnings.

    protected void

    Clicks on the element, using JS.

    protected void
    clickElementWithJs(org.openqa.selenium.WebElement element)

    Clicks on the element, using JS.

    static String
    concatUrl(String baseUrl, String uri)

    Combines a base URL with an URI to create a final URL.

    void
    drag(org.openqa.selenium.WebElement source)

    Simulate only a drag of source.

    void
    dragAndDrop(org.openqa.selenium.WebElement source, org.openqa.selenium.WebElement target)

    Simulate DnD of source element into the target element.

    void
    dragElementOver(org.openqa.selenium.WebElement source, org.openqa.selenium.WebElement target)

    Simulate a drag of source element and over the target element.

    executeScript(String script, Object... args)

    Executes the given JavaScript in the context of the currently selected frame or window.

    org.openqa.selenium.WebElement
    findElement(org.openqa.selenium.By by)
     
    List<org.openqa.selenium.WebElement>
    findElements(org.openqa.selenium.By by)
     

    Return a reference to the related TestBenchCommandExecutor instance.

    org.openqa.selenium.SearchContext

    Get a reference or a new instance of the SearchContext applicable to this class

    abstract org.openqa.selenium.WebDriver

    Returns the WebDriver instance or (if the previously provided WebDriver instance was not already a TestBenchDriverProxy instance) a TestBenchDriverProxy that wraps that driver.

    protected List<org.openqa.selenium.logging.LogEntry>

    Gets the log entries from the browser that have the given logging level or higher.

    protected int

    Gets current scroll position on x axis.

    protected int

    Gets current scroll position on y axis.

    protected boolean
    hasCssClass(org.openqa.selenium.WebElement element, String className)

    Checks if the given element has the given class name.

    boolean
    isElementPresent(org.openqa.selenium.By by)

    Returns true if an element can be found from the driver with given selector.

    protected void
    scrollBy(int deltaX, int deltaY)

    Scrolls the page by given amount of x and y deltas.

    protected void
    scrollIntoViewAndClick(org.openqa.selenium.WebElement element)

    Scrolls the page to the element specified and clicks it.

    protected void
    scrollToElement(org.openqa.selenium.WebElement element)

    Scrolls the page to the element given using javascript.

    Convenience method the return TestBenchCommands for the default WebDriver instance.

    protected void

    If dev server start in progress wait until it's started.

    protected void
    waitForElementNotPresent(org.openqa.selenium.By by)
     
    protected void
    waitForElementPresent(org.openqa.selenium.By by)
     
    protected void
    waitForElementVisible(org.openqa.selenium.By by)
     
    <T> T
    waitUntil(org.openqa.selenium.support.ui.ExpectedCondition<T> condition)

    Waits up to 10 seconds for the given condition to become neither null nor false.

    <T> T
    waitUntil(org.openqa.selenium.support.ui.ExpectedCondition<T> condition, long timeoutInSeconds)

    Waits the given number of seconds for the given condition to become neither null nor false.

    protected <T> void
    waitUntilNot(org.openqa.selenium.support.ui.ExpectedCondition<T> condition)

    Waits up to 10s for the given condition to become false.

    protected <T> void
    waitUntilNot(org.openqa.selenium.support.ui.ExpectedCondition<T> condition, long timeoutInSeconds)

    Waits the given number of seconds for the given condition to become false.

    <T extends TestBenchElement>
    T
    wrap(Class<T> elementType, org.openqa.selenium.WebElement element)

    Decorates the element with the specified Element type, making it possible to use component-specific API on elements found using standard Selenium API.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

    Methods inherited from interface com.vaadin.testbench.HasElementQuery

    $, $
  • Constructor Details

    • AbstractBrowserTestBase

      public AbstractBrowserTestBase()
  • Method Details

    • getDriver

      public abstract org.openqa.selenium.WebDriver getDriver()

      Returns the WebDriver instance or (if the previously provided WebDriver instance was not already a TestBenchDriverProxy instance) a TestBenchDriverProxy that wraps that driver.

      Specified by:

      getDriver in interface HasDriver

      Returns:

      the active WebDriver instance

    • findElement

      public org.openqa.selenium.WebElement findElement(org.openqa.selenium.By by)
    • findElements

      public List<org.openqa.selenium.WebElement> findElements(org.openqa.selenium.By by)
    • wrap

      public <T extends TestBenchElement> T wrap(Class<T> elementType, org.openqa.selenium.WebElement element)

      Decorates the element with the specified Element type, making it possible to use component-specific API on elements found using standard Selenium API.

      Type Parameters:

      T - the type of the TestBenchElement to return

      Parameters:

      elementType - The type (class) containing the API to decorate with

      element - The element instance to decorate

      Returns:

      The element wrapped in an instance of the specified element type.

    • executeScript

      public Object executeScript(String script, Object... args)

      Executes the given JavaScript in the context of the currently selected frame or window. The script fragment provided will be executed as the body of an anonymous function.

      This method wraps any returned WebElement as TestBenchElement.

      Parameters:

      script - the script to execute

      args - the arguments, available in the script as arguments[0]...arguments[N]

      Returns:

      whatever JavascriptExecutor.executeScript(String, Object...) returns

      Throws:

      UnsupportedOperationException - if the underlying driver does not support JavaScript execution

      See Also:

      • JavascriptExecutor.executeScript(String, Object...)

    • waitUntil

      public <T> T waitUntil(org.openqa.selenium.support.ui.ExpectedCondition<T> condition, long timeoutInSeconds)

      Waits the given number of seconds for the given condition to become neither null nor false. NotFoundExceptions are ignored by default.

      Use e.g. as waitUntil(ExpectedConditions.presenceOfElementLocated(by), 10);

      Type Parameters:

      T - The return type of the ExpectedCondition and this method

      Parameters:

      condition - Models a condition that might reasonably be expected to eventually evaluate to something that is neither null nor false.

      timeoutInSeconds - The timeout in seconds for the wait.

      Returns:

      The condition's return value if it returned something different from null or false before the timeout expired.

      Throws:

      org.openqa.selenium.TimeoutException - If the timeout expires.

      See Also:

      • FluentWait.until(java.util.function.Function<? super T, V>)
      • ExpectedCondition

    • waitUntil

      public <T> T waitUntil(org.openqa.selenium.support.ui.ExpectedCondition<T> condition)

      Waits up to 10 seconds for the given condition to become neither null nor false. NotFoundExceptions are ignored by default.

      Use e.g. as waitUntil(ExpectedConditions.presenceOfElementLocated(by));

      Type Parameters:

      T - The return type of the ExpectedCondition and this method

      Parameters:

      condition - Models a condition that might reasonably be expected to eventually evaluate to something that is neither null nor false.

      Returns:

      The condition's return value if it returned something different from null or false before the timeout expired.

      Throws:

      org.openqa.selenium.TimeoutException - If 10 seconds passed.

      See Also:

      • FluentWait.until(java.util.function.Function<? super T, V>)
      • ExpectedCondition

    • getContext

      public org.openqa.selenium.SearchContext getContext()

      Description copied from interface: HasSearchContext

      Get a reference or a new instance of the SearchContext applicable to this class

      Specified by:

      getContext in interface HasSearchContext

      Returns:

      a SearchContext instance

    • getCommandExecutor

      public TestBenchCommandExecutor getCommandExecutor()

      Description copied from interface: HasTestBenchCommandExecutor

      Return a reference to the related TestBenchCommandExecutor instance.

      Specified by:

      getCommandExecutor in interface HasTestBenchCommandExecutor

      Returns:

      the TestBenchCommandExecutor instance

    • testBench

      public TestBenchCommands testBench()

      Convenience method the return TestBenchCommands for the default WebDriver instance.

      Returns:

      The driver cast to a TestBenchCommands instance.

    • concatUrl

      public static String concatUrl(String baseUrl, String uri)

      Combines a base URL with an URI to create a final URL. This removes possible double slashes if the base URL ends with a slash and the URI begins with a slash.

      Parameters:

      baseUrl - the base URL

      uri - the URI

      Returns:

      the URL resulting from the combination of base URL and URI

    • waitUntilNot

      protected <T> void waitUntilNot(org.openqa.selenium.support.ui.ExpectedCondition<T> condition)

      Waits up to 10s for the given condition to become false. Use e.g. as waitUntilNot(ExpectedCondition).

      Type Parameters:

      T - the return type of the expected condition

      Parameters:

      condition - the condition to wait for to become false

    • isElementPresent

      public boolean isElementPresent(org.openqa.selenium.By by)

      Returns true if an element can be found from the driver with given selector.

      Parameters:

      by - the selector used to find element

      Returns:

      true if the element can be found

    • dragAndDrop

      public void dragAndDrop(org.openqa.selenium.WebElement source, org.openqa.selenium.WebElement target)

      Simulate DnD of source element into the target element.

      Parameters:

      source -

      target -

    • drag

      public void drag(org.openqa.selenium.WebElement source)

      Simulate only a drag of source.

      Parameters:

      source -

    • dragElementOver

      public void dragElementOver(org.openqa.selenium.WebElement source, org.openqa.selenium.WebElement target)

      Simulate a drag of source element and over the target element.

      Parameters:

      source -

      target -

    • waitUntilNot

      protected <T> void waitUntilNot(org.openqa.selenium.support.ui.ExpectedCondition<T> condition, long timeoutInSeconds)

      Waits the given number of seconds for the given condition to become false. Use e.g. as waitUntilNot(ExpectedCondition).

      Type Parameters:

      T - the return type of the expected condition

      Parameters:

      condition - the condition to wait for to become false

      timeoutInSeconds - the number of seconds to wait

    • waitForElementPresent

      protected void waitForElementPresent(org.openqa.selenium.By by)
    • waitForElementNotPresent

      protected void waitForElementNotPresent(org.openqa.selenium.By by)
    • waitForElementVisible

      protected void waitForElementVisible(org.openqa.selenium.By by)
    • hasCssClass

      protected boolean hasCssClass(org.openqa.selenium.WebElement element, String className)

      Checks if the given element has the given class name. Matches only full class names, i.e. has ("foo") does not match class="foobar"

      Parameters:

      element - the element to test

      className - the class names to match

      Returns:

      true if matches, false if not

    • assertEquals

      protected static void assertEquals(org.openqa.selenium.WebElement expectedElement, org.openqa.selenium.WebElement actualElement)

      Assert that the two elements are equal.

      Can be removed if https://dev.vaadin.com/ticket/18484 is fixed.

      Parameters:

      expectedElement - the expected element

      actualElement - the actual element

    • scrollBy

      protected void scrollBy(int deltaX, int deltaY)

      Scrolls the page by given amount of x and y deltas. Actual scroll values can be different if any delta is bigger then the corresponding document dimension.

      Parameters:

      deltaX - the offset in pixels to scroll horizontally

      deltaY - the offset in pixels to scroll vertically

    • scrollToElement

      protected void scrollToElement(org.openqa.selenium.WebElement element)

      Scrolls the page to the element given using javascript. Standard Selenium api does not work for current newest Chrome and ChromeDriver.

      Parameters:

      element - the element to scroll to, not null

    • scrollIntoViewAndClick

      protected void scrollIntoViewAndClick(org.openqa.selenium.WebElement element)

      Scrolls the page to the element specified and clicks it.

      Parameters:

      element - the element to scroll to and click

    • getScrollX

      protected int getScrollX()

      Gets current scroll position on x axis.

      Returns:

      current scroll position on x axis.

    • getScrollY

      protected int getScrollY()

      Gets current scroll position on y axis.

      Returns:

      current scroll position on y axis.

    • clickElementWithJs

      protected void clickElementWithJs(String elementId)

      Clicks on the element, using JS. This method is more convenient then Selenium findElement(By.id(urlId)).click(), because Selenium method changes scroll position, which is not always needed.

      Parameters:

      elementId - id of the

    • clickElementWithJs

      protected void clickElementWithJs(org.openqa.selenium.WebElement element)

      Clicks on the element, using JS. This method is more convenient then Selenium element.click(), because Selenium method changes scroll position, which is not always needed.

      Parameters:

      element - the element to be clicked on

    • getLogEntries

      protected List<org.openqa.selenium.logging.LogEntry> getLogEntries(Level level)

      Gets the log entries from the browser that have the given logging level or higher.

      Parameters:

      level - the minimum severity of logs included

      Returns:

      log entries from the browser

    • checkLogsForErrors

      protected void checkLogsForErrors(Predicate<String> acceptableMessagePredicate)

      Checks browser's log entries, throws an error for any client-side error and logs any client-side warnings.

      Parameters:

      acceptableMessagePredicate - allows to ignore log entries whose message is accaptable

      Throws:

      AssertionError - if an error is found in the browser logs

    • checkLogsForErrors

      protected void checkLogsForErrors()

      Checks browser's log entries, throws an error for any client-side error and logs any client-side warnings.

      Throws:

      AssertionError - if an error is found in the browser logs

    • waitForDevServer

      protected void waitForDevServer()

      If dev server start in progress wait until it's started. Otherwise return immidiately.

    • blur

      public void blur()

      Calls the blur() function on the current active element of the page, if any.