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 = DEFAULT_PORT, profile = DEFAULT_PROFILE_NAME) ⇒ Launcher

Returns a new instance of Launcher.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/selenium/webdriver/firefox/launcher.rb', line 13

def initialize(binary, port = DEFAULT_PORT, profile = DEFAULT_PROFILE_NAME)
  @binary       = binary
  @port         = port.to_i

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

  # need to be really specific about what host to use
  #
  # on os x, "localhost" will resolve to 3 different addresses (see /etc/hosts)
  # Ruby will loop over these and happily bind to the same port on each one,
  # making it completely unusable for our purposes.
  #
  @host = "127.0.0.1"
end

Instance Method Details

#assert_profileObject



146
147
148
# File 'lib/selenium/webdriver/firefox/launcher.rb', line 146

def assert_profile
  raise "must create_profile first" if @profile.nil?
end

#can_connect?Boolean

Returns:

  • (Boolean)


117
118
119
120
121
122
123
# File 'lib/selenium/webdriver/firefox/launcher.rb', line 117

def can_connect?
  TCPSocket.new(@host, @port).close
  true
rescue Errno::ECONNREFUSED, Errno::ENOTCONN, SocketError => e
  $stderr.puts "#{e.message} for #{@host}:#{@port}" if $DEBUG
  false
end

#connect_until_stableObject



106
107
108
109
110
111
112
113
114
115
# File 'lib/selenium/webdriver/firefox/launcher.rb', line 106

def connect_until_stable
  max_time = Time.now + STABLE_CONNECTION_TIMEOUT

  until Time.now >= max_time
    return if can_connect?
    sleep 0.25
  end

  raise Error::WebDriverError, "unable to obtain stable firefox connection in #{STABLE_CONNECTION_TIMEOUT} seconds"
end

#create_profileObject



81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/selenium/webdriver/firefox/launcher.rb', line 81

def create_profile
  unless @profile
    fetch_profile
    if @profile.nil?
      raise Error, WebDriverError, "could not find or create profile: #{profile.inspect}"
    end
  end

  @profile.delete_extensions_cache
  @profile.port = @port
  @profile.add_webdriver_extension(true)
  @profile.update_user_prefs
end

#fetch_profileObject



133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/selenium/webdriver/firefox/launcher.rb', line 133

def fetch_profile
  existing = Profile.from_name @profile_name

  unless existing
    @binary.create_base_profile @profile_name
    Profile.ini.refresh
    existing = Profile.from_name @profile_name
    raise "unable to find or create new profile" unless existing
  end

  @profile = existing
end

#find_free_portObject



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

def find_free_port
  port = @port

  until free_port?(port)
    port += 1
  end

  @port = port
end

#free_port?(port) ⇒ Boolean

Returns:

  • (Boolean)


125
126
127
128
129
130
131
# File 'lib/selenium/webdriver/firefox/launcher.rb', line 125

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

#launchObject



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/selenium/webdriver/firefox/launcher.rb', line 37

def launch
  with_lock do
    find_free_port
    create_profile
    start_silent_and_wait
    start
    connect_until_stable
  end

  self
end

#startObject



95
96
97
98
# File 'lib/selenium/webdriver/firefox/launcher.rb', line 95

def start
  assert_profile
  @binary.start_with @profile
end

#start_silent_and_waitObject



100
101
102
103
104
# File 'lib/selenium/webdriver/firefox/launcher.rb', line 100

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

#urlObject



33
34
35
# File 'lib/selenium/webdriver/firefox/launcher.rb', line 33

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

#with_lockObject



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/selenium/webdriver/firefox/launcher.rb', line 49

def with_lock
  max_time = Time.now + SOCKET_LOCK_TIMEOUT
  locking_port = @port - 1

  until Time.now > max_time
    begin
      socket_lock = TCPServer.new(@host, locking_port)
      # make sure the fd is not inherited by firefox
      socket_lock.fcntl(Fcntl::F_SETFD, Fcntl::FD_CLOEXEC) if defined? Fcntl::FD_CLOEXEC

      yield
      return
    rescue SocketError, Errno::EADDRINUSE
      sleep 0.1
    end
  end

  raise Error::WebDriverError, "unable to bind to locking port #{locking_port} within #{SOCKET_LOCK_TIMEOUT} seconds"
ensure
  socket_lock.close if socket_lock
end