Class: Watir::Locators::Element::Locator

Inherits:
Object
  • Object
show all
Defined in:
lib/watir-webdriver/locators/element/locator.rb

Direct Known Subclasses

TextArea::Locator, TextField::Locator

Constant Summary collapse

WD_FINDERS =
[
  :class,
  :class_name,
  :css,
  :id,
  :link,
  :link_text,
  :name,
  :partial_link_text,
  :tag_name,
  :xpath
]
CONVERTABLE_REGEXP =

Regular expressions that can be reliably converted to xpath ‘contains` expressions in order to optimize the .

%r{
  \A
    ([^\[\]\\^$.|?*+()]*) # leading literal characters
    [^|]*?                # do not try to convert expressions with alternates
    ([^\[\]\\^$.|?*+()]*) # trailing literal characters
  \z
}x

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parent, selector, selector_builder, element_validator) ⇒ Locator

Returns a new instance of Locator.



31
32
33
34
35
36
# File 'lib/watir-webdriver/locators/element/locator.rb', line 31

def initialize(parent, selector, selector_builder, element_validator)
  @parent = parent # either element or browser
  @selector = selector.dup
  @selector_builder = selector_builder
  @element_validator = element_validator
end

Instance Attribute Details

#element_validatorObject (readonly)

Returns the value of attribute element_validator.



6
7
8
# File 'lib/watir-webdriver/locators/element/locator.rb', line 6

def element_validator
  @element_validator
end

#selector_builderObject (readonly)

Returns the value of attribute selector_builder.



5
6
7
# File 'lib/watir-webdriver/locators/element/locator.rb', line 5

def selector_builder
  @selector_builder
end

Instance Method Details

#locateObject



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/watir-webdriver/locators/element/locator.rb', line 38

def locate
  e = by_id and return e # short-circuit if :id is given

  if @selector.size == 1
    element = find_first_by_one
  else
    element = find_first_by_multiple
  end

  # This actually only applies when finding by xpath/css - browser.text_field(:xpath, "//input[@type='radio']")
  # We don't need to validate the element if we built the xpath ourselves.
  # It is also used to alter behavior of methods locating more than one type of element
  # (e.g. text_field locates both input and textarea)
  element_validator.validate(element, @selector) if element
rescue Selenium::WebDriver::Error::NoSuchElementError, Selenium::WebDriver::Error::StaleElementReferenceError
  nil
end

#locate_allObject



56
57
58
59
60
61
62
# File 'lib/watir-webdriver/locators/element/locator.rb', line 56

def locate_all
  if @selector.size == 1
    find_all_by_one
  else
    find_all_by_multiple
  end
end