zmc
2023-10-12 ed135d79df12a2466b52dae1a82326941211dcc9
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# Licensed to the Software Freedom Conservancy (SFC) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The SFC licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.
import typing
 
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver.common.options import ArgOptions
 
 
class Log:
    def __init__(self) -> None:
        self.level = None
 
    def to_capabilities(self) -> dict:
        if self.level:
            return {"log": {"level": self.level}}
        return {}
 
 
class Options(ArgOptions):
    KEY = "safari.options"
 
    # @see https://developer.apple.com/documentation/webkit/about_webdriver_for_safari
    AUTOMATIC_INSPECTION = "safari:automaticInspection"
    AUTOMATIC_PROFILING = "safari:automaticProfiling"
 
    SAFARI_TECH_PREVIEW = "Safari Technology Preview"
 
    def __init__(self) -> None:
        super().__init__()
        self._binary_location = None
        self._preferences: dict = {}
        self.log = Log()
 
    @property
    def binary_location(self) -> str:
        """
        :Returns: The location of the browser binary otherwise an empty string
        """
        return self._binary_location
 
    @binary_location.setter
    def binary_location(self, value: str) -> None:
        """Allows you to set the browser binary to launch.
 
        :Args:
         - value : path to the browser binary
        """
        self._binary_location = value
 
    def to_capabilities(self) -> dict:
        """Marshals the  options to an desired capabilities object."""
        # This intentionally looks at the internal properties
        # so if a binary or profile has _not_ been set,
        # it will defer to geckodriver to find the system Firefox
        # and generate a fresh profile.
        caps = self._caps
        opts = {}
 
        if self._arguments:
            opts["args"] = self._arguments
        if self._binary_location:
            opts["binary"] = self._binary_location
        opts.update(self.log.to_capabilities())
 
        if opts:
            caps[Options.KEY] = opts
 
        return caps
 
    @property
    def default_capabilities(self) -> typing.Dict[str, str]:
        return DesiredCapabilities.SAFARI.copy()
 
    @property
    def automatic_inspection(self) -> bool:
        """:Returns: The option Automatic Inspection value"""
        return self._caps.get(self.AUTOMATIC_INSPECTION)
 
    @automatic_inspection.setter
    def automatic_inspection(self, value: bool) -> None:
        """Sets the option Automatic Inspection to value.
 
        :Args:
         - value: boolean value
        """
        self.set_capability(self.AUTOMATIC_INSPECTION, value)
 
    @property
    def automatic_profiling(self) -> bool:
        """:Returns: The options Automatic Profiling value"""
        return self._caps.get(self.AUTOMATIC_PROFILING)
 
    @automatic_profiling.setter
    def automatic_profiling(self, value: bool) -> None:
        """Sets the option Automatic Profiling to value.
 
        :Args:
         - value: boolean value
        """
        self.set_capability(self.AUTOMATIC_PROFILING, value)
 
    @property
    def use_technology_preview(self) -> bool:
        """:Returns: whether BROWSER_NAME is equal to Safari Technology Preview"""
        return self._caps.get("browserName") == self.SAFARI_TECH_PREVIEW
 
    @use_technology_preview.setter
    def use_technology_preview(self, value: bool) -> None:
        """Sets browser name to Safari Technology Preview if value else to
        safari.
 
        :Args:
         - value: boolean value
        """
        self.set_capability("browserName", self.SAFARI_TECH_PREVIEW if value else "safari")