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, _caps) ⇒ DriverBucket

Returns a new instance of DriverBucket.



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

def initialize(_session_id, _anonymous, _caps)
  @session_id = _session_id
  @bound_ctx = nil
  @anonymous = _anonymous
  @caps = _caps
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



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

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)


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

def bound?
  not @bound_ctx.nil?
end

#driver(_reset = false) ⇒ Object

get the current driver instance, reset it if required



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/selenium_surfer/driver_bucket.rb', line 20

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?

    case driver_name.to_sym
    when :remote
      # select capabilities object
      caps = SeleniumSurfer.config[:capabilities]
      caps = @caps if @caps
      caps = Selenium::WebDriver::Remote::Capabilities.firefox if caps.nil?

      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, :desired_capabilities => caps
    else
      @driver = Selenium::WebDriver.for driver_name.to_sym

      # apply browser configuration to new driver
      @driver.manage.window.resize_to(SeleniumSurfer.config[:window_width], SeleniumSurfer.config[:window_height]) rescue nil
    end
  end

  return @driver
end

#resetObject

force current driver connection to be discarded



55
56
57
58
59
60
# File 'lib/selenium_surfer/driver_bucket.rb', line 55

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

#unbindObject

unbinds the currently bound context.



78
79
80
81
82
83
84
# File 'lib/selenium_surfer/driver_bucket.rb', line 78

def unbind
  if @bound_ctx
    @bound_ctx.on_unbind if @bound_ctx.respond_to? :on_unbind
    @bound_ctx = nil
  end
  reset if @anonymous # reset bucket if required
end