Module: HtmlSelectorsHelpers

Defined in:
lib/SimpliTest/helpers/step_helpers/html_selectors_helpers.rb

Instance Method Summary collapse

Instance Method Details

#default_selector_for(locator) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/SimpliTest/helpers/step_helpers/html_selectors_helpers.rb', line 102

def default_selector_for(locator)
  case locator

  when "the page"
    with_selector_type "html > body"
  when "table's header"
    with_selector_type "table tbody > tr th"
  when /^paragraphs?$/
    with_selector_type  "p"
  when "\"table tr\""
    with_selector_type "table tr"
  when "\"div.some_class\""
    with_selector_type "div.some_class"
  else
    with_selector_type locator
  end
end


46
47
48
49
50
51
52
53
54
55
56
# File 'lib/SimpliTest/helpers/step_helpers/html_selectors_helpers.rb', line 46

def get_button_or_link_from(locator)
  begin
    get_element_from(locator, false, true)
  rescue
    begin
      page.find_link(locator)
    rescue
      page.find_button(locator)
    end
  end
end

#get_element_from(locator, try_field = true, find_button_or_link = false) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/SimpliTest/helpers/step_helpers/html_selectors_helpers.rb', line 27

def get_element_from(locator, try_field=true, find_button_or_link=false)
  selector, selector_type = selector_for(locator)
  element = nil
  patiently do
    if selector_type == 'xpath'
      element = page.find(:xpath, selector)
    elsif selector_type == 'css'
      element = page.find(:css, selector)
    elsif try_field
      element = page.find_field(selector)
    elsif find_button_or_link
      raise "Button or Link Not Found"
    else
      element = find(selector)
    end
  end
end

#is_css?(locator) ⇒ Boolean

Returns:

  • (Boolean)


82
83
84
# File 'lib/SimpliTest/helpers/step_helpers/html_selectors_helpers.rb', line 82

def is_css?(locator)
  selector_for(locator)[0].match(/css:(.*)/)
end

#is_xpath?(locator) ⇒ Boolean

Returns:

  • (Boolean)


78
79
80
# File 'lib/SimpliTest/helpers/step_helpers/html_selectors_helpers.rb', line 78

def is_xpath?(locator)
  selector_for(locator)[0].match(/xpath:(.*)/)
end

#mapping_for(selector) ⇒ Object



67
68
69
# File 'lib/SimpliTest/helpers/step_helpers/html_selectors_helpers.rb', line 67

def mapping_for(selector)
  SimpliTest.config_selectors[unquoted(selector)] || false
end

#pagination_element_for(index) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/SimpliTest/helpers/step_helpers/html_selectors_helpers.rb', line 10

def pagination_element_for(index)
  lookup = {
    'first' => 'firstButton',
    'last' => 'lastButton',
    'previous' => 'prevButton',
    'next' => 'nextButton'
  }
  selector = "##{lookup[index]}"
  elements = all(:css, selector)
  elements.first if elements
end


5
6
7
8
# File 'lib/SimpliTest/helpers/step_helpers/html_selectors_helpers.rb', line 5

def pagination_links_for(page_number)
  selector = "input[title='Page #{page_number}']"
  all(:css, selector)
end

#select_options_for(locator) ⇒ Object



22
23
24
25
# File 'lib/SimpliTest/helpers/step_helpers/html_selectors_helpers.rb', line 22

def select_options_for(locator)
  element = get_element_from(locator)
  get_text_from(element)
end

#selector_for(locator) ⇒ Object



58
59
60
61
62
63
64
65
# File 'lib/SimpliTest/helpers/step_helpers/html_selectors_helpers.rb', line 58

def selector_for(locator)
  if SimpliTest.config_selectors
    mapping = mapping_for(locator)
    mapping ? selector_from(mapping) : default_selector_for(locator)
  else 
    default_selector_for(locator)
  end
end

#selector_from(selector) ⇒ Object



71
72
73
74
75
# File 'lib/SimpliTest/helpers/step_helpers/html_selectors_helpers.rb', line 71

def selector_from(selector)
  selector_type = is_xpath?(selector) ? 'xpath' : (is_css?(selector) ? 'css' : 'other')
  selector = without_identifier(selector)
  return selector, selector_type
end

#selectors_from_section(section) ⇒ Object



139
140
141
# File 'lib/SimpliTest/helpers/step_helpers/html_selectors_helpers.rb', line 139

def selectors_from_section(section)
  SimpliTest.config_selectors[section].values
end

#unquoted(string) ⇒ Object



95
96
97
98
99
100
# File 'lib/SimpliTest/helpers/step_helpers/html_selectors_helpers.rb', line 95

def unquoted(string)
  #string[0] is the first char

  #string[-1,1] is the last char

  is_string_quoted = (string[0] == string[-1, 1]) && (%w[' "].include?(string[0]))
  is_string_quoted ? string.gsub(/^"|"$/, '') : string
end

#validate_absence_of(text) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/SimpliTest/helpers/step_helpers/html_selectors_helpers.rb', line 124

def validate_absence_of(text)
  min_wait_time = SimpliTest.config_settings ? SimpliTest.config_settings['MIN_WAIT_TIME'] : 2
  using_wait_time min_wait_time do #because we are validating absence we don't need to wait all 5 seconds?

    begin
      should_not have_link(text)
    rescue RSpec::Expectations::ExpectationNotMetError, Capybara::ExpectationNotMet
      element = get_button_or_link_from(text)
      element.should_not be_visible if element.respond_to?(:visible?)
      ## deprecating the approach below for performance reasons

      ##xpath = potential_hidden_paths_for(text).join('|')

      ##should have_xpath(xpath)

    end
  end
end

#with_selector_type(selector, selector_type = 'other') ⇒ Object



120
121
122
# File 'lib/SimpliTest/helpers/step_helpers/html_selectors_helpers.rb', line 120

def with_selector_type(selector, selector_type='other')
  [selector, selector_type]
end

#without_identifier(locator) ⇒ Object



86
87
88
89
90
91
92
# File 'lib/SimpliTest/helpers/step_helpers/html_selectors_helpers.rb', line 86

def without_identifier(locator)
  if match = (is_xpath?(locator) || is_css?(locator))
    return match[1]
  else
    return locator
  end
end