Class BrowserActionsHelper
java.lang.Object
com.shaft.gui.browser.internal.BrowserActionsHelper
Internal helper class for browser actions within the SHAFT framework.
Provides utility methods for navigating URLs, reporting pass/fail results,
maximizing browser windows, capturing page snapshots, and formatting URLs
for basic authentication.
Instances are typically created per action with a isSilent flag
that suppresses Allure report entries when set to true.
Example usage:
BrowserActionsHelper helper = new BrowserActionsHelper(false);
helper.navigateToNewUrl(driver, currentUrl, "https://example.com", "");
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intThe navigation timeout in seconds, sourced fromSHAFT.Properties.timeouts.browserNavigationTimeout(). -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionorg.openqa.selenium.DimensionattemptMaximizeUsingSeleniumWebDriver(org.openqa.selenium.WebDriver driver, String executionAddress, String targetBrowserName, String targetOperatingSystem) Attempts to maximize the browser window using the Selenium WebDriverWebDriver.Window.maximize()method.org.openqa.selenium.DimensionattemptMaximizeUsingSeleniumWebDriverManageWindow(org.openqa.selenium.WebDriver driver, int width, int height) Maximizes the browser window by explicitly setting its position to(0, 0)and its size to the provided dimensions using theWebDriver.WindowAPI.org.openqa.selenium.DimensionattemptMaximizeUsingToolkitAndJavascript(org.openqa.selenium.WebDriver driver, int width, int height) Attempts to maximize the browser window by reading the physical screen dimensions fromToolkitand resizing the window accordingly.capturePageSnapshot(org.openqa.selenium.WebDriver driver) Captures a snapshot of the current page.voidcheckNavigationWasSuccessful(org.openqa.selenium.WebDriver driver, String initialURL, String targetUrl, String targetUrlAfterRedirection) Waits until the browser URL reflects a successful navigation away from the initial URL, and optionally waits until it contains the expected target URL.voidconfirmThatWebsiteIsNotDown(org.openqa.selenium.WebDriver driver, String targetUrl) Verifies that the page currently loaded in the browser is not a network-error page.voidfailAction(Exception... rootCauseException) Reports a failed browser action without a WebDriver instance or test data.voidfailAction(org.openqa.selenium.WebDriver driver, String testData, Exception... rootCauseException) Reports a failed browser action, capturing a screenshot when a driver is provided.voidfailAction(org.openqa.selenium.WebDriver driver, String actionName, String testData, Exception... rootCauseException) Reports a failed browser action with an explicit action name, then throws a test failure.formatUrlForBasicAuthentication(String username, String password, String targetUrl) Embeds Basic-Authentication credentials into a URL so that the browser submits them automatically on navigation without requiring an interactive prompt.Extracts the registrable (top-private) domain name from a full URL.voidnavigateToNewUrl(org.openqa.selenium.WebDriver driver, String initialURL, String targetUrl, String targetUrlAfterRedirection) Navigates the browser to a new URL.voidpassAction(String testData) Reports a successful browser action without a WebDriver instance.voidpassAction(org.openqa.selenium.WebDriver driver, String testData) Reports a successful browser action, attaching a screenshot when a driver is provided.voidpassAction(org.openqa.selenium.WebDriver driver, String actionName, String testData) Reports a successful browser action with an explicit action name.voidwaitUntilUrlIsNot(org.openqa.selenium.WebDriver driver, String initialURL) Waits until the browser URL is no longer equal to the given initial URL.
-
Field Details
-
NAVIGATION_TIMEOUT_INTEGER
public static final int NAVIGATION_TIMEOUT_INTEGERThe navigation timeout in seconds, sourced fromSHAFT.Properties.timeouts.browserNavigationTimeout(). Used as the maximum wait duration for URL-change conditions.
-
-
Constructor Details
-
BrowserActionsHelper
public BrowserActionsHelper(boolean isSilent) Creates a newBrowserActionsHelper.- Parameters:
isSilent- whentrue, action results are not written to the Allure report; useful for internal/utility navigations that should not appear in test output
-
-
Method Details
-
passAction
Reports a successful browser action without a WebDriver instance. The action name is derived from the caller's method name.Example:
passAction("https://example.com");- Parameters:
testData- descriptive data associated with the action (e.g. the URL navigated to)
-
passAction
Reports a successful browser action, attaching a screenshot when a driver is provided. The action name is derived from the caller's method name.Example:
passAction(driver, "Page title was correct");- Parameters:
driver- the activeWebDriverinstance used to capture a screenshot, ornulltestData- descriptive data associated with the action
-
passAction
Reports a successful browser action with an explicit action name.Example:
passAction(driver, "navigateToURL", "https://example.com");- Parameters:
driver- the activeWebDriverinstance used to capture a screenshot, ornullactionName- the display name of the action as it should appear in the reporttestData- descriptive data associated with the action
-
failAction
Reports a failed browser action without a WebDriver instance or test data. The action name is derived from the caller's method name.Example:
failAction(rootCauseException);- Parameters:
rootCauseException- optional root-cause exception(s) to include in the report
-
failAction
public void failAction(org.openqa.selenium.WebDriver driver, String testData, Exception... rootCauseException) Reports a failed browser action, capturing a screenshot when a driver is provided. The action name is derived from the caller's method name.Example:
failAction(driver, "Navigation failed", rootCauseException);- Parameters:
driver- the activeWebDriverinstance used to capture a screenshot, ornulltestData- descriptive data associated with the failurerootCauseException- optional root-cause exception(s) to include in the report
-
failAction
public void failAction(org.openqa.selenium.WebDriver driver, String actionName, String testData, Exception... rootCauseException) Reports a failed browser action with an explicit action name, then throws a test failure.Example:
failAction(driver, "navigateToURL", "https://example.com", rootCauseException);- Parameters:
driver- the activeWebDriverinstance used to capture a screenshot, ornullactionName- the display name of the action as it should appear in the reporttestData- descriptive data associated with the failurerootCauseException- optional root-cause exception(s) to include in the report
-
confirmThatWebsiteIsNotDown
Verifies that the page currently loaded in the browser is not a network-error page. Waits up toNAVIGATION_TIMEOUT_INTEGERseconds using a fluent wait, checking well-known browser error message strings (e.g. "This site can't be reached").Example:
helper.confirmThatWebsiteIsNotDown(driver, "https://example.com");- Parameters:
driver- the activeWebDriverinstancetargetUrl- the URL that was navigated to, used in the failure message if an error page is detected
-
waitUntilUrlIsNot
Waits until the browser URL is no longer equal to the given initial URL. Times out afterNAVIGATION_TIMEOUT_INTEGERseconds, failing the test if the URL does not change.Example:
helper.waitUntilUrlIsNot(driver, "about:blank");- Parameters:
driver- the activeWebDriverinstanceinitialURL- the URL that the browser should navigate away from
-
attemptMaximizeUsingSeleniumWebDriver
public org.openqa.selenium.Dimension attemptMaximizeUsingSeleniumWebDriver(org.openqa.selenium.WebDriver driver, String executionAddress, String targetBrowserName, String targetOperatingSystem) Attempts to maximize the browser window using the Selenium WebDriverWebDriver.Window.maximize()method. This is the preferred approach for most browser/OS combinations, but is skipped for Chrome on macOS in local execution (known limitation).Example:
Dimension size = helper.attemptMaximizeUsingSeleniumWebDriver(driver, "local", "GoogleChrome", "Windows");- Parameters:
driver- the activeWebDriverinstanceexecutionAddress- the grid/cloud address, or"local"for local executiontargetBrowserName- the browser name (e.g."GoogleChrome")targetOperatingSystem- the OS name (e.g."Windows","Mac")- Returns:
- the window
Dimensionafter the maximize attempt
-
attemptMaximizeUsingToolkitAndJavascript
public org.openqa.selenium.Dimension attemptMaximizeUsingToolkitAndJavascript(org.openqa.selenium.WebDriver driver, int width, int height) Attempts to maximize the browser window by reading the physical screen dimensions fromToolkitand resizing the window accordingly. Falls back to JavaScript execution when running in a headless environment whereToolkitthrowsHeadlessException.Example:
Dimension size = helper.attemptMaximizeUsingToolkitAndJavascript(driver, 1920, 1080);- Parameters:
driver- the activeWebDriverinstancewidth- the fallback width to use when the screen size cannot be determinedheight- the fallback height to use when the screen size cannot be determined- Returns:
- the window
Dimensionafter the resize attempt
-
attemptMaximizeUsingSeleniumWebDriverManageWindow
public org.openqa.selenium.Dimension attemptMaximizeUsingSeleniumWebDriverManageWindow(org.openqa.selenium.WebDriver driver, int width, int height) Maximizes the browser window by explicitly setting its position to(0, 0)and its size to the provided dimensions using theWebDriver.WindowAPI.Example:
Dimension size = helper.attemptMaximizeUsingSeleniumWebDriverManageWindow(driver, 1920, 1080);- Parameters:
driver- the activeWebDriverinstancewidth- the desired window width in pixelsheight- the desired window height in pixels- Returns:
- the window
Dimensionafter the resize
-
capturePageSnapshot
Captures a snapshot of the current page. For Chromium-based browsers the Chrome DevTools ProtocolPage.captureSnapshotcommand is used to produce a full MHTML snapshot; for all other browsers the standardWebDriver.getPageSource()is used as a fallback.Example:
String snapshot = helper.capturePageSnapshot(driver);- Parameters:
driver- the activeWebDriverinstance, ornullto fall back directly to a page-source capture- Returns:
- the serialised page data as a
String(MHTML or HTML source)
-
formatUrlForBasicAuthentication
Embeds Basic-Authentication credentials into a URL so that the browser submits them automatically on navigation without requiring an interactive prompt. Credentials are percent-encoded viaURLEncoderto handle special characters.Example:
String authUrl = helper.formatUrlForBasicAuthentication("admin", "p@ssw0rd", "https://example.com/secure"); // returns "https://admin:p%40ssw0rd@example.com/secure"- Parameters:
username- the username for Basic Authenticationpassword- the password for Basic AuthenticationtargetUrl- the target URL (must start withhttp://orhttps://)- Returns:
- the URL with embedded Basic-Auth credentials
-
getDomainNameFromUrl
Extracts the registrable (top-private) domain name from a full URL. Uses the GuavaInternetDomainNameclass to determine the public suffix and top-level domain.Example:
String domain = helper.getDomainNameFromUrl("https://www.example.co.uk/page"); // returns "example.co.uk"- Parameters:
url- the full URL string to extract the domain from- Returns:
- the registrable domain name (e.g.
"example.co.uk")
-