| OLD | NEW |
| (Empty) |
| 1 #!/usr/bin/python | |
| 2 # Copyright (c) 2011 The Chromium Authors. All rights reserved. | |
| 3 # Use of this source code is governed by a BSD-style license that can be | |
| 4 # found in the LICENSE file. | |
| 5 | |
| 6 """Factory that creates ChromeDriver instances for pyauto.""" | |
| 7 | |
| 8 import os | |
| 9 import random | |
| 10 import tempfile | |
| 11 | |
| 12 import pyauto_paths | |
| 13 from selenium import webdriver | |
| 14 from selenium.webdriver.chrome import service | |
| 15 | |
| 16 | |
| 17 class ChromeDriverFactory(object): | |
| 18 """"Factory that creates ChromeDriver instances for pyauto. | |
| 19 | |
| 20 Starts a single chromedriver server when necessary. Users should call 'Stop' | |
| 21 when no longer using the factory. | |
| 22 """ | |
| 23 | |
| 24 def __init__(self): | |
| 25 self._chromedriver_server = None | |
| 26 | |
| 27 def NewChromeDriver(self, pyauto): | |
| 28 """Creates a new remote WebDriver instance. | |
| 29 | |
| 30 This instance will connect to a new automation provider of an already | |
| 31 running Chrome. | |
| 32 Args: | |
| 33 pyauto: pyauto.PyUITest instance | |
| 34 | |
| 35 Returns: | |
| 36 selenium.webdriver.remote.webdriver.WebDriver instance | |
| 37 """ | |
| 38 self._StartServerIfNecessary() | |
| 39 channel_id = 'testing' + hex(random.getrandbits(20 * 4))[2:-1] | |
| 40 if not pyauto.IsWin(): | |
| 41 channel_id = os.path.join(tempfile.gettempdir(), channel_id) | |
| 42 pyauto.CreateNewAutomationProvider(channel_id) | |
| 43 return webdriver.Remote(self._chromedriver_server.service_url, | |
| 44 {'chrome.channel': channel_id}) | |
| 45 | |
| 46 def _StartServerIfNecessary(self): | |
| 47 """Starts the ChromeDriver server, if not already started.""" | |
| 48 if self._chromedriver_server is None: | |
| 49 exe = pyauto_paths.GetChromeDriverExe() | |
| 50 assert exe, 'Cannot find chromedriver exe. Did you build it?' | |
| 51 self._chromedriver_server = service.Service(exe) | |
| 52 self._chromedriver_server.start() | |
| 53 | |
| 54 def Stop(self): | |
| 55 """Stops the ChromeDriver server, if running.""" | |
| 56 if self._chromedriver_server is not None: | |
| 57 self._chromedriver_server.stop() | |
| 58 self._chromedriver_server = None | |
| 59 | |
| 60 def __del__(self): | |
| 61 self.Stop() | |
| 62 | |
| OLD | NEW |