OLD | NEW |
---|---|
(Empty) | |
1 #!/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
| |
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. | |
Nirnimesh
2011/08/01 19:21:24
Why 'remote'?
kkania
2011/08/02 15:10:44
The remote webdriver is the most configurable. The
| |
29 | |
30 This instance will connect to a new automation provider of an already | |
31 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.
| |
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) | |
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
| |
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: | |
Nirnimesh
2011/08/01 19:21:24
assert exe, 'Cannot find chromedriver exe. Did you
kkania
2011/08/02 15:10:44
Done.
| |
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): | |
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.
| |
55 """Stops the ChromeDriver server, if running.""" | |
56 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.
| |
57 self._chromedriver_server.stop() | |
OLD | NEW |