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 WebDriver 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 WebDriverFactory(object): |
| 18 """"Factory that creates WebDriver 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 NewWebDriver(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 Returns: |
| 35 selenium.webdriver.remote.webdriver.WebDriver instance |
| 36 """ |
| 37 self._StartServerIfNecessary() |
| 38 channel_id = 'testing' + hex(random.getrandbits(20 * 4))[2:-1] |
| 39 if not pyauto.IsWin(): |
| 40 channel_id = os.path.join(tempfile.gettempdir(), channel_id) |
| 41 pyauto.CreateNewAutomationProvider(channel_id) |
| 42 return webdriver.Remote(self._chromedriver_server.service_url, |
| 43 {'chrome.channel': channel_id}) |
| 44 |
| 45 def _StartServerIfNecessary(self): |
| 46 """Starts the ChromeDriver server, if not already started.""" |
| 47 if self._chromedriver_server is None: |
| 48 exe = pyauto_paths.GetChromeDriverExe() |
| 49 if exe is None: |
| 50 raise RuntimeError('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() |
OLD | NEW |