options.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. # Licensed to the Software Freedom Conservancy (SFC) under one
  2. # or more contributor license agreements. See the NOTICE file
  3. # distributed with this work for additional information
  4. # regarding copyright ownership. The SFC licenses this file
  5. # to you under the Apache License, Version 2.0 (the
  6. # "License"); you may not use this file except in compliance
  7. # with the License. You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing,
  12. # software distributed under the License is distributed on an
  13. # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  14. # KIND, either express or implied. See the License for the
  15. # specific language governing permissions and limitations
  16. # under the License.
  17. import base64
  18. import os
  19. from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
  20. class Options(object):
  21. def __init__(self):
  22. self._binary_location = ''
  23. self._arguments = []
  24. self._extension_files = []
  25. self._extensions = []
  26. self._experimental_options = {}
  27. self._debugger_address = None
  28. @property
  29. def binary_location(self):
  30. """
  31. Returns the location of the binary otherwise an empty string
  32. """
  33. return self._binary_location
  34. @binary_location.setter
  35. def binary_location(self, value):
  36. """
  37. Allows you to set where the chromium binary lives
  38. :Args:
  39. - value: path to the Chromium binary
  40. """
  41. self._binary_location = value
  42. @property
  43. def debugger_address(self):
  44. """
  45. Returns the address of the remote devtools instance
  46. """
  47. return self._debugger_address
  48. @debugger_address.setter
  49. def debugger_address(self, value):
  50. """
  51. Allows you to set the address of the remote devtools instance
  52. that the ChromeDriver instance will try to connect to during an
  53. active wait.
  54. :Args:
  55. - value: address of remote devtools instance if any (hostname[:port])
  56. """
  57. self._debugger_address = value
  58. @property
  59. def arguments(self):
  60. """
  61. Returns a list of arguments needed for the browser
  62. """
  63. return self._arguments
  64. def add_argument(self, argument):
  65. """
  66. Adds an argument to the list
  67. :Args:
  68. - Sets the arguments
  69. """
  70. if argument:
  71. self._arguments.append(argument)
  72. else:
  73. raise ValueError("argument can not be null")
  74. @property
  75. def extensions(self):
  76. """
  77. Returns a list of encoded extensions that will be loaded into chrome
  78. """
  79. encoded_extensions = []
  80. for ext in self._extension_files:
  81. file_ = open(ext, 'rb')
  82. # Should not use base64.encodestring() which inserts newlines every
  83. # 76 characters (per RFC 1521). Chromedriver has to remove those
  84. # unnecessary newlines before decoding, causing performance hit.
  85. encoded_extensions.append(base64.b64encode(file_.read()).decode('UTF-8'))
  86. file_.close()
  87. return encoded_extensions + self._extensions
  88. def add_extension(self, extension):
  89. """
  90. Adds the path to the extension to a list that will be used to extract it
  91. to the ChromeDriver
  92. :Args:
  93. - extension: path to the \*.crx file
  94. """
  95. if extension:
  96. extension_to_add = os.path.abspath(os.path.expanduser(extension))
  97. if os.path.exists(extension_to_add):
  98. self._extension_files.append(extension_to_add)
  99. else:
  100. raise IOError("Path to the extension doesn't exist")
  101. else:
  102. raise ValueError("argument can not be null")
  103. def add_encoded_extension(self, extension):
  104. """
  105. Adds Base64 encoded string with extension data to a list that will be used to extract it
  106. to the ChromeDriver
  107. :Args:
  108. - extension: Base64 encoded string with extension data
  109. """
  110. if extension:
  111. self._extensions.append(extension)
  112. else:
  113. raise ValueError("argument can not be null")
  114. @property
  115. def experimental_options(self):
  116. """
  117. Returns a dictionary of experimental options for chrome.
  118. """
  119. return self._experimental_options
  120. def add_experimental_option(self, name, value):
  121. """
  122. Adds an experimental option which is passed to chrome.
  123. Args:
  124. name: The experimental option name.
  125. value: The option value.
  126. """
  127. self._experimental_options[name] = value
  128. def to_capabilities(self):
  129. """
  130. Creates a capabilities with all the options that have been set and
  131. returns a dictionary with everything
  132. """
  133. chrome = DesiredCapabilities.CHROME.copy()
  134. chrome_options = self.experimental_options.copy()
  135. chrome_options["extensions"] = self.extensions
  136. if self.binary_location:
  137. chrome_options["binary"] = self.binary_location
  138. chrome_options["args"] = self.arguments
  139. if self.debugger_address:
  140. chrome_options["debuggerAddress"] = self.debugger_address
  141. chrome["chromeOptions"] = chrome_options
  142. return chrome