| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
import typing |
| 18 |
|
| 19 |
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities |
| 20 |
from selenium.webdriver.common.options import ArgOptions |
| 21 |
|
| 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 |
|
| 63 |
class Options(ArgOptions): |
| 64 |
|
| 65 |
AUTOMATIC_INSPECTION = "safari:automaticInspection" |
| 66 |
AUTOMATIC_PROFILING = "safari:automaticProfiling" |
| 67 |
SAFARI_TECH_PREVIEW = "Safari Technology Preview" |
| 68 |
|
| 69 |
|
| 70 |
automatic_inspection = _SafariOptionsDescriptor(AUTOMATIC_INSPECTION, bool) |
| 71 |
"""Get or Set Automatic Inspection value: |
| 72 |
|
| 73 |
Usage |
| 74 |
----- |
| 75 |
- Get |
| 76 |
- `self.automatic_inspection` |
| 77 |
- Set |
| 78 |
- `self.automatic_inspection` = `value` |
| 79 |
|
| 80 |
Parameters |
| 81 |
---------- |
| 82 |
`value`: `bool` |
| 83 |
""" |
| 84 |
automatic_profiling = _SafariOptionsDescriptor(AUTOMATIC_PROFILING, bool) |
| 85 |
"""Get or Set Automatic Profiling value: |
| 86 |
|
| 87 |
Usage |
| 88 |
----- |
| 89 |
- Get |
| 90 |
- `self.automatic_profiling` |
| 91 |
- Set |
| 92 |
- `self.automatic_profiling` = `value` |
| 93 |
|
| 94 |
Parameters |
| 95 |
---------- |
| 96 |
`value`: `bool` |
| 97 |
""" |
| 98 |
use_technology_preview = _SafariOptionsDescriptor(SAFARI_TECH_PREVIEW, bool) |
| 99 |
"""Get and Set Technology Preview: |
| 100 |
|
| 101 |
Usage |
| 102 |
----- |
| 103 |
- Get |
| 104 |
- `self.use_technology_preview` |
| 105 |
- Set |
| 106 |
- `self.use_technology_preview` = `value` |
| 107 |
|
| 108 |
Parameters |
| 109 |
---------- |
| 110 |
`value`: `bool` |
| 111 |
""" |
| 112 |
|
| 113 |
@property |
| 114 |
def default_capabilities(self) -> typing.Dict[str, str]: |
| 115 |
return DesiredCapabilities.SAFARI.copy() |