summaryrefslogtreecommitdiffstats
path: root/chrome/test/install_test/chrome_options.py
blob: 48b67687e1c503ef886a315f419fa1c03ed6db31 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# Copyright (c) 2012 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

"""Chrome-specific options for configuring a ChromeDriver instance."""

import base64


class ChromeOptions(object):
  """Chrome-specific options for configuring a ChromeDriver instance."""

  def __init__(self):
    """Initialize ChromeOptions object."""
    self._capabilities = {'chrome.switches': [], 'chrome.extensions': []}

  def AddSwitch(self, switch):
    """Add a switch to be passed to Chrome.

    Args:
      switch: String switch to be passed to Chrome.
    """
    self._capabilities['chrome.switches'].append(switch)

  def AddExtension(self, extension):
    """Add an extension to be loaded onto Chrome.

    Args:
      extension: String path to the extension to be loaded onto Chrome.
    """
    with open(extension, 'rb') as ext_file:
      self._capabilities['chrome.extensions'].append(
          base64.b64encode(ext_file.read()))

  def SetUserDataDir(self, user_data_dir):
    """Set the Chrome user data dir.

    Args:
      user_data_dir: String path to the profile directory.
    """
    self.AddSwitch('user-data-dir=%s' % user_data_dir)

  def GetCapabilities(self):
    """Returns a capabilities object suitable for using with ChromeDriver."""
    return self._capabilities