Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/py/selenium/webdriver/safari/options.py
4076 views
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
18
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
19
from selenium.webdriver.common.options import ArgOptions
20
21
22
class _SafariOptionsDescriptor:
23
"""_SafariOptionsDescriptor is an implementation of Descriptor protocol.
24
25
Any look-up or assignment to the below attributes in `Options` class will be intercepted
26
by `__get__` and `__set__` method respectively when an attribute lookup happens:
27
28
- `automatic_inspection`
29
- `automatic_profiling`
30
- `use_technology_preview`
31
32
Example:
33
`self.automatic_inspection`
34
(`__get__` method does a dictionary look up in the dictionary `_caps` of `Options` class
35
and returns the value of key `safari:automaticInspection`)
36
37
Example:
38
`self.automatic_inspection` = True
39
(`__set__` method sets/updates the value of the key `safari:automaticInspection` in `_caps`
40
dictionary in `Options` class)
41
"""
42
43
def __init__(self, name, expected_type):
44
self.name = name
45
self.expected_type = expected_type
46
47
def __get__(self, obj, cls):
48
if self.name == "Safari Technology Preview":
49
return obj._caps.get("browserName") == self.name
50
return obj._caps.get(self.name)
51
52
def __set__(self, obj, value):
53
if not isinstance(value, self.expected_type):
54
raise TypeError(f"{self.name} must be of type {self.expected_type}")
55
if self.name == "Safari Technology Preview":
56
obj._caps["browserName"] = self.name if value else "safari"
57
else:
58
obj._caps[self.name] = value
59
60
61
class Options(ArgOptions):
62
# @see https://developer.apple.com/documentation/webkit/about_webdriver_for_safari
63
AUTOMATIC_INSPECTION = "safari:automaticInspection"
64
AUTOMATIC_PROFILING = "safari:automaticProfiling"
65
SAFARI_TECH_PREVIEW = "Safari Technology Preview"
66
67
# creating descriptor objects
68
automatic_inspection = _SafariOptionsDescriptor(AUTOMATIC_INSPECTION, bool)
69
"""Whether to enable automatic inspection."""
70
71
automatic_profiling = _SafariOptionsDescriptor(AUTOMATIC_PROFILING, bool)
72
"""Whether to enable automatic profiling."""
73
74
use_technology_preview = _SafariOptionsDescriptor(SAFARI_TECH_PREVIEW, bool)
75
"""Whether to use Safari Technology Preview."""
76
77
@property
78
def default_capabilities(self) -> dict[str, str]:
79
return DesiredCapabilities.SAFARI.copy()
80
81