Skip to main content

Browser Actions

Getting Started

To interact with web pages, create an instance of SHAFT.GUI.WebDriver:

DriverSetup.java
SHAFT.GUI.WebDriver driver = new SHAFT.GUI.WebDriver();

SHAFT detects your configuration from property files. If no properties are set, SHAFT uses sensible defaults: local execution, Chrome browser, and automatic driver management via Selenium Manager.

To close all running driver instances:

DriverTeardown.java
driver.quit();
NavigateToURL.java
driver.browser().navigateToURL("https://www.google.com");

Navigates to the specified URL. If the URL matches the current page, it refreshes instead. You can optionally verify the target URL after navigation:

NavigateToURLWithVerification.java
driver.browser().navigateToURL("https://www.google.com/", "google");
NavigateNewTab.java
import org.openqa.selenium.WindowType;

driver.browser().navigateToURL("https://www.google.com", WindowType.TAB);
driver.browser().navigateToURL("https://www.google.com", WindowType.WINDOW);
NavigateBack.java
driver.browser().navigateBack();

Navigates one step back in the browser history.

NavigateForward.java
driver.browser().navigateForward();

Navigates one step forward in the browser history.

Refresh Page

RefreshPage.java
driver.browser().refreshCurrentPage();

Refreshes the current page.

Get Current URL

GetCurrentURL.java
String currentUrl = driver.browser().getCurrentURL();

Returns the URL of the current page.

NavigateWithBasicAuth.java
driver.browser().navigateToURLWithBasicAuthentication(
"https://staging.example.com/secure",
"myUsername",
"myPassword",
"https://staging.example.com/dashboard"
);

Navigates to a URL that requires HTTP Basic Authentication. Provide the target URL, credentials, and the expected URL after a successful login. Useful for staging environments and internal tools protected by basic auth.

Window Management

Maximize Window

MaximizeWindow.java
driver.browser().maximizeWindow();

Maximizes the current browser window.

Full Screen Window

FullScreenWindow.java
driver.browser().fullScreenWindow();

Sets the current window to full screen mode.

Resize Window

ResizeWindow.java
driver.browser().setWindowSize(1440, 900);

Resizes the current window to the specified width and height.

Get Window Size

GetWindowSize.java
String windowSize = driver.browser().getWindowSize();

Returns the current window size as a string.

Get Window Title

GetWindowTitle.java
String title = driver.browser().getCurrentWindowTitle();

Returns the current window title.

Close Current Window

CloseWindow.java
driver.browser().closeCurrentWindow();

Closes the current browser window.

Switch Windows or Tabs

SwitchWindows.java
String windowHandle = driver.browser().getWindowHandle();
// ... code that opens a new window ...
driver.browser().switchToWindow(windowHandle); // switch back to the original window

The getWindowHandle() method returns a unique identifier for the current window, which can be used to switch between tabs and windows.

Get Page Source

GetPageSource.java
String pageSource = driver.browser().getPageSource();

Returns the current page source as a string.

Cookies

AddCookie.java
driver.browser().addCookie("cookieName", "cookieValue");
GetCookie.java
Cookie cookie = driver.browser().getCookie("cookieName");

Get All Cookies

GetAllCookies.java
Set<Cookie> cookies = driver.browser().getAllCookies();
GetCookieValue.java
String cookieValue = driver.browser().getCookieValue("cookieName");
GetCookieDomain.java
String cookieDomain = driver.browser().getCookieDomain("cookieName");
GetCookiePath.java
String cookiePath = driver.browser().getCookiePath("cookieName");
DeleteCookie.java
driver.browser().deleteCookie("cookieName");

Delete All Cookies

DeleteAllCookies.java
driver.browser().deleteAllCookies();

Screenshots and Snapshots

Capture Screenshot

CaptureScreenshot.java
driver.browser().captureScreenshot();

Captures a screenshot and attaches it to the Allure report.

captureSnapshot() vs capturePageSnapshot()

SHAFT provides two distinct snapshot methods — choose the one that fits your reporting needs:

MethodWhat it capturesAttached to Allure
captureSnapshot()Full-page screenshot and page sourceYes — both screenshot and HTML
capturePageSnapshot()Serialized DOM/page data only (no image)Yes — HTML source only

captureSnapshot()

CaptureSnapshot.java
driver.browser().captureSnapshot();

Captures a full page snapshot including both a screenshot and the page source, and attaches both to the Allure report.

capturePageSnapshot()

CapturePageSnapshot.java
driver.browser().capturePageSnapshot();

Captures and serializes the current page DOM data and attaches it to the Allure report as an HTML artifact. Use this when you only need the page structure without a visual screenshot.

generateLightHouseReport()

LightHouseReport.java
driver.browser().generateLightHouseReport();

Triggers a Google Lighthouse performance audit on the currently open page. The generated report is attached to the Allure output. Useful for catching performance regressions in CI/CD pipelines.

tip

Configure openLighthouseReportWhileExecution=true in your properties file to automatically open the Lighthouse report during test execution.

Wait Actions

Wait for Lazy Loading

WaitForLazyLoading.java
driver.browser().waitForLazyLoading();

Waits for lazy-loaded content to finish loading on the page.

Wait Until Title Is

WaitUntilTitle.java
driver.browser().waitUntilTitleIs("Expected Title");
driver.browser().waitUntilTitleContains("Partial Title");
driver.browser().waitUntilTitleNotContains("Old Title");

Wait Until URL Matches

WaitUntilURL.java
driver.browser().waitUntilUrlToBe("https://example.com/dashboard");
driver.browser().waitUntilUrlContains("dashboard");
driver.browser().waitUntilUrlNotContains("login");
driver.browser().waitUntilUrlMatches(".*dashboard.*");

Wait Until Number of Windows

WaitUntilWindows.java
driver.browser().waitUntilNumberOfWindowsToBe(2);

Network Interception

Mock HTTP Requests

MockRequest.java
import java.io.ByteArrayInputStream;
import org.openqa.selenium.remote.http.HttpRequest;
import org.openqa.selenium.remote.http.HttpResponse;

driver.browser().mock(
request -> request.getUri().contains("/api/data"),
new HttpResponse().setStatus(200).setContent(() -> new ByteArrayInputStream("{}".getBytes()))
);

Intercepts HTTP requests matching the predicate and returns the mocked response.

Intercept HTTP Requests

InterceptRequest.java
import java.io.ByteArrayInputStream;

driver.browser().intercept(
request -> request.getUri().contains("/api/data"),
new HttpResponse().setStatus(200).setContent(() -> new ByteArrayInputStream("{}".getBytes()))
);

Mobile Context

Get and Set Context

MobileContext.java
String context = driver.browser().getContext();
driver.browser().setContext("WEBVIEW_1");

Get Context Handles

ContextHandles.java
List<String> contexts = driver.browser().getContextHandles();

Accessibility Testing

SHAFT Engine integrates axe-core to run automated WCAG accessibility audits. Chain .accessibility() onto any browser action to start auditing:

AccessibilityExample.java
driver.browser().navigateToURL("https://example.com")
.accessibility()
.assertNoCriticalViolations("Home Page");

For a full reference of all accessibility methods, see Accessibility Testing.

Fluent Chaining

All browser actions support fluent chaining with .and():

FluentChaining.java
driver.browser()
.navigateToURL("https://www.google.com")
.and().maximizeWindow()
.and().captureScreenshot();
tip

SHAFT provides automatic reporting for every browser action. Check the Reporting section in the sidebar for details on the rich reports generated for each action.