Path¶
-
public interface
Path
¶ The heart of DollarX is the definition of Paths that represent W3C elements, whether in the browser or in a document. Path describes an element, or elements, using API that emulates regular description in English, and can represent almost any element that can be expressed as the XPATH. Path is immutable - any additional constraint creates a new Path instance and does not change the original one. It is important to remember that defining new Paths is very cheap, since it does not interact with the browser. For example, in selenium, if we want to define a WebElement inside another WebElement, it must involve interaction with the browser (which involves potential race conditions):
WebElement wrapper = driver.findElement(By.cssSelector("div.foo")); WebElement field = wrapper.findElement(By.cssSelector(".bar"));
In DollarX it will look like:
final Path wrapper = element.withClass("foo"); final Path field = element.withClass("bad").inside(wrapper);
Several points to note:
- Once defined, Path values are final and can be reused without cost, as opposed to functions.
- Creating arbitrarily complex Path is easy this way. It is far more maintainable than using explicit xpath.
- Creating Paths has nothing to do with the browser.
- The API offers many alternative ways to define equivalent Paths. The guideline is: If it means the same in English, it is mapped to an equivalent Path. For example, the following are equivalent:
element.that(hasClass("condition").and(isInside(dialog))); element.that(hasClass("condition")).and(isInside(dialog)); element.inside(dialog).that(hasClass("condition")); element.withClass("condition").and(isInside(dialog)); element.withClass("condition").and(isContainedIn(dialog)); element.that(hasAncesctor(dialog)).and(hasClass("condition")); // if you prefer to break the definition to two steps: Path condition = element.withClass("condition"); condition.inside(dialog);
- Path::toString returns a string that reads like english and expresses exactly the definition of the path. This is very useful when troubleshooting.
- To check what is the xpath a Path is mapped to, call
path.getXpath().get()
- Since it can be used as a wrapper of Selenium WebElement, you are not tied to using only DollarX - but can use it interchangeably with straight Selenium WebDriver.
The pattern in DollarX is to define exactly what you want to interact with or assert, as a Path, and then interact with the browser. This maximizes atomicity and performance, and avoid many of the pitfalls involved with interactions with a dynamic SPA. The standard implementation for Path is
BasicPath
.
Methods¶
after¶
afterSibling¶
ancestorOf¶
and¶
-
Path
and
(ElementProperty... prop)¶ Alias equivalent to
that
. Added for readability. Example:div.that(hasClass("a")).and(hasText("foo"));
Parameters: - prop – a list of element properties (constraints)
Returns: a new Path
before¶
beforeSibling¶
childOf¶
containing¶
contains¶
descendantOf¶
describedBy¶
-
Path
describedBy
(String description)¶ A useful method to give a readable description to the path, for example: Suppose that instead of describing it’s DOM positions and attributes, you prefer to describe it as “search result”. Then you’d call: searchResult = myElement.describedBy(“search result”); Now, calling System.out.println(firstOccurrenceOf(searchResult)), will print: “first occurrence of search result” This will replace its toString() result.
Parameters: - description – a readable description to that expresses the functionality of the path
Returns: a new Path similar to the old one but that is described by the given description
getAlternateXPath¶
getDescribedBy¶
getElementProperties¶
-
List<ElementProperty>
getElementProperties
()¶ Returns: Should not be typically used, unless you are developing for DollarX
getUnderlyingSource¶
getXPath¶
-
Optional<String>
getXPath
()¶ The Optional xpath is maps to. Note that the prefix that marks it is inside the document (for example; “//” as the prefix of the xpath) can be omitted. This is not a concern - it will be added automatically by DollarX when interacting with the browser.
Returns: an optional containing the xpath this Path is mapped to. The optional is empty only in case it is used as a wrapper of a WebElement (not the typical case).
immediatelyAfterSibling¶
immediatelyBeforeSibling¶
inside¶
insideTopLevel¶
or¶
parentOf¶
that¶
-
Path
that
(ElementProperty... prop)¶ returns a path with the provided properties. For example: div.that(hasText(“abc”), hasClass(“foo”));
Parameters: - prop –
- one or more properties. See ElementProperties documentation for details
Returns: a new path with the added constraints
- prop –
withClass¶
withClasses¶
withGlobalIndex¶
-
Path
withGlobalIndex
(Integer index)¶ Return the nth occurrence of the element in the entire document. Count starts at 1. The following expressions are equivalent: occurrenceNumber(4).of(myElement)); myElement.withGlobalIndex(4); Return the nth occurrence of the element in the entire document. Count starts at 1. For example: occurrenceNumber(3).of(listItem)
Parameters: - index – the number of occurrence
Returns: a new Path with the added constraint