Class: SeleniumSurfer::SurfContext

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

Overview

### Base class for robot contexts

Constant Summary

Constants inherited from SearchContext

SeleniumSurfer::SearchContext::TIMEOUT

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from SearchContext

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

Constructor Details

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

Returns a new instance of SurfContext.



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

def initialize(_bucket, _macro=nil, _stack=nil)
  super nil, nil

  @bucket = _bucket
  @macro = _macro || { max_retries: 5 }
  @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.



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

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.



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

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.



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

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)


50
51
52
# File 'lib/selenium_surfer/surf_context.rb', line 50

def bound?
  not @bucket.nil?
end

#cookiesObject

return the current page cookies as a hash



80
81
82
# File 'lib/selenium_surfer/surf_context.rb', line 80

def cookies
  driver.manage.all_cookies
end

#current_uriObject

return the current page url as an URI



75
76
77
# File 'lib/selenium_surfer/surf_context.rb', line 75

def current_uri
  URI.parse driver.current_url
end

#driver(_reset = false) ⇒ Object

retrieves the current driver being used by this context



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

def driver(_reset=false)
  raise UnboundContextError.new if not bound?
  @bucket.reset if _reset
  @bucket.driver
end

navigate to a given url (uses the max_retries setting)



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/selenium_surfer/surf_context.rb', line 85

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

  loop do
    begin
      driver(retries > 0).get(_url)
      @stack = [] # clear stack after successfull navigation
      break
    rescue Timeout::Error #, Selenium::WebDriver::Error::UnknownError
      # TODO: log this
      raise if retries >= max_retries
      retries += 1
      sleep 1.0
    end
  end
end

#on_unbindObject

bucket context interface implementation not to be called directly



135
136
137
# File 'lib/selenium_surfer/surf_context.rb', line 135

def on_unbind
  @bucket = @stack = nil
end

#quitObject

release and discard the current driver connection.



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

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

#releaseObject

release current driver connection



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

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

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

changes the context TODO: this method may be unnecesary…



105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/selenium_surfer/surf_context.rb', line 105

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



56
57
58
59
# File 'lib/selenium_surfer/surf_context.rb', line 56

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