Class: Selenium::WebDriver::Firefox::Launcher

Inherits:
Object
  • Object
show all
Defined in:
lib/selenium/webdriver/firefox/launcher.rb

Constant Summary collapse

SOCKET_LOCK_TIMEOUT =
45
STABLE_CONNECTION_TIMEOUT =
60

Instance Method Summary collapse

Constructor Details

#initialize(binary, port, profile = nil) ⇒ Launcher

Returns a new instance of Launcher.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/selenium/webdriver/firefox/launcher.rb', line 11

def initialize(binary, port, profile = nil)
  @binary = binary
  @port   = Integer(port)

  raise Error::WebDriverError, "invalid port: #{@port}" if @port < 1

  if profile.kind_of? Profile
    @profile = profile
  else
    @profile_name = profile
    @profile = nil
  end

  @host = "127.0.0.1"
end

Instance Method Details

#assert_profileObject



104
105
106
# File 'lib/selenium/webdriver/firefox/launcher.rb', line 104

def assert_profile
  raise Error::WebDriverError, "must create_profile first" unless @profile && @profile_dir
end

#connect_until_stableObject



77
78
79
80
81
82
83
# File 'lib/selenium/webdriver/firefox/launcher.rb', line 77

def connect_until_stable
  poller = SocketPoller.new(@host, @port, STABLE_CONNECTION_TIMEOUT)
  unless poller.connected?
    @binary.quit
    raise Error::WebDriverError, "unable to obtain stable firefox connection in #{STABLE_CONNECTION_TIMEOUT} seconds"
  end
end

#create_profileObject



58
59
60
61
62
63
64
# File 'lib/selenium/webdriver/firefox/launcher.rb', line 58

def create_profile
  fetch_profile if @profile.nil?

  @profile.add_webdriver_extension
  @profile.port = @port
  @profile_dir = @profile.layout_on_disk
end

#fetch_profileObject



93
94
95
96
97
98
99
100
101
102
# File 'lib/selenium/webdriver/firefox/launcher.rb', line 93

def fetch_profile
  if @profile_name
    @profile = Profile.from_name @profile_name
    if @profile.nil?
      raise Error::WebDriverError, "unable to find profile named: #{@profile_name.inspect}"
    end
  else
    @profile = Profile.new
  end
end

#find_free_portObject



48
49
50
51
52
53
54
55
56
# File 'lib/selenium/webdriver/firefox/launcher.rb', line 48

def find_free_port
  port = @port

  until free_port?(port)
    port += 1
  end

  @port = port
end

#free_port?(port) ⇒ Boolean

Returns:

  • (Boolean)


85
86
87
88
89
90
91
# File 'lib/selenium/webdriver/firefox/launcher.rb', line 85

def free_port?(port)
  s = TCPServer.new(@host, port)
  s.close
  true
rescue SocketError, Errno::EADDRINUSE
  false
end

#launchObject



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/selenium/webdriver/firefox/launcher.rb', line 31

def launch
  socket_lock.locked do
    find_free_port
    create_profile
    start_silent_and_wait
    start
    connect_until_stable
  end

  self
end

#quitObject



43
44
45
46
# File 'lib/selenium/webdriver/firefox/launcher.rb', line 43

def quit
  @binary.quit
  FileReaper.reap(@profile_dir) if @profile_dir
end

#socket_lockObject



108
109
110
# File 'lib/selenium/webdriver/firefox/launcher.rb', line 108

def socket_lock
  @socket_lock ||= SocketLock.new(@port - 1, SOCKET_LOCK_TIMEOUT)
end

#startObject



66
67
68
69
# File 'lib/selenium/webdriver/firefox/launcher.rb', line 66

def start
  assert_profile
  @binary.start_with @profile, @profile_dir
end

#start_silent_and_waitObject



71
72
73
74
75
# File 'lib/selenium/webdriver/firefox/launcher.rb', line 71

def start_silent_and_wait
  assert_profile
  @binary.start_with @profile, @profile_dir, "-silent"
  @binary.wait
end

#urlObject



27
28
29
# File 'lib/selenium/webdriver/firefox/launcher.rb', line 27

def url
  "http://#{@host}:#{@port}/hub"
end