Package io.karatelabs.driver.w3c
Class W3cDriver
java.lang.Object
io.karatelabs.driver.w3c.W3cDriver
- All Implemented Interfaces:
CoreDriver,Driver,io.karatelabs.js.ObjectLike,io.karatelabs.js.SimpleObject
W3C WebDriver protocol driver implementation.
Single class supporting all W3C-compatible browser drivers (chromedriver, geckodriver,
safaridriver, msedgedriver) via the
W3cBrowserType enum.
Uses java.net.http.HttpClient for W3C protocol communication, cleanly separated from scenario HTTP state.
CDP-only operations (intercept, PDF, ariaTree, Mouse) throw
UnsupportedOperationException.
-
Field Summary
Fields inherited from interface io.karatelabs.js.SimpleObject
TO_STRING -
Method Summary
Modifier and TypeMethodDescriptionvoidactivate()Activate the browser window (bring to front).Get an attribute of an element.voidback()Navigate back in history.Clear an input element.voidClear all cookies.Click an element.voidclose()Alias for quit().static W3cDriverconnect(String webDriverUrl, W3cDriverOptions opts) Connect to an existing WebDriver server (remote hub, SauceLabs, etc.).Get a cookie by name.voidSet a cookie.voiddeleteCookie(String name) Delete a cookie by name.voiddialog(boolean accept) Accept or dismiss the current dialog.voidAccept or dismiss the current dialog with optional prompt input.booleanCheck if an element is enabled.Execute JavaScript via W3C executeScript.booleanCheck if an element exists.Focus an element.voidforward()Navigate forward in history.voidSwitch frames.voidMake the browser window fullscreen.Get all cookies.Get the current frame info, or null if in main frame.Get the current dialog if one is open.Get the current dialog message.Get window dimensions and position.Get driver options.getPages()Get list of all page targets (tabs).Get the underlying W3C session for direct protocol access.getTitle()Get page title.getUrl()Get current URL.Highlight an element (for debugging).Get the outer HTML of an element.Input text into an element (focus, clear, type).voidintercept(InterceptHandler handler) Enable request interception for all requests.voidintercept(List<String> patterns, InterceptHandler handler) Enable request interception with a handler.booleanCheck if driver is terminated.keys()Get a Keys object for keyboard input.Find all elements matching a locator.voidmaximize()Maximize the browser window.voidminimize()Minimize the browser window.mouse()Get a Mouse object at position (0, 0).Get a Mouse object at specified coordinates.Get a Mouse object positioned at an element's center.voidNavigate to URL.voidonDialog(DialogHandler handler) Register a handler for JavaScript dialogs (alert, confirm, prompt, beforeunload).byte[]pdf()Generate a PDF of the current page with default options.byte[]Generate a PDF of the current page.voidquit()Close driver and browser.voidrefresh()Refresh the current page.voidreload()Reload the page ignoring cache.byte[]Take screenshot and return PNG bytes.byte[]screenshot(boolean embed) Take screenshot, optionally embed in report.byte[]screenshot(String locator, boolean embed) Element-clipped screenshot via the W3CGET /session/{id}/element/{eid}/screenshotendpoint.Execute JavaScript and return result.Scroll an element into view.voidsetDimensions(Map<String, Object> dimensions) Set window dimensions and/or position.voidNavigate to URL and wait for page load.static W3cDriverStart a WebDriver from a config map.voidStop request interception.voidswitchFrame(int index) Switch to an iframe by index.voidswitchFrame(String locator) Switch to an iframe by locator, or null to return to main frame.voidswitchPage(int index) Switch to a page by index.voidswitchPage(String titleOrUrl) Switch to a page by title or URL substring.voidTab switching.Get the text content of an element.Get the value of an input element.Wait for an element to exist.Wait for an element to exist with custom timeout.waitForAny(String[] locators) Wait for any of the locators to match.waitForAny(String[] locators, Duration timeout) Wait for any of the locators to match with custom timeout.waitForAny(String locator1, String locator2) Wait for any of the locators to match.waitForEnabled(String locator) Wait for an element to be enabled.waitForEnabled(String locator, Duration timeout) Wait for an element to be enabled with custom timeout.voidwaitForPageLoad(PageLoadStrategy strategy) Wait for page to load based on strategy.voidwaitForPageLoad(PageLoadStrategy strategy, Duration timeout) Wait for page to load with custom timeout.waitForResultCount(String locator, int count) Wait for a specific number of elements to match.waitForResultCount(String locator, int count, Duration timeout) Wait for a specific number of elements to match with custom timeout.waitForText(String locator, String expected) Wait for an element to contain specific text.waitForText(String locator, String expected, Duration timeout) Wait for an element to contain specific text with custom timeout.waitForUrl(String expected) Wait for URL to contain expected string.waitForUrl(String expected, Duration timeout) Wait for URL to contain expected string with custom timeout.booleanWait until a JavaScript expression evaluates to truthy.Wait until a JavaScript expression on an element evaluates to truthy.Wait until a JavaScript expression on an element evaluates to truthy.booleanWait until a JavaScript expression evaluates to truthy with custom timeout.Wait until a supplier returns a truthy value.Wait until a supplier returns a truthy value with custom timeout.voidWindow management.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface io.karatelabs.driver.Driver
above, below, delay, drainOpenedTargets, eval, failureScreenshot, highlight, highlightAll, highlightAll, input, input, input, isReady, isResponsive, jsGet, leftOf, locate, locateAll, near, optional, position, position, property, putMember, rightOf, screenshot, script, script, scriptAll, scriptAll, select, select, setCookies, submit, switchPageById, timeout, timeout, value, waitForResultCount, waitIfSubmitRequested, waitUntilReadyMethods inherited from interface io.karatelabs.js.ObjectLike
getMember, getPrototype, isExtensible, isFrozen, isOwnProperty, isSealed, putMember, removeMember, setExtensible, setFrozen, setSealedMethods inherited from interface io.karatelabs.js.SimpleObject
getMember, jsKeys, jsToString, removeMember, toMap
-
Method Details
-
start
Start a WebDriver from a config map. If webDriverUrl is set, connects to remote. Otherwise, launches the driver process. -
connect
Connect to an existing WebDriver server (remote hub, SauceLabs, etc.). -
getSession
Get the underlying W3C session for direct protocol access. -
eval
Execute JavaScript via W3C executeScript.Battle-tested pattern from v1 WebDriver: if JS execution fails, sleep once and retry before throwing. This handles transient failures that occur when the page is still loading or transitioning. The v1 codebase proved this single-retry approach effective across thousands of real-world test suites.
W3C element references in the result are auto-wrapped as W3cElement instances.
- Specified by:
evalin interfaceCoreDriver- Specified by:
evalin interfaceDriver- Parameters:
js- JavaScript expression to execute- Returns:
- the result of the JavaScript execution
-
keys
Description copied from interface:DriverGet a Keys object for keyboard input.- Specified by:
keysin interfaceCoreDriver- Specified by:
keysin interfaceDriver- Returns:
- the Keys object for typing and key events
-
screenshot
public byte[] screenshot()Description copied from interface:DriverTake screenshot and return PNG bytes.- Specified by:
screenshotin interfaceCoreDriver- Specified by:
screenshotin interfaceDriver- Returns:
- PNG image bytes
-
frame
Description copied from interface:DriverSwitch frames. Dispatches toDriver.switchFrame(int)orDriver.switchFrame(String).- Specified by:
framein interfaceCoreDriver- Specified by:
framein interfaceDriver- Parameters:
target- frame identifier: -nullto return to main frame -Integerfor frame index -Stringfor frame locator
-
dialog
Description copied from interface:DriverAccept or dismiss the current dialog with optional prompt input.- Specified by:
dialogin interfaceCoreDriver- Specified by:
dialogin interfaceDriver- Parameters:
accept- true to accept (OK), false to dismiss (Cancel)input- text for prompt dialogs (ignored if accept is false)
-
mouse
Description copied from interface:DriverGet a Mouse object at position (0, 0).- Specified by:
mousein interfaceCoreDriver- Specified by:
mousein interfaceDriver- Returns:
- a new Mouse object at position (0, 0)
-
pdf
Description copied from interface:DriverGenerate a PDF of the current page.- Specified by:
pdfin interfaceCoreDriver- Specified by:
pdfin interfaceDriver- Parameters:
pdfOptions- PDF options (scale, landscape, margins, etc.), or null for defaults- Returns:
- PDF bytes
-
cookie
Description copied from interface:DriverGet a cookie by name.- Specified by:
cookiein interfaceCoreDriver- Specified by:
cookiein interfaceDriver- Parameters:
name- the cookie name- Returns:
- the cookie as a Map, or null if not found
-
cookie
Description copied from interface:DriverSet a cookie.- Specified by:
cookiein interfaceCoreDriver- Specified by:
cookiein interfaceDriver- Parameters:
cookie- the cookie as a Map with keys: name, value, domain, path, etc.
-
intercept
Description copied from interface:DriverEnable request interception with a handler.- Specified by:
interceptin interfaceCoreDriver- Specified by:
interceptin interfaceDriver- Parameters:
patterns- URL patterns to intercept (e.g., "*api/*")handler- the intercept handler (return InterceptResponse to mock, null to continue)
-
window
Description copied from interface:DriverWindow management. Dispatches to maximize(), minimize(), fullscreen().- Specified by:
windowin interfaceCoreDriver- Specified by:
windowin interfaceDriver- Parameters:
operation- one of: "maximize", "minimize", "fullscreen", "normal"
-
tab
Description copied from interface:DriverTab switching. Dispatches toDriver.switchPage(int)orDriver.switchPage(String).- Specified by:
tabin interfaceCoreDriver- Specified by:
tabin interfaceDriver- Parameters:
target- tab identifier: -Integerfor tab index -Stringfor title or URL substring match
-
click
Description copied from interface:DriverClick an element. -
input
Description copied from interface:DriverInput text into an element (focus, clear, type). For time/date inputs that don't respond to CDP keystrokes, uses JS value assignment. -
clear
Description copied from interface:DriverClear an input element. -
focus
Description copied from interface:DriverFocus an element. -
scroll
Description copied from interface:DriverScroll an element into view. -
highlight
Description copied from interface:DriverHighlight an element (for debugging). -
text
Description copied from interface:DriverGet the text content of an element. -
html
Description copied from interface:DriverGet the outer HTML of an element. -
value
Description copied from interface:DriverGet the value of an input element. -
attribute
Description copied from interface:DriverGet an attribute of an element. -
enabled
Description copied from interface:DriverCheck if an element is enabled. -
exists
Description copied from interface:DriverCheck if an element exists. -
locateAll
Description copied from interface:DriverFind all elements matching a locator. Implementation requires driver-specific indexed locator support. -
waitFor
Description copied from interface:DriverWait for an element to exist. -
waitFor
Description copied from interface:DriverWait for an element to exist with custom timeout. -
waitForAny
Description copied from interface:DriverWait for any of the locators to match.- Specified by:
waitForAnyin interfaceDriver
-
waitForAny
Description copied from interface:DriverWait for any of the locators to match.- Specified by:
waitForAnyin interfaceDriver
-
waitForAny
Description copied from interface:DriverWait for any of the locators to match with custom timeout.- Specified by:
waitForAnyin interfaceDriver
-
waitForText
Description copied from interface:DriverWait for an element to contain specific text.- Specified by:
waitForTextin interfaceDriver
-
waitForText
Description copied from interface:DriverWait for an element to contain specific text with custom timeout.- Specified by:
waitForTextin interfaceDriver
-
waitForEnabled
Description copied from interface:DriverWait for an element to be enabled.- Specified by:
waitForEnabledin interfaceDriver
-
waitForEnabled
Description copied from interface:DriverWait for an element to be enabled with custom timeout.- Specified by:
waitForEnabledin interfaceDriver
-
waitForUrl
Description copied from interface:DriverWait for URL to contain expected string.- Specified by:
waitForUrlin interfaceDriver
-
waitForUrl
Description copied from interface:DriverWait for URL to contain expected string with custom timeout.- Specified by:
waitForUrlin interfaceDriver
-
waitUntil
Description copied from interface:DriverWait until a JavaScript expression on an element evaluates to truthy. -
waitUntil
Description copied from interface:DriverWait until a JavaScript expression on an element evaluates to truthy. -
waitUntil
Description copied from interface:DriverWait until a JavaScript expression evaluates to truthy. -
waitUntil
Description copied from interface:DriverWait until a JavaScript expression evaluates to truthy with custom timeout. -
waitUntil
Description copied from interface:DriverWait until a supplier returns a truthy value. -
waitUntil
Description copied from interface:DriverWait until a supplier returns a truthy value with custom timeout. -
waitForResultCount
Description copied from interface:DriverWait for a specific number of elements to match.- Specified by:
waitForResultCountin interfaceDriver
-
waitForResultCount
Description copied from interface:DriverWait for a specific number of elements to match with custom timeout.- Specified by:
waitForResultCountin interfaceDriver
-
getUrl
Description copied from interface:DriverGet current URL. -
getTitle
Description copied from interface:DriverGet page title. -
deleteCookie
Description copied from interface:DriverDelete a cookie by name.- Specified by:
deleteCookiein interfaceDriver
-
clearCookies
public void clearCookies()Description copied from interface:DriverClear all cookies.- Specified by:
clearCookiesin interfaceDriver
-
getCookies
Description copied from interface:DriverGet all cookies.- Specified by:
getCookiesin interfaceDriver
-
getDimensions
Description copied from interface:DriverGet window dimensions and position.- Specified by:
getDimensionsin interfaceDriver
-
setDimensions
Description copied from interface:DriverSet window dimensions and/or position.- Specified by:
setDimensionsin interfaceDriver
-
getPages
Description copied from interface:DriverGet list of all page targets (tabs). -
getDialog
Description copied from interface:DriverGet the current dialog if one is open. This is useful for detecting dialogs that appear after actions. -
onDialog
Description copied from interface:DriverRegister a handler for JavaScript dialogs (alert, confirm, prompt, beforeunload). -
setUrl
Description copied from interface:DriverNavigate to URL and wait for page load. -
waitForPageLoad
Description copied from interface:DriverWait for page to load based on strategy.- Specified by:
waitForPageLoadin interfaceDriver
-
waitForPageLoad
Description copied from interface:DriverWait for page to load with custom timeout.- Specified by:
waitForPageLoadin interfaceDriver
-
refresh
public void refresh()Description copied from interface:DriverRefresh the current page. -
reload
public void reload()Description copied from interface:DriverReload the page ignoring cache. -
back
public void back()Description copied from interface:DriverNavigate back in history. -
forward
public void forward()Description copied from interface:DriverNavigate forward in history. -
script
Description copied from interface:DriverExecute JavaScript and return result. -
screenshot
public byte[] screenshot(boolean embed) Description copied from interface:DriverTake screenshot, optionally embed in report.- Specified by:
screenshotin interfaceDriver
-
screenshot
Element-clipped screenshot via the W3CGET /session/{id}/element/{eid}/screenshotendpoint.- Specified by:
screenshotin interfaceDriver
-
getDialogText
Description copied from interface:DriverGet the current dialog message.- Specified by:
getDialogTextin interfaceDriver
-
dialog
public void dialog(boolean accept) Description copied from interface:DriverAccept or dismiss the current dialog. -
switchFrame
public void switchFrame(int index) Description copied from interface:DriverSwitch to an iframe by index.- Specified by:
switchFramein interfaceDriver
-
switchFrame
Description copied from interface:DriverSwitch to an iframe by locator, or null to return to main frame.- Specified by:
switchFramein interfaceDriver
-
getCurrentFrame
Description copied from interface:DriverGet the current frame info, or null if in main frame.- Specified by:
getCurrentFramein interfaceDriver
-
maximize
public void maximize()Description copied from interface:DriverMaximize the browser window. -
minimize
public void minimize()Description copied from interface:DriverMinimize the browser window. -
fullscreen
public void fullscreen()Description copied from interface:DriverMake the browser window fullscreen.- Specified by:
fullscreenin interfaceDriver
-
activate
public void activate()Description copied from interface:DriverActivate the browser window (bring to front). -
pdf
public byte[] pdf()Description copied from interface:DriverGenerate a PDF of the current page with default options. -
mouse
Description copied from interface:DriverGet a Mouse object positioned at an element's center. -
mouse
Description copied from interface:DriverGet a Mouse object at specified coordinates. -
switchPage
Description copied from interface:DriverSwitch to a page by title or URL substring.- Specified by:
switchPagein interfaceDriver
-
switchPage
public void switchPage(int index) Description copied from interface:DriverSwitch to a page by index.- Specified by:
switchPagein interfaceDriver
-
intercept
Description copied from interface:DriverEnable request interception for all requests. -
stopIntercept
public void stopIntercept()Description copied from interface:DriverStop request interception.- Specified by:
stopInterceptin interfaceDriver
-
close
public void close()Description copied from interface:DriverAlias for quit(). -
quit
public void quit()Description copied from interface:DriverClose driver and browser.- Specified by:
quitin interfaceCoreDriver- Specified by:
quitin interfaceDriver
-
isTerminated
public boolean isTerminated()Description copied from interface:DriverCheck if driver is terminated.- Specified by:
isTerminatedin interfaceCoreDriver- Specified by:
isTerminatedin interfaceDriver
-
getOptions
Description copied from interface:DriverGet driver options.- Specified by:
getOptionsin interfaceCoreDriver- Specified by:
getOptionsin interfaceDriver
-