Skip to content

Commit 90a1382

Browse files
author
AutomatedTester
committed
Add service handler and minimal update to driver to use service
1 parent 03587ea commit 90a1382

File tree

2 files changed

+123
-7
lines changed

2 files changed

+123
-7
lines changed

Diff for: py/selenium/webdriver/firefox/service.py

+113
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
#!/usr/bin/python
2+
#
3+
# Copyright 2011 Webdriver_name committers
4+
# Copyright 2011 Google Inc.
5+
#
6+
# Licensed under the Apache License, Version 2.0 (the "License");
7+
# you may not use this file except in compliance with the License.
8+
# You may obtain a copy of the License at
9+
#
10+
# http://www.apache.org/licenses/LICENSE-2.0
11+
#
12+
# Unless required by applicable law or agreed to in writing, software
13+
# distributed under the License is distributed on an "AS IS" BASIS,
14+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
# See the License for the specific language governing permissions and
16+
# limitations under the License.
17+
import os
18+
import subprocess
19+
from subprocess import PIPE
20+
import time
21+
22+
from selenium.common.exceptions import WebDriverException
23+
from selenium.webdriver.common import utils
24+
25+
class Service(object):
26+
"""
27+
Object that manages the starting and stopping of the GeckoDriver
28+
"""
29+
30+
def __init__(self, executable_path, firefox_binary=None, port=0, service_args=None,
31+
log_path=None, env=None):
32+
"""
33+
Creates a new instance of the Service
34+
35+
:Args:
36+
- executable_path : Path to the GeckoDriver
37+
- port : Port the service is running on
38+
- service_args : List of args to pass to the Geckodriver service
39+
- log_path : Path for the GeckoDriver service to log to"""
40+
41+
self.port = port
42+
self.path = executable_path
43+
self.firefox_binary = firefox_binary
44+
self.service_args = service_args or []
45+
46+
if self.port == 0:
47+
self.port = utils.free_port()
48+
self.env = env
49+
50+
def start(self):
51+
"""
52+
Starts the GeckoDriver "wires" Service.
53+
54+
:Exceptions:
55+
- WebDriverException : Raised either when it can't start the service
56+
or when it can't connect to the service
57+
"""
58+
env = self.env or os.environ
59+
try:
60+
#import pdb; pdb.set_trace()
61+
self.process = subprocess.Popen([
62+
self.path,
63+
"-b", self.firefox_binary, '--webdriver-port', "%d" % self.port],
64+
env=env, stdout=PIPE)
65+
except Exception as e:
66+
raise WebDriverException(
67+
"'" + os.path.basename(self.path) + "' executable needs to be \
68+
available in the path.\n %s" % str(e))
69+
count = 0
70+
while not utils.is_connectable(self.port):
71+
count += 1
72+
time.sleep(1)
73+
if count == 30:
74+
raise WebDriverException("Can not connect to the '" +
75+
os.path.basename(self.path) + "'")
76+
77+
@property
78+
def service_url(self):
79+
"""
80+
Gets the url of the GeckoDriver Service
81+
"""
82+
return "http://localhost:%d" % self.port
83+
84+
def stop(self):
85+
"""
86+
Tells the GeckoDriver to stop and cleans up the process
87+
"""
88+
#If its dead dont worry
89+
if self.process is None:
90+
return
91+
92+
#Tell the Server to die!
93+
'''try:
94+
from urllib import request as url_request
95+
except ImportError:
96+
import urllib2 as url_request
97+
98+
url_request.urlopen("http://127.0.0.1:%d/shutdown" % self.port)
99+
count = 0
100+
while utils.is_connectable(self.port):
101+
if count == 30:
102+
break
103+
count += 1
104+
time.sleep(1)
105+
'''
106+
#Tell the Server to properly die in case
107+
try:
108+
if self.process:
109+
self.process.kill()
110+
self.process.wait()
111+
except OSError:
112+
# kill may not be available under windows environment
113+
pass

Diff for: py/selenium/webdriver/firefox/webdriver.py

+10-7
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import socket
2626
import sys
2727
from .firefox_binary import FirefoxBinary
28+
from .service import Service
2829
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
2930
from selenium.webdriver.firefox.extension_connection import ExtensionConnection
3031
from selenium.webdriver.firefox.firefox_profile import FirefoxProfile
@@ -36,8 +37,8 @@ class WebDriver(RemoteWebDriver):
3637
# There is no native event support on Mac
3738
NATIVE_EVENTS_ALLOWED = sys.platform != "darwin"
3839

39-
def __init__(self, firefox_profile=None, firefox_binary=None, timeout=30,
40-
capabilities=None, proxy=None):
40+
def __init__(self, firefox_profile=None, firefox_binary='/Users/dburns/development/mozilla/mozilla-inbound/obj-ff-dbg/dist/Nightly.app/Contents/MacOS/firefox', timeout=30,
41+
capabilities=None, proxy=None, executable_path='wires'):
4142

4243
self.binary = firefox_binary
4344
self.profile = firefox_profile
@@ -57,9 +58,11 @@ def __init__(self, firefox_profile=None, firefox_binary=None, timeout=30,
5758
if proxy is not None:
5859
proxy.add_to_capabilities(capabilities)
5960

61+
self.service = Service(executable_path, firefox_binary=self.binary)
62+
self.service.start()
63+
6064
RemoteWebDriver.__init__(self,
61-
command_executor=ExtensionConnection("127.0.0.1", self.profile,
62-
self.binary, timeout),
65+
command_executor=self.service.service_url,
6366
desired_capabilities=capabilities,
6467
keep_alive=True)
6568
self._is_remote = False
@@ -72,13 +75,13 @@ def quit(self):
7275
# Happens if Firefox shutsdown before we've read the response from
7376
# the socket.
7477
pass
75-
self.binary.kill()
76-
try:
78+
self.service.stop()
79+
'''try:
7780
shutil.rmtree(self.profile.path)
7881
if self.profile.tempfolder is not None:
7982
shutil.rmtree(self.profile.tempfolder)
8083
except Exception as e:
81-
print(str(e))
84+
print(str(e))'''
8285

8386
@property
8487
def firefox_profile(self):

0 commit comments

Comments
 (0)