1 # Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file.
5 """Test fixture for tests involving installing/updating Chrome.
7 Provides an interface to install or update chrome from within a testcase, and
8 allows users to run tests using installed version of Chrome. User and system
9 level installations are supported, and either one can be used for running the
10 tests. Currently the only platform it supports is Windows.
18 import chrome_installer_win
20 _DIRECTORY
= os
.path
.dirname(os
.path
.abspath(__file__
))
21 sys
.path
.append(os
.path
.join(_DIRECTORY
, os
.path
.pardir
, os
.path
.pardir
,
22 os
.path
.pardir
, 'third_party', 'webdriver',
24 sys
.path
.append(os
.path
.join(_DIRECTORY
, os
.path
.pardir
, 'pylib'))
26 # This import should go after sys.path is set appropriately.
27 from chrome
import Chrome
28 from selenium
import webdriver
29 import selenium
.webdriver
.chrome
.service
as service
30 from selenium
.webdriver
.chrome
.service
import WebDriverException
32 from common
import util
35 class InstallTest(unittest
.TestCase
):
36 """Base updater test class.
38 All dependencies, like Chrome installers and ChromeDriver, are downloaded at
39 the beginning of the test. Dependencies are downloaded in the temp directory.
40 This download occurs only once, before the first test is executed. Each test
41 case starts an instance of ChromeDriver and terminates it upon completion.
42 All updater tests should derive from this class.
46 class SampleUpdater(InstallTest):
48 def testCanOpenGoogle(self):
49 self.Install(self.GetUpdateBuilds()[0])
51 self._driver.get('http://www.google.com/')
52 self.Install(self.GetUpdateBuilds()[1])
54 self._driver.get('http://www.google.org/')
56 Include the following in your updater test script to make it run standalone.
58 from install_test import Main
60 if __name__ == '__main__':
63 To fire off an updater test, use the command below.
64 python test_script.py --url=<URL> --update-builds=24.0.1299.0,24.0.1300.0
69 _installer_options
= []
70 _install_type
= chrome_installer_win
.InstallationType
.USER
72 def __init__(self
, methodName
='runTest'):
73 unittest
.TestCase
.__init
__(self
, methodName
)
75 current_version
= chrome_installer_win
.ChromeInstallation
.GetCurrent()
77 current_version
.Uninstall()
80 """Called before each unittest to prepare the test fixture."""
84 """Called at the end of each unittest to do any test related cleanup."""
85 # Confirm ChromeDriver was instantiated, before attempting to quit.
86 if self
._driver
is not None:
89 except WebDriverException
:
92 self
._installation
.Uninstall()
94 def _StartService(self
):
95 """Starts ChromeDriver service."""
96 self
._service
= service
.Service(InstallTest
._chrome
_driver
)
99 def StartChrome(self
, caps
={}, options
=None):
100 """Creates a ChromeDriver instance.
102 If both caps and options have the same settings, the settings from options
106 caps: Capabilities that will be passed to ChromeDriver.
107 options: ChromeOptions object that will be passed to ChromeDriver.
109 self
._driver
= Chrome(self
._service
.service_url
, caps
, options
)
111 def Install(self
, build
, master_pref
=None):
112 """Helper method that installs the specified Chrome build.
115 build: Chrome version number that will be used for installation.
116 master_pref: Location of the master preferences file.
121 except WebDriverException
:
124 options
.extend(self
._installer
_options
)
125 if self
._install
_type
== chrome_installer_win
.InstallationType
.SYSTEM
:
126 options
.append('--system-level')
128 options
.append('--installerdata=%s' % master_pref
)
129 self
._installation
= chrome_installer_win
.Install(
130 self
._installer
_paths
[build
],
135 def GetInstallBuild(self
):
136 """Returns Chorme build to be used for install test scenarios."""
137 return self
._install
_build
139 def GetUpdateBuilds(self
):
140 """Returns Chrome builds to be used for update test scenarios."""
141 return self
._update
_builds
144 def _Download(url
, path
):
145 """Downloads a file from the specified URL.
148 url: URL where the file is located.
149 path: Location where file will be downloaded.
152 RuntimeError: URL or file name is invalid.
154 if not util
.DoesUrlExist(url
):
155 raise RuntimeError('Either the URL or the file name is invalid.')
156 urllib
.urlretrieve(url
, path
)
159 def SetInstallType(install_type
):
160 """Sets Chrome installation type.
163 install_type: Type of installation(i.e., user or system).
165 InstallTest
._install
_type
= install_type
168 def InitTestFixture(install_build
, update_builds
, base_url
, options
):
169 """Static method for passing command options to InstallTest.
171 We do not instantiate InstallTest. Therefore, command arguments cannot be
172 passed to its constructor. Since InstallTest needs to use these options,
173 and using globals is not an option, this method can be used by the Main
174 class to pass the arguments it parses onto InstallTest.
177 install_build: A string representing the Chrome build to be used for
178 install testing. Pass this argument only if testing
179 fresh install scenarios.
180 update_builds: A list that contains the Chrome builds to be used for
181 testing update scenarios. Pass this argument only if
182 testing upgrade scenarios.
183 base_url: Base url of the 'official chrome builds' page.
184 options: A list that contains options to be passed to Chrome installer.
186 system
= util
.GetPlatformName()
187 InstallTest
._install
_build
= install_build
188 InstallTest
._update
_builds
= update_builds
189 InstallTest
._installer
_options
= options
190 tempdir
= util
.MakeTempDir()
192 if InstallTest
._install
_build
:
193 builds
.append(InstallTest
._install
_build
)
194 if InstallTest
._update
_builds
:
195 builds
.extend(InstallTest
._update
_builds
)
196 # Remove any duplicate build numbers.
197 builds
= list(frozenset(builds
))
199 url
= '%s%s/%s/mini_installer.exe' % (base_url
, build
, system
)
200 installer_path
= os
.path
.join(tempdir
, 'mini_installer_%s.exe' % build
)
201 InstallTest
._installer
_paths
[build
] = installer_path
202 InstallTest
._Download
(url
, installer_path
)
203 InstallTest
._chrome
_driver
= os
.path
.join(tempdir
, 'chromedriver.exe')
204 url
= '%s%s/%s/%s/chromedriver.exe' % (base_url
, build
, system
,
206 InstallTest
._Download
(url
, InstallTest
._chrome
_driver
)