Suppression for crbug/241044.
[chromium-blink-merge.git] / chrome / test / install_test / install_test.py
blob71aab87dd2430d680cb9401f501705fcb159e7dd
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.
11 """
13 import os
14 import sys
15 import unittest
16 import urllib
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',
23 'pylib'))
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.
44 Example:
46 class SampleUpdater(InstallTest):
48 def testCanOpenGoogle(self):
49 self.Install(self.GetUpdateBuilds()[0])
50 self.StartChrome()
51 self._driver.get('http://www.google.com/')
52 self.Install(self.GetUpdateBuilds()[1])
53 self.StartChrome()
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__':
61 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
65 """
67 _installer_paths = {}
68 _chrome_driver = ''
69 _installer_options = []
70 _install_type = chrome_installer_win.InstallationType.USER
72 def __init__(self, methodName='runTest'):
73 unittest.TestCase.__init__(self, methodName)
74 self._driver = None
75 current_version = chrome_installer_win.ChromeInstallation.GetCurrent()
76 if current_version:
77 current_version.Uninstall()
79 def setUp(self):
80 """Called before each unittest to prepare the test fixture."""
81 self._StartService()
83 def tearDown(self):
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:
87 try:
88 self._driver.quit()
89 except WebDriverException:
90 pass
91 self._service.stop()
92 self._installation.Uninstall()
94 def _StartService(self):
95 """Starts ChromeDriver service."""
96 self._service = service.Service(InstallTest._chrome_driver)
97 self._service.start()
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
103 will be used.
105 Args:
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.
114 Args:
115 build: Chrome version number that will be used for installation.
116 master_pref: Location of the master preferences file.
118 if self._driver:
119 try:
120 self._driver.quit()
121 except WebDriverException:
122 pass
123 options = []
124 options.extend(self._installer_options)
125 if self._install_type == chrome_installer_win.InstallationType.SYSTEM:
126 options.append('--system-level')
127 if master_pref:
128 options.append('--installerdata=%s' % master_pref)
129 self._installation = chrome_installer_win.Install(
130 self._installer_paths[build],
131 self._install_type,
132 build,
133 options)
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
143 @staticmethod
144 def _Download(url, path):
145 """Downloads a file from the specified URL.
147 Args:
148 url: URL where the file is located.
149 path: Location where file will be downloaded.
151 Raises:
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)
158 @staticmethod
159 def SetInstallType(install_type):
160 """Sets Chrome installation type.
162 Args:
163 install_type: Type of installation(i.e., user or system).
165 InstallTest._install_type = install_type
167 @staticmethod
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.
176 Args:
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()
191 builds = []
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))
198 for build in 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,
205 'chrome-win32.test')
206 InstallTest._Download(url, InstallTest._chrome_driver)