|
20 | 20 | from selenium.webdriver.common.options import ArgOptions
|
21 | 21 |
|
22 | 22 |
|
| 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 | + |
23 | 63 | class Options(ArgOptions):
|
24 | 64 | # @see https://developer.apple.com/documentation/webkit/about_webdriver_for_safari
|
25 | 65 | AUTOMATIC_INSPECTION = "safari:automaticInspection"
|
26 | 66 | AUTOMATIC_PROFILING = "safari:automaticProfiling"
|
27 |
| - |
28 | 67 | SAFARI_TECH_PREVIEW = "Safari Technology Preview"
|
29 | 68 |
|
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: |
33 | 72 |
|
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` |
38 | 79 |
|
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: |
42 | 86 |
|
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` |
49 | 93 |
|
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: |
54 | 100 |
|
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` |
58 | 107 |
|
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 | + """ |
65 | 112 |
|
66 | 113 | @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