Package io.karatelabs.driver
Class Locators
java.lang.Object
io.karatelabs.driver.Locators
Utility class for transforming locators into JavaScript expressions.
Supports CSS selectors, XPath, and wildcard 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 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 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 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 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:
-
-
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. -
optionSelector
Generate JS to select a dropdown option. Dispatches both 'input' and 'change' events with bubbles:true for framework compatibility.- "{}" 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. -
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.
-