Class: SeleniumSurfer::DriverBucket

Inherits:
Object
  • Object
show all
Defined in:
lib/selenium_surfer/driver_bucket.rb

Overview

### Webdriver connection wrapper

By wrapping the connection is posible to control reconnection and bound context, this allows for safe context navigation.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(_session_id, _anonymous) ⇒ DriverBucket

Returns a new instance of DriverBucket.



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

def initialize(_session_id, _anonymous)
  @session_id = _session_id
  @bound_ctx = nil
  @anonymous = _anonymous
end

Instance Attribute Details

#session_idObject (readonly)

Returns the value of attribute session_id.



10
11
12
# File 'lib/selenium_surfer/driver_bucket.rb', line 10

def session_id
  @session_id
end

Instance Method Details

#bind(_ctx) ⇒ Object

bind a context to this bucket

The context may implement the ‘on_unbind` method to be notified when the bucket it is unbound from the bucket



63
64
65
66
# File 'lib/selenium_surfer/driver_bucket.rb', line 63

def bind(_ctx)
  @bound_ctx.on_unbind if @bound_ctx and @bound_ctx.respond_to? :on_unbind
  @bound_ctx = _ctx
end

#bound?Boolean

return true if there is a context bound to this bucket

Returns:

  • (Boolean)


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

def bound?
  not @bound_ctx.nil?
end

#driver(_reset = false) ⇒ Object

get the current driver instance, reset it if required



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/selenium_surfer/driver_bucket.rb', line 19

def driver(_reset=false)
  reset if _reset

  # TODO retrieve config data from config file instead of ENV

  if @driver.nil?
    driver_name = SeleniumSurfer.config[:webdriver]
    raise ConfigurationError.new 'must provide a webdriver type' if driver_name.nil?

    @driver = case driver_name.to_sym
    when 'remote'
      url = SeleniumSurfer.config[:remote_host]

      # setup a custom client to use longer timeouts
      client = Selenium::WebDriver::Remote::Http::Default.new
      client.timeout = SeleniumSurfer.config[:remote_timeout]

      @driver = Selenium::WebDriver.for :remote, :url => url, :http_client => client
    else
      @driver = Selenium::WebDriver.for driver_name.to_sym
    end
  end

  return @driver
end

#resetObject

force current driver connection to be discarded



46
47
48
49
50
51
# File 'lib/selenium_surfer/driver_bucket.rb', line 46

def reset
  if @driver
    @driver.quit rescue nil
    @driver = nil
  end
end

#unbind(_force_reset = false) ⇒ Object

unbinds the currently bound context.



69
70
71
72
73
74
75
# File 'lib/selenium_surfer/driver_bucket.rb', line 69

def unbind(_force_reset=false)
  if @bound_ctx
    @bound_ctx.on_unbind if @bound_ctx.respond_to? :on_unbind
    @bound_ctx = nil
  end
  reset if _force_reset or @anonymous # reset bucket if required
end