[telemetry] Create BrowserTestCase to reuse the browser for browser_unittest.
[chromium-blink-merge.git] / tools / telemetry / telemetry / unittest / browser_test_case.py
blobc824e74c5bed2b1a8c8c9c42527976b31036b57f
1 # Copyright 2014 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 import os
6 import unittest
8 from telemetry.core import browser_finder
9 from telemetry.unittest import options_for_unittests
10 from telemetry.util import path
13 class BrowserTestCase(unittest.TestCase):
14 @classmethod
15 def setUpClass(cls):
16 options = options_for_unittests.GetCopy()
17 cls.CustomizeBrowserOptions(options.browser_options)
18 browser_to_create = browser_finder.FindBrowser(options)
19 if not browser_to_create:
20 raise Exception('No browser found, cannot continue test.')
22 cls._browser = None
23 try:
24 cls._browser = browser_to_create.Create()
25 cls._browser.Start()
26 except:
27 cls.tearDownClass()
28 raise
30 @classmethod
31 def tearDownClass(cls):
32 if cls._browser:
33 cls._browser.Close()
34 cls._browser = None
36 @classmethod
37 def CustomizeBrowserOptions(cls, options):
38 """Override to add test-specific options to the BrowserOptions object"""
39 pass
41 @classmethod
42 def UrlOfUnittestFile(cls, filename):
43 cls._browser.SetHTTPServerDirectories(path.GetUnittestDataDir())
44 file_path = os.path.join(path.GetUnittestDataDir(), filename)
45 return cls._browser.http_server.UrlOf(file_path)