|
| 1 | +# Licensed to the Software Freedom Conservancy (SFC) under one |
| 2 | +# or more contributor license agreements. See the NOTICE file |
| 3 | +# distributed with this work for additional information |
| 4 | +# regarding copyright ownership. The SFC licenses this file |
| 5 | +# to you under the Apache License, Version 2.0 (the |
| 6 | +# "License"); you may not use this file except in compliance |
| 7 | +# with the License. You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, |
| 12 | +# software distributed under the License is distributed on an |
| 13 | +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +# KIND, either express or implied. See the License for the |
| 15 | +# specific language governing permissions and limitations |
| 16 | +# under the License. |
| 17 | +import subprocess |
| 18 | +from subprocess import PIPE |
| 19 | +import time |
| 20 | +from selenium.common.exceptions import WebDriverException |
| 21 | +from selenium.webdriver.common import utils |
| 22 | + |
| 23 | +class Service(object): |
| 24 | + """ |
| 25 | + Object that manages the starting and stopping of the EdgeDriver |
| 26 | + """ |
| 27 | + |
| 28 | + def __init__(self, executable_path, port=0): |
| 29 | + """ |
| 30 | + Creates a new instance of the Service |
| 31 | +
|
| 32 | + :Args: |
| 33 | + - executable_path : Path to the EdgeDriver |
| 34 | + - port : Port the service is running on |
| 35 | + """x |
| 36 | + |
| 37 | + self.path = executable_path |
| 38 | + |
| 39 | + self.port = port |
| 40 | + if self.port == 0: |
| 41 | + self.port = utils.free_port() |
| 42 | + |
| 43 | + def start(self): |
| 44 | + """ |
| 45 | + Starts the EdgeDriver Service. |
| 46 | +
|
| 47 | + :Exceptions: |
| 48 | + - WebDriverException : Raised either when it can't start the service |
| 49 | + or when it can't connect to the service |
| 50 | + """ |
| 51 | + try: |
| 52 | + cmd = [self.path, "--port=%d" % self.port] |
| 53 | + self.process = subprocess.Popen(cmd, |
| 54 | + stdout=PIPE, stderr=PIPE) |
| 55 | + except TypeError: |
| 56 | + raise |
| 57 | + except: |
| 58 | + raise WebDriverException( |
| 59 | + "The EdgeDriver executable needs to be available in the path. " |
| 60 | + "Please download from http://go.microsoft.com/fwlink/?LinkId=619687 ") |
| 61 | + count = 0 |
| 62 | + while not utils.is_url_connectable(self.port): |
| 63 | + count += 1 |
| 64 | + time.sleep(1) |
| 65 | + if count == 30: |
| 66 | + raise WebDriverException("Can not connect to the EdgeDriver") |
| 67 | + |
| 68 | + def stop(self): |
| 69 | + """ |
| 70 | + Tells the EdgeDriver to stop and cleans up the process |
| 71 | + """ |
| 72 | + #If its dead dont worry |
| 73 | + if self.process is None: |
| 74 | + return |
| 75 | + |
| 76 | + #Tell the Server to die! |
| 77 | + try: |
| 78 | + from urllib import request as url_request |
| 79 | + except ImportError: |
| 80 | + import urllib2 as url_request |
| 81 | + |
| 82 | + url_request.urlopen("http://127.0.0.1:%d/shutdown" % self.port) |
| 83 | + count = 0 |
| 84 | + while utils.is_connectable(self.port): |
| 85 | + if count == 30: |
| 86 | + break |
| 87 | + count += 1 |
| 88 | + time.sleep(1) |
| 89 | + |
| 90 | + #Tell the Server to properly die in case |
| 91 | + try: |
| 92 | + if self.process: |
| 93 | + self.process.stdout.close() |
| 94 | + self.process.stderr.close() |
| 95 | + self.process.kill() |
| 96 | + self.process.wait() |
| 97 | + except WindowsError: |
| 98 | + # kill may not be available under windows environment |
| 99 | + pass |
0 commit comments