diff options
author | kkania@chromium.org <kkania@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-08-15 17:03:45 +0000 |
---|---|---|
committer | kkania@chromium.org <kkania@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-08-15 17:03:45 +0000 |
commit | 16b1429e1cafbadaa66003dcf8e9c9b720df52ef (patch) | |
tree | 2fb3db631c736fb659b51f4db8a4b45ccf65c30b /chrome/test/pyautolib | |
parent | 4ae54ce3b336e361efd3acf0de21f7116cac0e5a (diff) | |
download | chromium_src-16b1429e1cafbadaa66003dcf8e9c9b720df52ef.zip chromium_src-16b1429e1cafbadaa66003dcf8e9c9b720df52ef.tar.gz chromium_src-16b1429e1cafbadaa66003dcf8e9c9b720df52ef.tar.bz2 |
Let pyauto create an attached webdriver instance to manipulate web pages.
Same patch as before with some fixes.
BUG=49379
TEST=none
Review URL: http://codereview.chromium.org/7634031
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@96788 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/test/pyautolib')
-rw-r--r-- | chrome/test/pyautolib/chrome_driver_factory.py | 62 | ||||
-rw-r--r-- | chrome/test/pyautolib/pyauto.py | 62 | ||||
-rw-r--r-- | chrome/test/pyautolib/pyauto_paths.py | 57 |
3 files changed, 156 insertions, 25 deletions
diff --git a/chrome/test/pyautolib/chrome_driver_factory.py b/chrome/test/pyautolib/chrome_driver_factory.py new file mode 100644 index 0000000..3ea970d --- /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() + diff --git a/chrome/test/pyautolib/pyauto.py b/chrome/test/pyautolib/pyauto.py index 329c9bf..89ef512 100644 --- a/chrome/test/pyautolib/pyauto.py +++ b/chrome/test/pyautolib/pyauto.py @@ -46,35 +46,17 @@ import types import unittest import urllib +import pyauto_paths + def _LocateBinDirs(): """Setup a few dirs where we expect to find dependency libraries.""" - script_dir = os.path.dirname(__file__) - chrome_src = os.path.join(script_dir, os.pardir, os.pardir, os.pardir) - - bin_dirs = { - 'linux2': [ os.path.join(chrome_src, 'out', 'Debug'), - os.path.join(chrome_src, 'sconsbuild', 'Debug'), - os.path.join(chrome_src, 'out', 'Release'), - os.path.join(chrome_src, 'sconsbuild', 'Release')], - 'linux3': [ os.path.join(chrome_src, 'out', 'Debug'), - os.path.join(chrome_src, 'sconsbuild', 'Debug'), - os.path.join(chrome_src, 'out', 'Release'), - os.path.join(chrome_src, 'sconsbuild', 'Release')], - 'darwin': [ os.path.join(chrome_src, 'xcodebuild', 'Debug'), - os.path.join(chrome_src, 'xcodebuild', 'Release')], - 'win32': [ os.path.join(chrome_src, 'chrome', 'Debug'), - os.path.join(chrome_src, 'build', 'Debug'), - os.path.join(chrome_src, 'chrome', 'Release'), - os.path.join(chrome_src, 'build', 'Release')], - 'cygwin': [ os.path.join(chrome_src, 'chrome', 'Debug'), - os.path.join(chrome_src, 'chrome', 'Release')], - } - deps_dirs = [ os.path.join(script_dir, os.pardir, - os.pardir, os.pardir, 'third_party'), - script_dir, + deps_dirs = [ + os.path.dirname(__file__), + pyauto_paths.GetThirdPartyDir(), + os.path.join(pyauto_paths.GetThirdPartyDir(), 'webdriver', 'python'), ] - sys.path += map(os.path.normpath, bin_dirs.get(sys.platform, []) + deps_dirs) + sys.path += map(os.path.normpath, pyauto_paths.GetBuildDirs() + deps_dirs) _LocateBinDirs() @@ -105,6 +87,7 @@ from pyauto_errors import NTPThumbnailNotShownError import pyauto_utils import simplejson as json # found in third_party +_CHROME_DRIVER_FACTORY = None _HTTP_SERVER = None _REMOTE_PROXY = None _OPTIONS = None @@ -2830,6 +2813,31 @@ class PyUITest(pyautolib.PyUITestBase, unittest.TestCase): } return self._GetResultFromJSONRequest(cmd_dict) + def NewWebDriver(self): + """Returns a new remote WebDriver instance. + + Returns: + selenium.webdriver.remote.webdriver.WebDriver instance + """ + from chrome_driver_factory import ChromeDriverFactory + global _CHROME_DRIVER_FACTORY + if _CHROME_DRIVER_FACTORY is None: + _CHROME_DRIVER_FACTORY = ChromeDriverFactory() + return _CHROME_DRIVER_FACTORY.NewChromeDriver(self) + + def CreateNewAutomationProvider(self, channel_id): + """Creates a new automation provider. + + The provider will open a named channel in server mode. + Args: + channel_id: the channel_id to open the server channel with + """ + cmd_dict = { + 'command': 'CreateNewAutomationProvider', + 'channel_id': channel_id + } + self._GetResultFromJSONRequest(cmd_dict) + ## ChromeOS section def GetLoginInfo(self): @@ -3822,6 +3830,10 @@ class PyUITestSuite(pyautolib.PyUITestSuiteBase, unittest.TestSuite): if _HTTP_SERVER: self._StopHTTPServer() + global _CHROME_DRIVER_FACTORY + if _CHROME_DRIVER_FACTORY is not None: + _CHROME_DRIVER_FACTORY.Stop() + def _StartHTTPServer(self): """Start a local file server hosting data files over http://""" global _HTTP_SERVER diff --git a/chrome/test/pyautolib/pyauto_paths.py b/chrome/test/pyautolib/pyauto_paths.py new file mode 100644 index 0000000..5b9bb1e --- /dev/null +++ b/chrome/test/pyautolib/pyauto_paths.py @@ -0,0 +1,57 @@ +#!/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. + +"""Common paths for pyauto tests.""" + +import os +import sys + + +def GetSourceDir(): + """Returns src/ directory.""" + script_dir = os.path.abspath(os.path.dirname(__file__)) + return os.path.join(script_dir, os.pardir, os.pardir, os.pardir) + + +def GetThirdPartyDir(): + """Returns src/third_party directory.""" + return os.path.join(GetSourceDir(), 'third_party') + + +def GetBuildDirs(): + """Returns list of possible build directories.""" + build_dirs = { + 'linux2': [ os.path.join('out', 'Debug'), + os.path.join('sconsbuild', 'Debug'), + os.path.join('out', 'Release'), + os.path.join('sconsbuild', 'Release')], + 'linux3': [ os.path.join('out', 'Debug'), + os.path.join('sconsbuild', 'Debug'), + os.path.join('out', 'Release'), + os.path.join('sconsbuild', 'Release')], + 'darwin': [ os.path.join('xcodebuild', 'Debug'), + os.path.join('xcodebuild', 'Release')], + 'win32': [ os.path.join('chrome', 'Debug'), + os.path.join('build', 'Debug'), + os.path.join('chrome', 'Release'), + os.path.join('build', 'Release')], + 'cygwin': [ os.path.join('chrome', 'Debug'), + os.path.join('chrome', 'Release')], + } + src_dir = GetSourceDir() + return map(lambda dir: os.path.join(src_dir, dir), + build_dirs.get(sys.platform, [])) + + +def GetChromeDriverExe(): + """Returns path to ChromeDriver executable, or None if cannot be found.""" + exe_name = 'chromedriver' + if sys.platform == 'win32': + exe_name += '.exe' + for dir in GetBuildDirs(): + exe = os.path.join(dir, exe_name) + if os.path.exists(exe): + return exe + return None |