Class: SeleniumSurfer::SurfContext

Inherits:
SearchContext show all
Defined in:
lib/selenium_surfer/surf_context.rb

Overview

### Base class for robot contexts

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from SearchContext

#explode, #fill, #method_missing, #respond_to?, #search

Constructor Details

#initialize(_bucket, _macro = nil, _stack = nil) ⇒ SurfContext

Returns a new instance of SurfContext.



38
39
40
41
42
43
44
# File 'lib/selenium_surfer/surf_context.rb', line 38

def initialize(_bucket, _macro=nil, _stack=nil)
  @bucket = _bucket
  @macro = _macro || {}
  @stack = _stack || []

  @bucket.bind self
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class SeleniumSurfer::SearchContext

Class Method Details

.macro_attr_accessor(*_names) ⇒ Object

add a macro attribute accessor to context.

A macro attribute persist through context changes.



31
32
33
34
# File 'lib/selenium_surfer/surf_context.rb', line 31

def self.macro_attr_accessor(*_names)
  macro_attr_reader *_names
  macro_attr_writer *_names
end

.macro_attr_reader(*_names) ⇒ Object

add a macro attribute reader to context.

A macro attribute persist through context changes.



21
22
23
24
25
# File 'lib/selenium_surfer/surf_context.rb', line 21

def self.macro_attr_reader(*_names)
  _names.each do |name|
    send :define_method, "#{name}" do @macro[name.to_sym] end
  end
end

.macro_attr_writer(*_names) ⇒ Object

add a macro attribute writer to context.

A macro attribute persist through context changes.



11
12
13
14
15
# File 'lib/selenium_surfer/surf_context.rb', line 11

def self.macro_attr_writer(*_names)
  _names.each do |name|
    send :define_method, "#{name}=" do |v| @macro[name.to_sym] = v end
  end
end

Instance Method Details

#bound?Boolean

return true if context is bound

Returns:

  • (Boolean)


47
48
49
# File 'lib/selenium_surfer/surf_context.rb', line 47

def bound?
  not @bucket.nil?
end

#driverObject

retrieves the current driver being used by this context



61
62
63
# File 'lib/selenium_surfer/surf_context.rb', line 61

def driver
  load_driver
end

navigate to a given url (uses the max_retries setting)



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/selenium_surfer/surf_context.rb', line 71

def navigate(_url, _params=nil)
  _url += "?#{_params.to_query}" if _params
  retries = 0

  loop do
    begin
      load_driver(retries > 0).get(_url)
      @stack = [] # clear stack after successfull navigation
      break
    rescue Timeout::Error, Selenium::WebDriver::Error::UnknownError
      trace "Error when opening #{_url}!"
      raise if retries >= @max_retries
      retries += 1
      sleep 1.0
    end
  end
end

#on_unbindObject

bucket context interface implementation not to be called directly



127
128
129
# File 'lib/selenium_surfer/surf_context.rb', line 127

def on_unbind
  @bucket = @stack = nil
end

#quitObject

release and discard the current driver connection.



112
113
114
115
116
# File 'lib/selenium_surfer/surf_context.rb', line 112

def quit
  return false if not bound?
  @bucket.unbind true
  return true
end

#releaseObject

release current driver connection



105
106
107
108
109
# File 'lib/selenium_surfer/surf_context.rb', line 105

def release
  return false if not bound?
  @bucket.unbind
  return true
end

#resetObject

resets the current driver connection, does not release it.



119
120
121
122
123
# File 'lib/selenium_surfer/surf_context.rb', line 119

def reset
  return false if not bound?
  @bucket.reset
  return true
end

#step(_selector = nil, _options = {}) ⇒ Object

changes the context TODO: this method may be unecesary…



91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/selenium_surfer/surf_context.rb', line 91

def step(_selector=nil, _options={})
  _options[:css] = _selector if _selector
  new_context = search_elements(_options)
  begin
    @stack << new_context
    yield
  ensure
    @stack.pop
  end

  return true
end

#switch_to(_klass = nil) ⇒ Object

switch to another context new context class should be a SurfContext subclass



53
54
55
56
# File 'lib/selenium_surfer/surf_context.rb', line 53

def switch_to(_klass=nil)
  raise UnboundContextError.new unless bound?
  _klass.new @bucket, @macro, @stack
end

#titleObject

return the current page title



66
67
68
# File 'lib/selenium_surfer/surf_context.rb', line 66

def title
  load_driver.title
end