Skip to content

Commit bc8ff4f

Browse files
[py] moved safari options to descriptor class (#12553)
1 parent 5336d62 commit bc8ff4f

File tree

1 file changed

+79
-45
lines changed

1 file changed

+79
-45
lines changed

py/selenium/webdriver/safari/options.py

Lines changed: 79 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -20,62 +20,96 @@
2020
from selenium.webdriver.common.options import ArgOptions
2121

2222

23+
class _SafariOptionsDescriptor:
24+
"""_SafariOptionsDescriptor is an implementation of Descriptor protocol:
25+
26+
: Any look-up or assignment to the below attributes in `Options` class will be intercepted
27+
by `__get__` and `__set__` method respectively.
28+
29+
- `automatic_inspection`
30+
- `automatic_profiling`
31+
- `use_technology_preview`
32+
33+
: When an attribute lookup happens,
34+
Example:
35+
`self.automatic_inspection`
36+
`__get__` method does a dictionary look up in the dictionary `_caps` of `Options` class
37+
and returns the value of key `safari:automaticInspection`
38+
: When an attribute assignment happens,
39+
Example:
40+
`self.automatic_inspection` = True
41+
`__set__` method sets/updates the value of the key `safari:automaticInspection` in `_caps`
42+
dictionary in `Options` class.
43+
"""
44+
45+
def __init__(self, name, expected_type):
46+
self.name = name
47+
self.expected_type = expected_type
48+
49+
def __get__(self, obj, cls):
50+
if self.name == "Safari Technology Preview":
51+
return obj._caps.get("browserName") == self.name
52+
return obj._caps.get(self.name)
53+
54+
def __set__(self, obj, value):
55+
if not isinstance(value, self.expected_type):
56+
raise TypeError(f"{self.name} must be of type {self.expected_type}")
57+
if self.name == "Safari Technology Preview":
58+
obj._caps["browserName"] = self.name if value else "safari"
59+
else:
60+
obj._caps[self.name] = value
61+
62+
2363
class Options(ArgOptions):
2464
# @see https://developer.apple.com/documentation/webkit/about_webdriver_for_safari
2565
AUTOMATIC_INSPECTION = "safari:automaticInspection"
2666
AUTOMATIC_PROFILING = "safari:automaticProfiling"
27-
2867
SAFARI_TECH_PREVIEW = "Safari Technology Preview"
2968

30-
@property
31-
def default_capabilities(self) -> typing.Dict[str, str]:
32-
return DesiredCapabilities.SAFARI.copy()
69+
# creating descriptor objects
70+
automatic_inspection = _SafariOptionsDescriptor(AUTOMATIC_INSPECTION, bool)
71+
"""Get or Set Automatic Inspection value:
3372
34-
@property
35-
def automatic_inspection(self) -> bool:
36-
""":Returns: The option Automatic Inspection value"""
37-
return self._caps.get(self.AUTOMATIC_INSPECTION)
73+
Usage
74+
-----
75+
- Get
76+
- `self.automatic_inspection`
77+
- Set
78+
- `self.automatic_inspection` = `value`
3879
39-
@automatic_inspection.setter
40-
def automatic_inspection(self, value: bool) -> None:
41-
"""Sets the option Automatic Inspection to value.
80+
Parameters
81+
----------
82+
`value`: `bool`
83+
"""
84+
automatic_profiling = _SafariOptionsDescriptor(AUTOMATIC_PROFILING, bool)
85+
"""Get or Set Automatic Profiling value:
4286
43-
:Args:
44-
- value: boolean value
45-
"""
46-
if not isinstance(value, bool):
47-
raise TypeError("Automatic Inspection must be a boolean")
48-
self.set_capability(self.AUTOMATIC_INSPECTION, value)
87+
Usage
88+
-----
89+
- Get
90+
- `self.automatic_profiling`
91+
- Set
92+
- `self.automatic_profiling` = `value`
4993
50-
@property
51-
def automatic_profiling(self) -> bool:
52-
""":Returns: The options Automatic Profiling value"""
53-
return self._caps.get(self.AUTOMATIC_PROFILING)
94+
Parameters
95+
----------
96+
`value`: `bool`
97+
"""
98+
use_technology_preview = _SafariOptionsDescriptor(SAFARI_TECH_PREVIEW, bool)
99+
"""Get and Set Technology Preview:
54100
55-
@automatic_profiling.setter
56-
def automatic_profiling(self, value: bool) -> None:
57-
"""Sets the option Automatic Profiling to value.
101+
Usage
102+
-----
103+
- Get
104+
- `self.use_technology_preview`
105+
- Set
106+
- `self.use_technology_preview` = `value`
58107
59-
:Args:
60-
- value: boolean value
61-
"""
62-
if not isinstance(value, bool):
63-
raise TypeError("Automatic Profiling must be a boolean")
64-
self.set_capability(self.AUTOMATIC_PROFILING, value)
108+
Parameters
109+
----------
110+
`value`: `bool`
111+
"""
65112

66113
@property
67-
def use_technology_preview(self) -> bool:
68-
""":Returns: whether BROWSER_NAME is equal to Safari Technology Preview"""
69-
return self._caps.get("browserName") == self.SAFARI_TECH_PREVIEW
70-
71-
@use_technology_preview.setter
72-
def use_technology_preview(self, value: bool) -> None:
73-
"""Sets browser name to Safari Technology Preview if value else to
74-
safari.
75-
76-
:Args:
77-
- value: boolean value
78-
"""
79-
if not isinstance(value, bool):
80-
raise TypeError("Use Technology Preview must be a boolean")
81-
self.set_capability("browserName", self.SAFARI_TECH_PREVIEW if value else "safari")
114+
def default_capabilities(self) -> typing.Dict[str, str]:
115+
return DesiredCapabilities.SAFARI.copy()

0 commit comments

Comments
 (0)