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.
4
5import os
6import unittest
7
8from telemetry.core import browser_finder
9from telemetry.unittest import options_for_unittests
10from telemetry.util import path
11
12
13class 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.')
21
22    cls._browser = None
23    try:
24      cls._browser = browser_to_create.Create()
25    except:
26      cls.tearDownClass()
27      raise
28
29  @classmethod
30  def tearDownClass(cls):
31    if cls._browser:
32      cls._browser.Close()
33      cls._browser = None
34
35  @classmethod
36  def CustomizeBrowserOptions(cls, options):
37    """Override to add test-specific options to the BrowserOptions object"""
38    pass
39
40  @classmethod
41  def UrlOfUnittestFile(cls, filename):
42    cls._browser.SetHTTPServerDirectories(path.GetUnittestDataDir())
43    file_path = os.path.join(path.GetUnittestDataDir(), filename)
44    return cls._browser.http_server.UrlOf(file_path)
45