public class SeleniumHelper
Provides helper methods for Selenium functional tests. Wraps counters for screenshots and other states which require initialization.
| Modifier and Type | Class and Description |
|---|---|
class |
SeleniumHelper.TextToBePresentInElementWithText
Allows to retrieve the text parameter passed to
ExpectedConditions#textToBePresentInElement(org.openqa.selenium.WebElement, java.lang.String) as well as test the type which is useful for unit testing. |
| Modifier and Type | Field and Description |
|---|---|
static org.hamcrest.Matcher |
MISSING_DOCTYPE_MESSAGE_MATCHER
Accounts for the fact that some Selenium driver implementations don't include the doctype in their page source return value, but we might want to evaluate the driver's page source rather than the real page's source.
|
static java.lang.String |
PAGE_SOURCE_END_MARKER_DEFAULT |
static java.lang.String |
PAGE_SOURCE_START_MARKER_DEFAULT |
static java.lang.String |
SCREENSHOT_END_MARKER_DEFAULT |
static java.lang.String |
SCREENSHOT_START_MARKER_DEFAULT |
static int |
WEB_DRIVER_WAIT_TIMEOUT_DEFAULT |
| Constructor and Description |
|---|
SeleniumHelper()
Creates a new
SeleniumHelper using the defaults of #SeleniumHelper(int) and #WEB_DRIVER_WAIT_TIMEOUT_DEFAULT for the web driver wait timeout |
SeleniumHelper()
Creates a new
SeleniumHelper using default values #SCREENSHOT_START_MARKER_DEFAULT, #SCREENSHOT_END_MARKER_DEFAULT, #PAGE_SOURCE_START_MARKER_DEFAULT, #PAGE_SOURCE_END_MARKER_DEFAULT and a temporary screenshot directory |
SeleniumHelper() |
SeleniumHelper() |
SeleniumHelper()
Creates a new
SeleniumHelper. |
SeleniumHelper() |
| Modifier and Type | Method and Description |
|---|---|
static org.openqa.selenium.support.ui.ExpectedCondition<java.lang.Boolean> |
absenceOfWebElement()
Allows to check for the absence of a web element. See https://stackoverflow.com/questions/6533597/webdriver-how-to-check-if-an-page-object-web-element-exists which is referencing https://groups.google.com/forum/#!topic/webdriver/kJqbbLVo40E for an explanation why this is a good if not the only way to perform the check.
|
void |
assertMessagesContains() |
void |
assertMessagesContains() |
void |
assertMessagesContainsAny()
Checks whether any string in
text is contained in the p:messages element messages using and returns after the first match or fails using MessageTextNotContainedException if none of texts match. Depending on the Selenium configuration this might a TimeoutException for every non-matching string in texts which can be ignored. |
static void |
assertResponseCodeEquals()
Compares the response code of
uRLConnection to responseCode |
static void |
assertResponseCodeEquals()
Compares the response code of
uRLConnection to responseCode |
org.openqa.selenium.support.ui.FluentWait<org.openqa.selenium.WebDriver> |
createFluentWait()
Factory method for tests which has to be used until support for mocking constructors via instrumentalisation with PowerMockito works in JUnit 5 (see comments in SeleniumHelperTest.testAssertMessagesContainsAny for details and an issue report link).
|
static java.util.Set<java.util.List> |
createMessagePermutation()
Creates the permutation of
texts which might be useful in in order to reflect that validation constraint violoations are generally in undefined order since Validator.validate returns a Set.This methods creates n faculty (n!) items for n items in text which can take a long time to check (five items can already take up to a minute). Note that the type of the collection used for texts determines whether the return value contains the item more than once or not. An example for steam fetichist (involving SteamEx stream extension): createMessagePermutation(StreamEx.of(entities.stream()) .map(prop -> String.format("invalid: key '%s' mustn't be mapped to null", prop.getName())) .append("invalid: mapping for this entity isn't valid. Details should have been given to you.") .collect(Collectors.toSet())) .stream() .map(perm -> StreamEx.of(perm) .joining("\n", String.format("%s\n" + "The following constraints are violated:\n", BackingBean.X_FAILED_ERROR_SUMMARY), String.format("\nFix the corresponding values in the components."))) //joining isn't necessary because \n could have been //added to each of the permutation items, this was //used as a way to get to know the awesome StreamEx //library .collect(Collectors.toSet()); |
static java.util.Set<java.util.List> |
createMessagePermutation() |
org.openqa.selenium.support.ui.WebDriverWait |
createWebDriverWait() |
void |
logDOMTree()
Logs the current state of the DOM tree by executing
return document.childNodes[1].outerHTML; in browser. |
void |
logPageSource() |
void |
logPageSourcePretty() |
void |
logPageSourcePretty()
Logs the page source retrieved from
browser to logger as info message. |
void |
logPageSourceURLPretty() |
static java.lang.String |
randomString()
Wrapper around random string generation which allows to quickly avoid temporary problems like Chrome Selenium driver not accepting unicode characters outside Basic Multilingual Plane (BMP) (asked https://stackoverflow.com/questions/46950897/how-to-produce-random-basic-multilingual-plane-bmp-strings-in-java for inputs)
|
java.lang.String |
retrieveMessagesText() |
static java.lang.String |
retrievePlainText() |
static org.openqa.selenium.WebElement |
retrieveSlideMenuItem()
Retrieves a specific menu item in the nested submenus opened based on the specified indices assuming that all menu items are links.
|
static java.util.List<org.openqa.selenium.WebElement> |
retrieveSlideMenuItems()
Retrieves all menu items in the nested submenus opened based on the specified indices assuming that all menu items are links.
|
void |
screenshot()
Takes a screenshot with description
#NO_DESCRIPTION. |
void |
screenshot()
Takes a screenshot of
browser and stores it according to the value of the system property #SCREENSHOT_OUTPUT_MODE_PROPERTY_NAME while managing the numbering and description in file names. |
void |
screenshotFullPage() |
void |
screenshotFullPage() |
void |
validatePageSource() |
void |
validatePageSource() |
void |
validatePageSource() |
<V> void |
webDriverWait() |
<V> void |
webDriverWait() |
<V> void |
webDriverWait()
Creates a WebDriverWait and allows the timeout configuration in this instance to be overridden (while other configuration parameters of this instance can still be used).
|
<V> void |
webDriverWait()
Creates a WebDriverWait and allows the timeout configuration in this instance to be overridden (while other configuration parameters of this instance can still be used).
|
<V> void |
webDriverWait()
Creates a WebDriverWait and allows the timeout configuration in this instance to be overridden (while other configuration parameters of this instance can still be used).
|
public static java.lang.String SCREENSHOT_START_MARKER_DEFAULT
public static java.lang.String SCREENSHOT_END_MARKER_DEFAULT
public static java.lang.String PAGE_SOURCE_START_MARKER_DEFAULT
public static java.lang.String PAGE_SOURCE_END_MARKER_DEFAULT
public static int WEB_DRIVER_WAIT_TIMEOUT_DEFAULT
public static org.hamcrest.Matcher MISSING_DOCTYPE_MESSAGE_MATCHER
Accounts for the fact that some Selenium driver implementations don't include the doctype in their page source return value, but we might want to evaluate the driver's page source rather than the real page's source.
public SeleniumHelper()
Creates a new SeleniumHelper using the defaults of #SeleniumHelper(int) and #WEB_DRIVER_WAIT_TIMEOUT_DEFAULT for the web driver wait timeout
public SeleniumHelper()
Creates a new SeleniumHelper using default values #SCREENSHOT_START_MARKER_DEFAULT, #SCREENSHOT_END_MARKER_DEFAULT, #PAGE_SOURCE_START_MARKER_DEFAULT, #PAGE_SOURCE_END_MARKER_DEFAULT and a temporary screenshot directory
public SeleniumHelper()
public SeleniumHelper()
public SeleniumHelper()
Creates a new SeleniumHelper.
screenshotDirscreenshotStartMarker, screeenshotEndMarker, pageSourceStartMarker, pageSourceEndMarker or screenshotDir are nullpublic SeleniumHelper()
public static java.lang.String randomString()
Wrapper around random string generation which allows to quickly avoid temporary problems like Chrome Selenium driver not accepting unicode characters outside Basic Multilingual Plane (BMP) (asked https://stackoverflow.com/questions/46950897/how-to-produce-random-basic-multilingual-plane-bmp-strings-in-java for inputs)
public java.lang.String retrieveMessagesText()
public static java.lang.String retrievePlainText()
public void assertMessagesContains()
public void assertMessagesContains()
public void assertMessagesContainsAny()
Checks whether any string in text is contained in the p:messages element messages using and returns after the first match or fails using MessageTextNotContainedException if none of SeleniumHelper.webDriverWaittexts match. Depending on the Selenium configuration this might a TimeoutException for every non-matching string in texts which can be ignored.
messagestexts is null or empty in order to be able to detect unexpected text conditions earlySeleniumHelper.webDriverWaitpublic static java.util.Set<java.util.List> createMessagePermutation()
Creates the permutation of texts which might be useful in in order to reflect that validation constraint violoations are generally in undefined order since SeleniumHelper.assertMessagesContainsAnyValidator.validate returns a Set.This methods creates n faculty (n!) items for n items in text which can take a long time to check (five items can already take up to a minute). Note that the type of the collection used for texts determines whether the return value contains the item more than once or not. An example for steam fetichist (involving SteamEx stream extension): createMessagePermutation(StreamEx.of(entities.stream()) .map(prop -> String.format("invalid: key '%s' mustn't be mapped to null", prop.getName())) .append("invalid: mapping for this entity isn't valid. Details should have been given to you.") .collect(Collectors.toSet())) .stream() .map(perm -> StreamEx.of(perm) .joining("\n", String.format("%s\n" + "The following constraints are violated:\n", BackingBean.X_FAILED_ERROR_SUMMARY), String.format("\nFix the corresponding values in the components."))) //joining isn't necessary because \n could have been //added to each of the permutation items, this was //used as a way to get to know the awesome StreamEx //library .collect(Collectors.toSet());
SeleniumHelper.assertMessagesContainsAnypublic static java.util.Set<java.util.List> createMessagePermutation()
public void screenshot()
Takes a screenshot with description #NO_DESCRIPTION.
SeleniumHelper.screenshotpublic void screenshot()
Takes a screenshot of browser and stores it according to the value of the system property #SCREENSHOT_OUTPUT_MODE_PROPERTY_NAME while managing the numbering and description in file names.
browser or description is nullpublic void screenshotFullPage()
public void screenshotFullPage()
public void validatePageSource()
public void validatePageSource()
public void validatePageSource()
public void logPageSource()
public void logPageSourcePretty()
public void logPageSourcePretty()
Logs the page source retrieved from browser to logger as info message.
public void logPageSourceURLPretty()
public void logDOMTree()
Logs the current state of the DOM tree by executing return document.childNodes[1].outerHTML; in browser.
ParserConfigurationException - if a parser configuration error occurs during setup of pretty printing dependenciesSAXException - if a SAX exception occurs during setup of pretty printing dependenciespublic <V> void webDriverWait()
public <V> void webDriverWait()
public <V> void webDriverWait()
Creates a WebDriverWait and allows the timeout configuration in this instance to be overridden (while other configuration parameters of this instance can still be used).
public <V> void webDriverWait()
Creates a WebDriverWait and allows the timeout configuration in this instance to be overridden (while other configuration parameters of this instance can still be used).
public <V> void webDriverWait()
Creates a WebDriverWait and allows the timeout configuration in this instance to be overridden (while other configuration parameters of this instance can still be used).
public static void assertResponseCodeEquals()
Compares the response code of uRLConnection to responseCode
responseCodepublic static void assertResponseCodeEquals()
Compares the response code of uRLConnection to responseCode
responseCodepublic static org.openqa.selenium.WebElement retrieveSlideMenuItem()
Retrieves a specific menu item in the nested submenus opened based on the specified indices assuming that all menu items are links.
public static java.util.List<org.openqa.selenium.WebElement> retrieveSlideMenuItems()
Retrieves all menu items in the nested submenus opened based on the specified indices assuming that all menu items are links.
public static org.openqa.selenium.support.ui.ExpectedCondition<java.lang.Boolean> absenceOfWebElement()
Allows to check for the absence of a web element. See https://stackoverflow.com/questions/6533597/webdriver-how-to-check-if-an-page-object-web-element-exists which is referencing https://groups.google.com/forum/#!topic/webdriver/kJqbbLVo40E for an explanation why this is a good if not the only way to perform the check.
public org.openqa.selenium.support.ui.FluentWait<org.openqa.selenium.WebDriver> createFluentWait()
Factory method for tests which has to be used until support for mocking constructors via instrumentalisation with PowerMockito works in JUnit 5 (see comments in SeleniumHelperTest.testAssertMessagesContainsAny for details and an issue report link).
FluentWait referencepublic org.openqa.selenium.support.ui.WebDriverWait createWebDriverWait()