Browser Actions
Getting Started
To interact with web pages, create an instance of SHAFT.GUI.WebDriver:
SHAFT.GUI.WebDriver driver = new SHAFT.GUI.WebDriver();
SHAFT detects your configuration from property files. If no properties are set, SHAFT uses sensible defaults.
To close all running driver instances:
driver.quit();
For trust-gated natural-language browser, element, and touch workflows, see Natural Language Actions.
Navigation
Navigate to URL
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:
driver.browser().navigateToURL("https://www.google.com/", "google");
Navigate to URL in a New Tab or Window
import org.openqa.selenium.WindowType;
driver.browser().navigateToURL("https://www.google.com", WindowType.TAB);
driver.browser().navigateToURL("https://www.google.com", WindowType.WINDOW);
Navigate Back
driver.browser().navigateBack();
Navigates one step back in the browser history.
Navigate Forward
driver.browser().navigateForward();
Navigates one step forward in the browser history.
Refresh Page
driver.browser().refreshCurrentPage();
Refreshes the current page.
Get Current URL
String currentUrl = driver.browser().getCurrentURL();
Returns the URL of the current page.
Navigate to URL with Basic Authentication
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
driver.browser().maximizeWindow();
Maximizes the current browser window.
Full Screen Window
driver.browser().fullScreenWindow();
Sets the current window to full screen mode.
Resize Window
driver.browser().setWindowSize(1440, 900);
Resizes the current window to the specified width and height.
Get Window Size
String windowSize = driver.browser().getWindowSize();
Returns the current window size as a string.
Get Window Title
String title = driver.browser().getCurrentWindowTitle();
Returns the current window title.
Close Current Window
driver.browser().closeCurrentWindow();
Closes the current browser window.
Switch Windows or Tabs
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
String pageSource = driver.browser().getPageSource();
Returns the current page source as a string.
Cookies
Add Cookie
driver.browser().addCookie("cookieName", "cookieValue");
Get Cookie
Cookie cookie = driver.browser().getCookie("cookieName");
Get All Cookies
Set<Cookie> cookies = driver.browser().getAllCookies();
Get Cookie Value
String cookieValue = driver.browser().getCookieValue("cookieName");
Get Cookie Domain
String cookieDomain = driver.browser().getCookieDomain("cookieName");
Get Cookie Path
String cookiePath = driver.browser().getCookiePath("cookieName");
Delete Cookie
driver.browser().deleteCookie("cookieName");
Delete All Cookies
driver.browser().deleteAllCookies();
Storage State
Use storage state when a test needs to reuse an authenticated browser session
without repeating the login flow. SHAFT saves cookies, localStorage, and
sessionStorage to a JSON file.
driver.browser()
.navigateToURL("https://app.example.com")
.and().saveStorageState("target/auth-state.json");
Load storage state after navigating to a compatible origin so browser cookie domain rules can apply.
driver.browser()
.navigateToURL("https://app.example.com")
.and().loadStorageState("target/auth-state.json")
.and().refreshCurrentPage();
Screenshots and Snapshots
Capture Screenshot
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:
| Method | What it captures | Attached to Allure |
|---|---|---|
captureSnapshot() | Full-page screenshot and page source | Yes — both screenshot and HTML |
capturePageSnapshot() | Serialized DOM/page data only (no image) | Yes — HTML source only |
captureSnapshot()
driver.browser().captureSnapshot();
Captures a full page snapshot including both a screenshot and the page source, and attaches both to the Allure report.
capturePageSnapshot()
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()
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.
Configure openLighthouseReportWhileExecution=true in your properties file to automatically open the Lighthouse report during test execution.
Wait Actions
Wait for Lazy Loading
driver.browser().waitForLazyLoading();
Waits for lazy-loaded content to finish loading on the page.
Wait Until Title Is
driver.browser().waitUntilTitleIs("Expected Title");
driver.browser().waitUntilTitleContains("Partial Title");
driver.browser().waitUntilTitleNotContains("Old Title");
Wait Until URL Matches
driver.browser().waitUntilUrlToBe("https://example.com/dashboard");
driver.browser().waitUntilUrlContains("dashboard");
driver.browser().waitUntilUrlNotContains("login");
driver.browser().waitUntilUrlMatches(".*dashboard.*");
Wait Until Number of Windows
driver.browser().waitUntilNumberOfWindowsToBe(2);
Network Interception
Intercept and Mock HTTP Requests
driver.browser()
.interceptRequest()
.get()
.urlContains("/api/data")
.respond()
.statusCode(200)
.jsonBody("{}")
.perform();
Intercepts browser HTTP requests matching the builder criteria and returns the mocked response.
Validate Intercepted Responses
driver.browser()
.interceptRequest()
.get()
.pathEquals("/api/data")
.assertResponse(response -> response
.body()
.contains("{}")
.perform());
Use clearNetworkInterceptors() to remove active browser network rules before the driver session ends.
Record, Replay, and Validate Contracts
Contract methods capture matching browser traffic into the same deterministic
contract file used by SHAFT.API. Replay turns captured responses into browser
network mocks. Assert and verify modes compare live traffic with the stored
contract and attach readable Allure diffs for mismatches.
driver.browser().startContractRecording(
"src/test/resources/contracts/search.json",
"/api/search");
driver.browser().navigateToURL("https://example.com/search");
SHAFT.Contracts.stopRecording();
driver.browser().assertContract(
"src/test/resources/contracts/search.json",
"/api/search");
driver.browser().navigateToURL("https://example.com/search");
SHAFT.Contracts.stopValidation();
driver.browser().replayContract("src/test/resources/contracts/search.json");
See UI and API contract replay for combined browser and API examples.
Browser Network Profiles
DevTools-capable Selenium drivers can switch the active browser session offline, throttle throughput, block resource patterns, and restore the default network state. Unsupported drivers keep the test running and add a deterministic observability warning to the trace metadata.
driver.browser()
.goOffline()
.and().restoreNetwork()
.and().throttleNetwork(250, 64, 32)
.and().blockNetworkResources("*.png", "*.jpg");
Call restoreNetwork() after a profile-specific assertion when later steps
need normal connectivity.
Mobile Context
Get and Set Context
String context = driver.browser().getContext();
driver.browser().setContext("WEBVIEW_1");
Get Context Handles
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:
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():
driver.browser()
.navigateToURL("https://www.google.com")
.and().maximizeWindow()
.and().captureScreenshot();
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.