class SeleniumSurfer::SurfContext
Base class for robot contexts
Public Class Methods
macro_attr_accessor(*_names)
click to toggle source
add a macro attribute accessor to context.
A macro attribute persist through context changes.
# 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)
click to toggle source
add a macro attribute reader to context.
A macro attribute persist through context changes.
# 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)
click to toggle source
add a macro attribute writer to context.
A macro attribute persist through context changes.
# 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
new(_bucket, _macro=nil, _stack=nil)
click to toggle source
Calls superclass method
SeleniumSurfer::SearchContext::new
# 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
Public Instance Methods
bound?()
click to toggle source
return true if context is bound
# File lib/selenium_surfer/surf_context.rb, line 50 def bound? not @bucket.nil? end
current_uri()
click to toggle source
return the current page url as an URI
# File lib/selenium_surfer/surf_context.rb, line 75 def current_uri URI.parse driver.current_url end
driver(_reset=false)
click to toggle source
retrieves the current driver being used by this context
# 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
on_unbind()
click to toggle source
bucket context interface implementation not to be called directly
# File lib/selenium_surfer/surf_context.rb, line 135 def on_unbind @bucket = @stack = nil end
quit()
click to toggle source
release and discard the current driver connection.
# File lib/selenium_surfer/surf_context.rb, line 126 def quit return false if not bound? @bucket.reset @bucket.unbind return true end
release()
click to toggle source
release current driver connection
# 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={}) { || ... }
click to toggle source
changes the context TODO: this method may be unnecesary…
# 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)
click to toggle source
switch to another context new context class should be a SurfContext
subclass
# 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
Private Instance Methods
context()
click to toggle source
# File lib/selenium_surfer/surf_context.rb, line 141 def context raise UnboundContextError.new if not bound? @stack.last || [driver] end
observe() { || ... }
click to toggle source
# File lib/selenium_surfer/surf_context.rb, line 146 def observe # get current url return yield # compare url after function call, if changed reset stack end