Module: Watigiri::Locators::LocatorHelpers

Included in:
Button::Locator, Cell::Locator, Element::Locator, Row::Locator, TextArea::Locator, TextField::Locator
Defined in:
lib/watigiri/locators/element/locator.rb

Instance Method Summary collapse

Instance Method Details

#fetch_value(element, how) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/watigiri/locators/element/locator.rb', line 57

def fetch_value(element, how)
  noko_element = noko_element(element)
  return super if noko_element.nil?

  case how
  when :text
    noko_element.inner_text
  when :tag_name
    noko_element.name.downcase
  else
    noko_element.attribute(how.to_s.tr('_', '-')).to_s.strip
  end
end

#locateObject



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/watigiri/locators/element/locator.rb', line 17

def locate
  @nokogiri = @selector.delete(:nokogiri)
  return super unless @nokogiri || regex?

  set_nokogiri

  element = using_watir(:first)
  return if element.nil?
  @nokogiri ? element.element : nokogiri_to_selenium(element)
rescue Selenium::WebDriver::Error::NoSuchElementError, Selenium::WebDriver::Error::StaleElementReferenceError
  nil
end

#locate_allObject



30
31
32
33
34
35
36
37
38
# File 'lib/watigiri/locators/element/locator.rb', line 30

def locate_all
  @nokogiri = @selector.delete(:nokogiri)
  return super unless @nokogiri || regex?

  set_nokogiri

  elements = using_watir(:all)
  @nokogiri ? elements.map(&:element) : elements.map { |el| nokogiri_to_selenium(el) }
end

#locate_element(how, what, _driver_scope = @query_scope.wd) ⇒ Object

Is only used when there is no regex, index or visibility locators



41
42
43
44
45
46
# File 'lib/watigiri/locators/element/locator.rb', line 41

def locate_element(how, what, _driver_scope = @query_scope.wd)
  return super unless @nokogiri

  el = @query_scope.doc.send("at_#{how}", what)
  Watigiri::Element.new element: el, selector: {how => what}
end

#locate_elements(how, what, _scope = @query_scope.wd) ⇒ Object

“how” can only be :css or :xpath



49
50
51
52
53
54
55
# File 'lib/watigiri/locators/element/locator.rb', line 49

def locate_elements(how, what, _scope = @query_scope.wd)
  return super unless @nokogiri || regex?

  @query_scope.doc.send(how, what).map do |el|
    Watigiri::Element.new element: el, selector: {how => what}
  end
end

#noko_element(element) ⇒ Object



85
86
87
88
89
90
91
92
93
# File 'lib/watigiri/locators/element/locator.rb', line 85

def noko_element(element)
  if !(@nokogiri || regex?) || element.is_a?(Selenium::WebDriver::Element)
    nil
  elsif element.is_a?(Watigiri::Element)
    element.element
  else
    element
  end
end

#nokogiri_to_selenium(element) ⇒ Object



71
72
73
74
75
76
# File 'lib/watigiri/locators/element/locator.rb', line 71

def nokogiri_to_selenium(element)
  return element if element.is_a?(Selenium::WebDriver::Element)
  tag = element.tag_name
  index = @query_scope.doc.xpath(".//#{tag}").find_index { |el| el == element.element }
  Watir::Element.new(@query_scope, index: index, tag_name: tag).wd
end

#regex?Boolean

Returns:

  • (Boolean)


78
79
80
81
82
83
# File 'lib/watigiri/locators/element/locator.rb', line 78

def regex?
  return @regex unless @regex.nil?

  return false unless (@selector.keys & i[adjacent visible label text visible_text visible_label]).empty?
  @regex = @selector.values.any? { |v| v.is_a?(Regexp) }
end

#set_nokogiriObject



95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/watigiri/locators/element/locator.rb', line 95

def set_nokogiri
  return if @query_scope.doc

  # should be using `#fragment` instead of `#inner_html`, but can't because of
  # https://github.com/sparklemotion/nokogiri/issues/572
  doc = if @query_scope.is_a?(Watir::Browser)
          Nokogiri::HTML(@query_scope.html)
        else
          Nokogiri::HTML(@query_scope.inner_html)
        end

  @query_scope.doc = doc.tap { |d| d.css('script').remove }
end

#text_regexp_deprecationObject



109
110
111
# File 'lib/watigiri/locators/element/locator.rb', line 109

def text_regexp_deprecation(*)
  # Nokogiri can not determine visible text so no need to check
end