|
| 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 |
0 commit comments