options.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. from selenium.common.exceptions import InvalidArgumentException
  18. from selenium.webdriver.common.proxy import Proxy
  19. from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
  20. from selenium.webdriver.firefox.firefox_profile import FirefoxProfile
  21. class Log(object):
  22. def __init__(self):
  23. self.level = None
  24. def to_capabilities(self):
  25. if self.level is not None:
  26. return {"log": {"level": self.level}}
  27. return {}
  28. class Options(object):
  29. KEY = "moz:firefoxOptions"
  30. def __init__(self):
  31. self._binary = None
  32. self._preferences = {}
  33. self._profile = None
  34. self._proxy = None
  35. self._arguments = []
  36. self.log = Log()
  37. @property
  38. def binary(self):
  39. """Returns the location of the binary."""
  40. return self._binary
  41. @binary.setter
  42. def binary(self, new_binary):
  43. """Sets location of the browser binary, either by string or
  44. ``FirefoxBinary`` instance.
  45. """
  46. if not isinstance(new_binary, FirefoxBinary):
  47. new_binary = FirefoxBinary(new_binary)
  48. self._binary = new_binary
  49. @property
  50. def binary_location(self):
  51. return self.binary
  52. @binary.setter # noqa
  53. def binary_location(self, value):
  54. self.binary = value
  55. @property
  56. def preferences(self):
  57. """Returns a dict of preferences."""
  58. return self._preferences
  59. def set_preference(self, name, value):
  60. """Sets a preference."""
  61. self._preferences[name] = value
  62. @property
  63. def proxy(self):
  64. """ returns Proxy if set otherwise None."""
  65. return self._proxy
  66. @proxy.setter
  67. def proxy(self, value):
  68. if not isinstance(value, Proxy):
  69. raise InvalidArgumentException("Only Proxy objects can be passed in.")
  70. self._proxy = value
  71. @property
  72. def profile(self):
  73. """Returns the Firefox profile to use."""
  74. return self._profile
  75. @profile.setter
  76. def profile(self, new_profile):
  77. """Sets location of the browser profile to use, either by string
  78. or ``FirefoxProfile``.
  79. """
  80. if not isinstance(new_profile, FirefoxProfile):
  81. new_profile = FirefoxProfile(new_profile)
  82. self._profile = new_profile
  83. @property
  84. def arguments(self):
  85. """Returns a list of browser process arguments."""
  86. return self._arguments
  87. def add_argument(self, argument):
  88. """Add argument to be used for the browser process."""
  89. if argument is None:
  90. raise ValueError()
  91. self._arguments.append(argument)
  92. def to_capabilities(self):
  93. """Marshals the Firefox options to a `moz:firefoxOptions`
  94. object.
  95. """
  96. # This intentionally looks at the internal properties
  97. # so if a binary or profile has _not_ been set,
  98. # it will defer to geckodriver to find the system Firefox
  99. # and generate a fresh profile.
  100. opts = {}
  101. if self._binary is not None:
  102. opts["binary"] = self._binary._start_cmd
  103. if len(self._preferences) > 0:
  104. opts["prefs"] = self._preferences
  105. if self._proxy is not None:
  106. self._proxy.add_to_capabilities(opts)
  107. if self._profile is not None:
  108. opts["profile"] = self._profile.encoded
  109. if len(self._arguments) > 0:
  110. opts["args"] = self._arguments
  111. opts.update(self.log.to_capabilities())
  112. if len(opts) > 0:
  113. return {Options.KEY: opts}
  114. return {}