Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(265)

Unified Diff: chrome/test/pyautolib/chrome_driver_factory.py

Issue 7634031: Let pyauto create an attached webdriver instance to manipulate web pages. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: forgot to re-add the previously added files Created 9 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/test/pyautolib/chrome_driver_factory.py
diff --git a/chrome/test/pyautolib/chrome_driver_factory.py b/chrome/test/pyautolib/chrome_driver_factory.py
new file mode 100644
index 0000000000000000000000000000000000000000..3ea970db0749421b5e7dfb912ad220874c944c83
--- /dev/null
+++ b/chrome/test/pyautolib/chrome_driver_factory.py
@@ -0,0 +1,62 @@
+#!/usr/bin/python
+# 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 ChromeDriver instances for pyauto."""
+
+import os
+import random
+import tempfile
+
+import pyauto_paths
+from selenium import webdriver
+from selenium.webdriver.chrome import service
+
+
+class ChromeDriverFactory(object):
+ """"Factory that creates ChromeDriver 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 NewChromeDriver(self, pyauto):
+ """Creates a new remote WebDriver instance.
+
+ This instance will connect to a new automation provider of an already
+ running Chrome.
+ 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)
+ 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()
+ assert exe, 'Cannot find chromedriver exe. Did you build it?'
+ self._chromedriver_server = service.Service(exe)
+ self._chromedriver_server.start()
+
+ def Stop(self):
+ """Stops the ChromeDriver server, if running."""
+ if self._chromedriver_server is not None:
+ self._chromedriver_server.stop()
+ self._chromedriver_server = None
+
+ def __del__(self):
+ self.Stop()
+

Powered by Google App Engine
This is Rietveld 408576698