Chromium Code Reviews| Index: chrome/test/pyautolib/webdriver_factory.py |
| diff --git a/chrome/test/pyautolib/webdriver_factory.py b/chrome/test/pyautolib/webdriver_factory.py |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..678082b4b43dd8626342eefba3bc51c02704c821 |
| --- /dev/null |
| +++ b/chrome/test/pyautolib/webdriver_factory.py |
| @@ -0,0 +1,57 @@ |
| +#!/usr/bin/python |
|
Nirnimesh
2011/08/01 19:21:24
I see 'webdriver' and 'chromedriver' used intercha
kkania
2011/08/02 15:10:44
changed to chromedriver
|
| +# Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| +# Use of this source code is governed by a BSD-style license that can be |
| +# found in the LICENSE file. |
| + |
| +"""Factory that creates WebDriver instances for pyauto.""" |
| + |
| +import os |
| +import random |
| +import tempfile |
| + |
| +import pyauto_paths |
| +from selenium import webdriver |
| +from selenium.webdriver.chrome import service |
| + |
| + |
| +class WebDriverFactory(object): |
| + """"Factory that creates WebDriver instances for pyauto. |
| + |
| + Starts a single chromedriver server when necessary. Users should call 'Stop' |
| + when no longer using the factory. |
| + """ |
| + |
| + def __init__(self): |
| + self._chromedriver_server = None |
| + |
| + def NewWebDriver(self, pyauto): |
| + """Creates a new remote WebDriver instance. |
|
Nirnimesh
2011/08/01 19:21:24
Why 'remote'?
kkania
2011/08/02 15:10:44
The remote webdriver is the most configurable. The
|
| + |
| + This instance will connect to a new automation provider of an already |
| + running Chrome. |
|
Nirnimesh
2011/08/01 19:21:24
nit: leave a blank line before Args: and Returns:
kkania
2011/08/02 15:10:44
Done.
|
| + Args: |
| + pyauto: pyauto.PyUITest instance |
| + Returns: |
| + selenium.webdriver.remote.webdriver.WebDriver instance |
| + """ |
| + self._StartServerIfNecessary() |
| + channel_id = 'testing' + hex(random.getrandbits(20 * 4))[2:-1] |
| + if not pyauto.IsWin(): |
| + channel_id = os.path.join(tempfile.gettempdir(), channel_id) |
|
Nirnimesh
2011/08/01 19:21:24
where does this file reside on linux?
kkania
2011/08/02 15:10:44
http://docs.python.org/library/tempfile.html#tempf
|
| + pyauto.CreateNewAutomationProvider(channel_id) |
| + return webdriver.Remote(self._chromedriver_server.service_url, |
| + {'chrome.channel': channel_id}) |
| + |
| + def _StartServerIfNecessary(self): |
| + """Starts the ChromeDriver server, if not already started.""" |
| + if self._chromedriver_server is None: |
| + exe = pyauto_paths.GetChromeDriverExe() |
| + if exe is None: |
|
Nirnimesh
2011/08/01 19:21:24
assert exe, 'Cannot find chromedriver exe. Did you
kkania
2011/08/02 15:10:44
Done.
|
| + raise RuntimeError('Cannot find chromedriver exe. Did you build it?') |
| + self._chromedriver_server = service.Service(exe) |
| + self._chromedriver_server.start() |
| + |
| + def Stop(self): |
|
Nirnimesh
2011/08/01 19:21:24
Should the destructor call Stop() automatically to
kkania
2011/08/02 15:10:44
It could, but since __del__ isn't called until the
Nirnimesh
2011/08/02 18:07:13
Yes please
__del__ is guaranteed to get called wh
kkania
2011/08/02 23:19:19
Done.
|
| + """Stops the ChromeDriver server, if running.""" |
| + if self._chromedriver_server is not None: |
|
Nirnimesh
2011/08/01 19:21:24
Add: self._chromedriver_server = None after this l
kkania
2011/08/02 15:10:44
Done.
|
| + self._chromedriver_server.stop() |