Package 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 -
Method Summary
Modifier and TypeMethodDescriptionprotected static void
assertEquals
(org.openqa.selenium.WebElement expectedElement, org.openqa.selenium.WebElement actualElement) Assert that the two elements are equal.void
blur()
Calls theblur()
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
clickElementWithJs
(String elementId) Clicks on the element, using JS.protected void
clickElementWithJs
(org.openqa.selenium.WebElement element) Clicks on the element, using JS.static String
Combines a base URL with an URI to create a final URL.void
drag
(org.openqa.selenium.WebElement source) Simulate only a drag ofsource
.void
dragAndDrop
(org.openqa.selenium.WebElement source, org.openqa.selenium.WebElement target) Simulate DnD ofsource
element into thetarget
element.void
dragElementOver
(org.openqa.selenium.WebElement source, org.openqa.selenium.WebElement target) Simulate a drag ofsource
element and over thetarget
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 relatedTestBenchCommandExecutor
instance.org.openqa.selenium.SearchContext
Get a reference or a new instance of the SearchContext applicable to this classabstract org.openqa.selenium.WebDriver
Returns theWebDriver
instance or (if the previously provided WebDriver instance was not already aTestBenchDriverProxy
instance) aTestBenchDriverProxy
that wraps that driver.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.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 returnTestBenchCommands
for the defaultWebDriver
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>
TDecorates 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 theWebDriver
instance or (if the previously provided WebDriver instance was not already aTestBenchDriverProxy
instance) aTestBenchDriverProxy
that wraps that driver. -
findElement
public org.openqa.selenium.WebElement findElement(org.openqa.selenium.By by) -
findElements
-
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 theTestBenchElement
to return- Parameters:
elementType
- The type (class) containing the API to decorate withelement
- The element instance to decorate- Returns:
- The element wrapped in an instance of the specified element type.
-
executeScript
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
asTestBenchElement
.- Parameters:
script
- the script to executeargs
- the arguments, available in the script asarguments[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.NotFoundException
s are ignored by default.Use e.g. as
waitUntil(ExpectedConditions.presenceOfElementLocated(by), 10);
- Type Parameters:
T
- The return type of theExpectedCondition
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.NotFoundException
s are ignored by default.Use e.g. as
waitUntil(ExpectedConditions.presenceOfElementLocated(by));
- Type Parameters:
T
- The return type of theExpectedCondition
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 interfaceHasSearchContext
- Returns:
- a
SearchContext
instance
-
getCommandExecutor
Description copied from interface:HasTestBenchCommandExecutor
Return a reference to the relatedTestBenchCommandExecutor
instance.- Specified by:
getCommandExecutor
in interfaceHasTestBenchCommandExecutor
- Returns:
- the
TestBenchCommandExecutor
instance
-
testBench
Convenience method the returnTestBenchCommands
for the defaultWebDriver
instance.- Returns:
- The driver cast to a TestBenchCommands instance.
-
concatUrl
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 URLuri
- 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. aswaitUntilNot(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 ofsource
element into thetarget
element.- Parameters:
source
-target
-
-
drag
public void drag(org.openqa.selenium.WebElement source) Simulate only a drag ofsource
.- Parameters:
source
-
-
dragElementOver
public void dragElementOver(org.openqa.selenium.WebElement source, org.openqa.selenium.WebElement target) Simulate a drag ofsource
element and over thetarget
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. aswaitUntilNot(ExpectedCondition)
.- Type Parameters:
T
- the return type of the expected condition- Parameters:
condition
- the condition to wait for to become falsetimeoutInSeconds
- 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
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 testclassName
- 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 elementactualElement
- 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 horizontallydeltaY
- 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, notnull
-
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
Clicks on the element, using JS. This method is more convenient then SeleniumfindElement(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 Seleniumelement.click()
, because Selenium method changes scroll position, which is not always needed.- Parameters:
element
- the element to be clicked on
-
getLogEntries
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
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 theblur()
function on the current active element of the page, if any.
-