Module: SK::Browser

Extended by:
Browser
Included in:
Browser
Defined in:
lib/browser.rb

Instance Method Summary collapse

Instance Method Details

#_click_el(el, extra) ⇒ Object



216
217
218
219
220
221
# File 'lib/browser.rb', line 216

def _click_el(el,extra)
  SK::Trace.trace("*** click_el")
  rescue_exceptions { el.click }
  sleep 1+extra
  self
end

#_dropdown_el(select_box, tagname, list_option) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/browser.rb', line 58

def _dropdown_el(select_box,tagname,list_option)
  # trace "dropdown el... select_box=#{select_box}"
  # trace "dropdown el... tagname=#{tagname} option=#{list_option}"

  # actually returns the option object for the dropdown
  # so the calling code can chain a select by method
  # option(find({name: name}))
  options = select_box.find_elements(:tag_name => "#{tagname}")

  options.each do |option_field|
    # trace "looking at #{option_field.text}"
    if option_field.text == "#{list_option}"
      option_field.click
      break
    end
  end
end

#_find_inside(locator, base) ⇒ Object



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/browser.rb', line 136

def _find_inside(locator, base)
  # trace "find inside. locator=#{locator} base=#{base}"
  # find an element by locator inside another element (base)
  case
  when locator.has_key?(:value)
    # trace "... locate by value: #{locator[:value]}}"
    find_input(:value,locator[:value]) 
  when locator.has_key?(:type)
    # trace "... locate by type: #{locator[:type]}" 
    find_input(:type,locator[:type]) 
  else
    # a standard selenium locator so just use it
    rescue_exceptions { base.find_element(locator) } 
  end
end

#_get_attribute(el, attr) ⇒ Object



94
95
96
# File 'lib/browser.rb', line 94

def _get_attribute(el,attr)
  attr == 'text' ? el.text : el.attribute(attr)
end

#_set_el(el, value) ⇒ Object



110
111
112
113
# File 'lib/browser.rb', line 110

def _set_el(el,value)
  el.clear
  el.send_keys(value)
end

#all(locator) ⇒ Object



162
163
164
# File 'lib/browser.rb', line 162

def all(locator)
  rescue_exceptions { driver.find_elements(locator) }
end

#click(locator, pause = 0) ⇒ Object

Actions ####



210
211
212
213
214
# File 'lib/browser.rb', line 210

def click(locator,pause=0)
  SK::Trace.trace("*** click is depricated")
  el = find(locator)
  _click_el(el,pause) if el
end

#count_refs(ref, locator) ⇒ Object



172
173
174
175
176
# File 'lib/browser.rb', line 172

def count_refs(ref, locator)
  elements = all(locator)
  # trace("count from #{elements.length} links...")
  elements.count{ |e| e.attribute('href').include? ref }    
end

#driverObject

Simple Accessors ####



253
254
255
# File 'lib/browser.rb', line 253

def driver
  SK::driver
end


49
50
51
52
53
54
55
56
57
# File 'lib/browser.rb', line 49

def dropdown(locator,tagname,list_option)
    warn "browser.dropdown is depricated. use SK::Dropdown instead. locator=#{locator}"
    # actually returns the option object for the dropdown
    # so the calling code can chain a select by method
    # option(find({name: name}))
    select_box = find(locator)
    # select_box = $driver.find_element(locator)
    _dropdown_el(select_box,tagname,list_option)
end

#error(s) ⇒ Object



18
19
20
# File 'lib/browser.rb', line 18

def error(s)
  SK::Trace.error(s)
end

#find(param) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/browser.rb', line 120

def find(param)
  # find accepts a single value or array
  # of locators to find an element.  the array
  # is meant to be a this under this structure
  if param.kind_of?(Array)
    base = driver
    param.reverse.each do |loc|
      base = _find_inside(loc,base)
    end  
    return base     
  else
    # not an array, a single
    _find_inside(param,driver)
  end
end

#find_input(key, type) ⇒ Object



152
153
154
155
156
157
158
159
160
# File 'lib/browser.rb', line 152

def find_input(key,type)
  # selenium does not support a locator search by type
  # so we need a specialized function to find the element
  elements = all({tag_name: 'input'})
  elements.find do |e| 
    # trace "> #{e.attribute(key)} :: #{type}"
    e.attribute(key) == type
  end
end

#get_rows(locator, less_row) ⇒ Object



192
193
194
195
196
197
# File 'lib/browser.rb', line 192

def get_rows(locator,less_row)
  rest  = less_row.to_i
  #table = rescue_exceptions { driver.find_elements(table_count) }
  table = all(locator)
  return table.count-rest
end

#get_text(locator) ⇒ Object



199
200
201
202
# File 'lib/browser.rb', line 199

def get_text(locator)
  find(locator).text
  # return rescue_exceptions { driver.find_element(locator).text }
end

#get_value(locator) ⇒ Object



204
205
206
# File 'lib/browser.rb', line 204

def get_value(locator)
  find(locator).attribute('value')
end

#goto(site, page = '') ⇒ Object

main stuff #########



27
28
29
# File 'lib/browser.rb', line 27

def goto(site,page='')
  gotourl("https://#{site}/#{page}")
end

#gotourl(url, seconds = 1) ⇒ Object



33
34
35
36
37
38
# File 'lib/browser.rb', line 33

def gotourl(url,seconds=1)
  uri = URI.escape(url)
  trace("go to #{uri}")
  rescue_exceptions { SK::driver.get uri }
  sleep seconds
end

#gotox(site, page = '') ⇒ Object



30
31
32
# File 'lib/browser.rb', line 30

def gotox(site,page='')
  gotourl("http://#{site}/#{page}")
end

#has_content?(content) ⇒ Boolean

Returns:

  • (Boolean)


178
179
180
181
# File 'lib/browser.rb', line 178

def has_content?(content)
  # puts trace("has content? #{content}")
  return self.source.include? content
end

#has_el?(locator) ⇒ Boolean

Returns:

  • (Boolean)


183
184
185
186
# File 'lib/browser.rb', line 183

def has_el?(locator)
  find(locator)
  # rescue_exceptions { driver.find_element(locator) }
end

#has_text?(locator, text) ⇒ Boolean

Returns:

  • (Boolean)


188
189
190
# File 'lib/browser.rb', line 188

def has_text?(locator,text)
  find(locator).text == text
end

#match(text, locator) ⇒ Object



166
167
168
169
170
# File 'lib/browser.rb', line 166

def match(text,locator)
  elements = all(locator)
  # trace("searching #{elements.length} links...")
  elements.find{ |e| e.text.include? text }
end

#option(dd) ⇒ Object



40
41
42
43
# File 'lib/browser.rb', line 40

def option(dd)
   warn "browser.option is depricated. use SK::Dropdown instead"
   Selenium::WebDriver::Support::Select.new(dd)
end

#pause(secs, text = "") ⇒ Object

local utilities



11
12
13
14
# File 'lib/browser.rb', line 11

def pause(secs,text="")
  trace("pause #{secs} seconds #{text}")
  sleep secs
end

#radio(locator) ⇒ Object



98
99
100
101
# File 'lib/browser.rb', line 98

def radio(locator)
  warn "browser.radio is depricated. use SK::RadioSet instead. locator=#{locator}"
  self.click(locator,0)
end

#rescue_exceptionsObject

Key Core Operations ###



225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
# File 'lib/browser.rb', line 225

def rescue_exceptions
  # we do not want our tests to fail because selenium throws an error
  # instead we want to explicitly test using "expects" in the cases
  begin
    yield
  rescue Selenium::WebDriver::Error::NoSuchElementError
    warn "Element does not exist."
    false
  rescue Selenium::WebDriver::Error::StaleElementReferenceError
    warn "Element is stale."
    false
  rescue Selenium::WebDriver::Error::ElementNotVisibleError
    warn "Element is not visible."
    false
  rescue Selenium::WebDriver::Error::WebDriverError => e
    error "Webdriver Error. #{e}"
    false
  rescue Net::ReadTimeout
    error "Network Timeout - no response."
    false
  rescue IOError   
    error "IOError"
    false
  end
end

#search(base_locator, target_locator, value, attr = 'text') ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/browser.rb', line 76

def search(base_locator,target_locator,value,attr='text')
  # search within the locator element for all matching tags (by name)
  # return that element that has the attribute of text that matches
  # the text_value passed 
  element = find(base_locator)
  unless element # was found
    error "wt search: element not found. locator = #{base_locator}"
    return false
  end
  elements = element.find_elements(target_locator)
  elements.each do |el|
    el_attr = _get_attribute(el,attr)
    # trace "wt search at #{el_attr} for #{value}"
    return el if el_attr == "#{value}"          
  end
  return false
end

#select(locator, value) ⇒ Object



45
46
47
# File 'lib/browser.rb', line 45

def select(locator,value)
    dropdown(locator,"option",value)
end

#set(locator, value) ⇒ Object



103
104
105
106
107
108
109
# File 'lib/browser.rb', line 103

def set(locator,value)
  # set now accepts a single or an
  # array of locators, by using newfind
  el = find(locator)
  error "cannot set #{locator}" unless el
  _set_el(el,value) if el
end

#sourceObject



256
257
258
# File 'lib/browser.rb', line 256

def source
  driver.page_source
end

#submit(name, secs = 1) ⇒ Object



115
116
117
118
# File 'lib/browser.rb', line 115

def submit(name,secs=1)
  click(name: name)
  sleep secs
end

#titleObject



262
263
264
# File 'lib/browser.rb', line 262

def title
  driver.title
end

#trace(s) ⇒ Object



15
16
17
# File 'lib/browser.rb', line 15

def trace(s)
  SK::Trace.trace(s)
end

#urlObject



259
260
261
# File 'lib/browser.rb', line 259

def url
  driver.current_url
end

#warn(s) ⇒ Object



21
22
23
# File 'lib/browser.rb', line 21

def warn(s)
  SK::Trace.notice(s)
end