1# Copyright 2016 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. 4 5import unittest 6 7from py_utils import cloud_storage 8from telemetry.internal.browser import browser_finder 9from telemetry.testing import options_for_unittests 10from telemetry.util import wpr_modes 11 12 13class SeriallyExecutedBrowserTestCase(unittest.TestCase): 14 def __init__(self, methodName): 15 super(SeriallyExecutedBrowserTestCase, self).__init__(methodName) 16 self._private_methodname = methodName 17 18 def shortName(self): 19 """Returns the method name this test runs, without the package prefix.""" 20 return self._private_methodname 21 22 @classmethod 23 def Name(cls): 24 return cls.__name__ 25 26 @classmethod 27 def AddCommandlineArgs(cls, parser): 28 pass 29 30 @classmethod 31 def setUpClass(cls): 32 cls._finder_options = options_for_unittests.GetCopy() 33 cls.platform = None 34 cls.browser = None 35 cls._browser_to_create = None 36 cls._browser_options = None 37 38 @classmethod 39 def SetBrowserOptions(cls, browser_options): 40 """Sets the browser option for the browser to create. 41 42 Args: 43 browser_options: Browser options object for the browser we want to test. 44 """ 45 cls._browser_options = browser_options 46 cls._browser_to_create = browser_finder.FindBrowser(browser_options) 47 if not cls.platform: 48 cls.platform = cls._browser_to_create.platform 49 cls.platform.network_controller.InitializeIfNeeded() 50 else: 51 assert cls.platform == cls._browser_to_create.platform, ( 52 'All browser launches within same test suite must use browsers on ' 53 'the same platform') 54 55 @classmethod 56 def StartWPRServer(cls, archive_path=None, archive_bucket=None): 57 """Start a webpage replay server. 58 59 Args: 60 archive_path: Path to the WPR file. If there is a corresponding sha1 file, 61 this archive will be automatically downloaded from Google Storage. 62 archive_bucket: The bucket to look for the WPR archive. 63 """ 64 assert cls._browser_options, ( 65 'Browser options must be set with |SetBrowserOptions| prior to ' 66 'starting WPR') 67 assert not cls.browser, 'WPR must be started prior to browser being started' 68 69 cloud_storage.GetIfChanged(archive_path, archive_bucket) 70 cls.platform.network_controller.Open(wpr_modes.WPR_REPLAY, []) 71 cls.platform.network_controller.StartReplay(archive_path=archive_path) 72 73 @classmethod 74 def StopWPRServer(cls): 75 cls.platform.network_controller.StopReplay() 76 77 @classmethod 78 def StartBrowser(cls): 79 assert cls._browser_options, ( 80 'Browser options must be set with |SetBrowserOptions| prior to ' 81 'starting WPR') 82 assert not cls.browser, 'Browser is started. Must close it first' 83 84 cls.browser = cls._browser_to_create.Create(cls._browser_options) 85 86 @classmethod 87 def StopBrowser(cls): 88 assert cls.browser, 'Browser is not started' 89 cls.browser.Close() 90 cls.browser = None 91 92 @classmethod 93 def tearDownClass(cls): 94 if cls.platform: 95 cls.platform.StopAllLocalServers() 96 cls.platform.network_controller.Close() 97 if cls.browser: 98 cls.StopBrowser() 99 100 @classmethod 101 def SetStaticServerDirs(cls, dirs_path): 102 assert cls.platform 103 assert isinstance(dirs_path, list) 104 cls.platform.SetHTTPServerDirectories(dirs_path) 105 106 @classmethod 107 def UrlOfStaticFilePath(cls, file_path): 108 return cls.platform.http_server.UrlOf(file_path) 109