Skip to content

GenericModule::XPath

jfinkhaeuser edited this page Dec 17, 2014 · 1 revision

GenericModule::XPath

Generic modules expose functionality both in the cucumber World object and in the LapisLazuli browser.

The XPath module provides helper functions for easier XPath composition:

  • xp_contains(node, needle, [separator]) - creates an XPath clause finding an element by matching the given node against the given needle. The separator defaults to a space character.
  • If a separator is a space, then the XPath clause will match the needle if it's a separate word in the node value.
  • If the separator is an empty string, the XPath clause will match the needle anywhere in the node value.
  • xp_and(first, second) - combines two XPath clauses with a logical AND operator.
  • xp_or(first, second) - combines two XPath clauses with a logical OR operator.
  • xp_not(expr) - negates an XPath clause.

Example

# Matches any element whose text includes 'foo' as a space separated word
:xpath => "//[#{xp_contains('text()', 'foo')}]"

# Matches any element with the given class name
:xpath => "//[#{xp_contains('@class', 'bar')}]"

# A combination, with operators:
text_is_foo = xp_contains('text()', 'foo')
class_has_bar = xp_contains('@class', 'bar')
id_contains_baz = xp_contains('@id', 'baz', '')

combined = xp_and(
  text_is_foo,
  xp_or(
    class_has_bar,
    id_contains_baz
  )
)
:xpath => "//#{combined}"