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 voidassertEquals(org.openqa.selenium.WebElement expectedElement, org.openqa.selenium.WebElement actualElement) Assert that the two elements are equal.voidblur()Calls theblur()function on the current active element of the page, if any.protected voidChecks browser's log entries, throws an error for any client-side error and logs any client-side warnings.protected voidcheckLogsForErrors(Predicate<String> acceptableMessagePredicate) Checks browser's log entries, throws an error for any client-side error and logs any client-side warnings.protected voidclickElementWithJs(String elementId) Clicks on the element, using JS.protected voidclickElementWithJs(org.openqa.selenium.WebElement element) Clicks on the element, using JS.static StringCombines a base URL with an URI to create a final URL.voiddrag(org.openqa.selenium.WebElement source) Simulate only a drag ofsource.voiddragAndDrop(org.openqa.selenium.WebElement source, org.openqa.selenium.WebElement target) Simulate DnD ofsourceelement into thetargetelement.voiddragElementOver(org.openqa.selenium.WebElement source, org.openqa.selenium.WebElement target) Simulate a drag ofsourceelement and over thetargetelement.executeScript(String script, Object... args) Executes the given JavaScript in the context of the currently selected frame or window.org.openqa.selenium.WebElementfindElement(org.openqa.selenium.By by) List<org.openqa.selenium.WebElement> findElements(org.openqa.selenium.By by) Return a reference to the relatedTestBenchCommandExecutorinstance.org.openqa.selenium.SearchContextGet a reference or a new instance of the SearchContext applicable to this classabstract org.openqa.selenium.WebDriverReturns theWebDriverinstance or (if the previously provided WebDriver instance was not already aTestBenchDriverProxyinstance) aTestBenchDriverProxythat 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 intGets current scroll position on x axis.protected intGets current scroll position on y axis.protected booleanhasCssClass(org.openqa.selenium.WebElement element, String className) Checks if the given element has the given class name.booleanisElementPresent(org.openqa.selenium.By by) Returns true if an element can be found from the driver with given selector.protected voidscrollBy(int deltaX, int deltaY) Scrolls the page by given amount of x and y deltas.protected voidscrollIntoViewAndClick(org.openqa.selenium.WebElement element) Scrolls the page to the element specified and clicks it.protected voidscrollToElement(org.openqa.selenium.WebElement element) Scrolls the page to the element given using javascript.Convenience method the returnTestBenchCommandsfor the defaultWebDriverinstance.protected voidIf dev server start in progress wait until it's started.protected voidwaitForElementNotPresent(org.openqa.selenium.By by) protected voidwaitForElementPresent(org.openqa.selenium.By by) protected voidwaitForElementVisible(org.openqa.selenium.By by) <T> TwaitUntil(org.openqa.selenium.support.ui.ExpectedCondition<T> condition) Waits up to 10 seconds for the given condition to become neither null nor false.<T> TwaitUntil(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> voidwaitUntilNot(org.openqa.selenium.support.ui.ExpectedCondition<T> condition) Waits up to 10s for the given condition to become false.protected <T> voidwaitUntilNot(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, waitMethods inherited from interface com.vaadin.testbench.HasElementQuery
$, $
-
Constructor Details
-
AbstractBrowserTestBase
public AbstractBrowserTestBase()
-
-
Method Details
-
getDriver
public abstract org.openqa.selenium.WebDriver getDriver()Returns theWebDriverinstance or (if the previously provided WebDriver instance was not already aTestBenchDriverProxyinstance) aTestBenchDriverProxythat 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 theTestBenchElementto 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
WebElementasTestBenchElement.- 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:
-
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 theExpectedConditionand 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:
-
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 theExpectedConditionand 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:
-
getContext
public org.openqa.selenium.SearchContext getContext()Description copied from interface:HasSearchContextGet a reference or a new instance of the SearchContext applicable to this class- Specified by:
getContextin interfaceHasSearchContext- Returns:
- a
SearchContextinstance
-
getCommandExecutor
Description copied from interface:HasTestBenchCommandExecutorReturn a reference to the relatedTestBenchCommandExecutorinstance.- Specified by:
getCommandExecutorin interfaceHasTestBenchCommandExecutor- Returns:
- the
TestBenchCommandExecutorinstance
-
testBench
Convenience method the returnTestBenchCommandsfor the defaultWebDriverinstance.- 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 ofsourceelement into thetargetelement.- 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 ofsourceelement and over thetargetelement.- 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:
trueif matches,falseif 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.
-