Class Locators
Locator types:
- CSS selector: "#id", ".class", "tag", "tag.class"
- XPath: "//div", "./span", "../parent", "(//div)[2]"
- Wildcard: "{div}text", "{^div}partial", "{tag:2}text", "{:2}text"
- Pure JS: "(expression)" - passed through unchanged
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic StringattributeJs(String locator, String name) Generate JS to get an attribute of an element.static StringGenerate JS to clear an input element.static StringGenerate JS to click an element.static StringGenerate a pure-JS locator that resolves to the closest ancestor (or self) matching the given CSS selector, ornullif no ancestor matches.static StringThe full event sequence a real<select>change produces, fired on the element expressione.static StringGenerate JS to count matching elements.static StringGenerate JS to check if element is enabled.static StringescapeForJs(String text) Escape a string for embedding in a JavaScript double-quoted string.static StringescapeXpathString(String text) Escape a string for use in XPath.static StringGenerate JS to check if element exists.static StringexpandWildcard(String locator) Expand wildcard locator to JavaScript resolver call.static StringGenerate JS to find all matching elements and return their unique selectors.static StringGenerate JS to focus an element with cursor at end.static StringgetPositionJs(String locator) Generate JS to get element position.static StringGenerate JS to highlight an element.static StringhighlightAll(String locator, int millis) Generate JS to highlight all elements matching a locator.static StringinnerHtmlJs(String locator) Generate JS to get inner HTML of an element.static StringGenerate JS to set the value of an input element with events.static booleanCheck if a locator is an XPath expression.static StringGenerate a JS expression that evaluates totruewhen the element resolved by the given locator matches the CSS selector, orfalseotherwise (including when the element does not exist).static StringoptionSelector(String locator, String text) Generate JS to select a dropdown option.static StringouterHtmlJs(String locator) Generate JS to get outer HTML of an element.static StringpropertyJs(String locator, String name) Generate JS to get a property of an element.static StringscopedCountJs(String baseJsLocator, String cssChildSelector) Generate a JS expression that counts descendants of the given pure-JS base locator matching the child CSS selector.static StringscopedIndexedSelectorJs(String baseJsLocator, String cssChildSelector, int index) Generate a pure-JS locator that evaluates to the nth descendant of the given pure-JS base locator matching the child CSS selector.static StringscopedSelectorJs(String baseJsLocator, String cssChildSelector) Generate a pure-JS locator that evaluates to the first descendant of the given pure-JS base locator matching the child CSS selector, ornullif no match.static StringscriptAllSelector(String locator, Object expression) Execute a function on all elements matching locator.static StringscriptAllSelector(String locator, Object expression, String contextNode) Execute a function on all elements matching locator with context.static StringscriptSelector(String locator, Object expression) Execute a function on a single element found by locator.static StringscriptSelector(String locator, Object expression, String contextNode) Execute a function on a single element found by locator with context.static StringGenerate JS to scroll an element into view.static StringTransform a locator into a JavaScript expression that returns a single element.static StringTransform a locator into a JavaScript expression with a context node.static StringselectorAll(String locator) Transform a locator into a JavaScript expression that returns all matching elements.static StringselectorAll(String locator, String contextNode) Transform a locator into a JavaScript expression that returns all matching elements.static StringGenerate JS to get text content of an element.static StringtoFunction(Object expression) Convert shorthand expression to a function.static StringGenerate JS to get the value of an input element.static StringWrap JavaScript in an IIFE (Immediately Invoked Function Expression).
-
Field Details
-
SCROLL_JS_FUNCTION
- See Also:
-
ELEMENT_NOT_FOUND
Marker in the page-sideErrorthrown by an action whose locator resolved to null.CdpDrivermatches on this to tell a transient re-resolve miss (the document swapped under us — retryable) apart from a genuine JS error in the page under test.- See Also:
-
-
Constructor Details
-
Locators
public Locators()
-
-
Method Details
-
selector
Transform a locator into a JavaScript expression that returns a single element.- Parameters:
locator- CSS selector, XPath, wildcard, or JS expression- Returns:
- JavaScript expression
-
selector
Transform a locator into a JavaScript expression with a context node.- Parameters:
locator- CSS selector, XPath, wildcard, or JS expressioncontextNode- JavaScript expression for the context node- Returns:
- JavaScript expression
-
isXpath
Check if a locator is an XPath expression. -
expandWildcard
Expand wildcard locator to JavaScript resolver call. Uses browser-side JS that matches the same logic as locator generation.- {tag}text → window.__kjs.resolve('tag', 'text', 1, false)
- {^tag}text → window.__kjs.resolve('tag', 'text', 1, true)
- {tag:2}text → window.__kjs.resolve('tag', 'text', 2, false)
- {:2}text → window.__kjs.resolve('*', 'text', 2, false)
-
selectorAll
Transform a locator into a JavaScript expression that returns all matching elements. -
selectorAll
Transform a locator into a JavaScript expression that returns all matching elements. -
wrapInFunctionInvoke
Wrap JavaScript in an IIFE (Immediately Invoked Function Expression). -
toFunction
Convert shorthand expression to a function.- JsFunction: arrow functions like
_ => _.valueare serialized to source - "_" expressions: "_.value" → _ => _.value
- "!" expressions: "!_.disabled" → _ => !_.disabled
- JsFunction: arrow functions like
-
scriptSelector
Execute a function on a single element found by locator. Expression can be a String or JsFunction (arrow function). -
scriptSelector
Execute a function on a single element found by locator with context. Expression can be a String or JsFunction (arrow function). -
scriptAllSelector
Execute a function on all elements matching locator. Expression can be a String or JsFunction (arrow function). -
scriptAllSelector
Execute a function on all elements matching locator with context. Expression can be a String or JsFunction (arrow function). -
highlight
Generate JS to highlight an element. -
highlightAll
Generate JS to highlight all elements matching a locator. -
commitFieldEventsJs
The full event sequence a real<select>change produces, fired on the element expressione.input+changeupdate the value;blur+focusouttrigger a framework's field-level commit/validation — so a form that only commits (or clears a "required" error) on blur (Guidewire, SAP and many enterprise UIs) commits at select time, instead of lazily on the next interaction. Without the blur pair, a programmatic select sets the value but leaves the framework's field state stale, which reads as "the select didn't stick". (Native semantics:blurdoes not bubble;focusoutdoes — fire both so direct and delegated listeners run.) -
optionSelector
Generate JS to select a dropdown option. Dispatches the full real-selection event sequence (input, change, blur, focusout) so a framework that commits or validates a field on blur commits at select time (seecommitFieldEventsJs(java.lang.String)).- "{}" prefix: select by exact text only
- "{^}" prefix: select by text contains
- otherwise: try value first, then fall back to text match
-
getPositionJs
Generate JS to get element position. -
focusJs
Generate JS to focus an element with cursor at end. -
clickJs
Generate JS to click an element. -
scrollJs
Generate JS to scroll an element into view. -
inputJs
Generate JS to set the value of an input element with events. -
clearJs
Generate JS to clear an input element. -
textJs
Generate JS to get text content of an element. -
valueJs
Generate JS to get the value of an input element. -
attributeJs
Generate JS to get an attribute of an element. -
propertyJs
Generate JS to get a property of an element. -
closestJs
Generate a pure-JS locator that resolves to the closest ancestor (or self) matching the given CSS selector, ornullif no ancestor matches.Mirrors the native
Element.closest()DOM API. Result starts with(so it passes throughselector(String)unchanged and can be used anywhere a locator string is accepted (element ops, waits, script). -
matchesJs
Generate a JS expression that evaluates totruewhen the element resolved by the given locator matches the CSS selector, orfalseotherwise (including when the element does not exist).Mirrors the native
Element.matches()DOM API. UnlikeclosestJs(java.lang.String, java.lang.String), this returns a boolean, so it is a full JS expression meant fordriver.script()rather than a locator string. -
scopedSelectorJs
Generate a pure-JS locator that evaluates to the first descendant of the given pure-JS base locator matching the child CSS selector, ornullif no match.Used by
BaseElement.locate(String)when the base element's locator is itself a JS expression (e.g. produced byclosestJs(java.lang.String, java.lang.String)) — naive string-concat would produce invalid JS. -
scopedCountJs
Generate a JS expression that counts descendants of the given pure-JS base locator matching the child CSS selector. Returns 0 when the base is missing. -
scopedIndexedSelectorJs
public static String scopedIndexedSelectorJs(String baseJsLocator, String cssChildSelector, int index) Generate a pure-JS locator that evaluates to the nth descendant of the given pure-JS base locator matching the child CSS selector. Index is zero-based.Re-resolves the base on every access, so the resulting locator tolerates DOM mutations between the count and later element ops — matching how CDP driver
createIndexedLocatorbehaves for CSS/XPath locators. -
enabledJs
Generate JS to check if element is enabled. -
existsJs
Generate JS to check if element exists. -
outerHtmlJs
Generate JS to get outer HTML of an element. -
innerHtmlJs
Generate JS to get inner HTML of an element. -
findAllJs
Generate JS to find all matching elements and return their unique selectors. Returns an array of CSS selectors that can be used to re-locate each element. -
countJs
Generate JS to count matching elements. -
escapeForJs
Escape a string for embedding in a JavaScript double-quoted string. -
escapeXpathString
Escape a string for use in XPath. Handles strings containing single quotes, double quotes, or both.
-